-
-
Notifications
You must be signed in to change notification settings - Fork 804
Expand file tree
/
Copy pathdebug.go
More file actions
205 lines (181 loc) · 6.56 KB
/
Copy pathdebug.go
File metadata and controls
205 lines (181 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) 2015-present Jeevanandam M (jeeva@myjeeva.com), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT
package resty
import (
"fmt"
"net/http"
"time"
)
type (
// DebugLogCallbackFunc is called with the fully-populated [DebugLog] before
// Resty formats or writes the debug output. Use it to inspect or mutate the
// log entry, for example to add custom fields.
//
// See [Client.OnDebugLog].
DebugLogCallbackFunc func(*DebugLog)
// DebugLogFormatterFunc formats a [DebugLog] into a string for debug output.
// See the built-in implementations [DebugLogFormatter] and [DebugLogJSONFormatter].
//
// See [Client.SetDebugLogFormatter].
DebugLogFormatterFunc func(*DebugLog) string
// DebugLog holds the request, response, and optional trace details captured
// during a single Resty request execution for debug logging.
DebugLog struct {
Request *DebugLogRequest `json:"request"`
Response *DebugLogResponse `json:"response"`
TraceInfo *TraceInfo `json:"trace_info"`
}
// DebugLogRequest captures debug information about a [Request].
DebugLogRequest struct {
// CorrelationID is the request correlation ID (see [Request.SetCorrelationID]).
CorrelationID string `json:"correlation_id"`
// Host is the target host of the request.
Host string `json:"host"`
// URI is the request URI including path and query string.
URI string `json:"uri"`
// Method is the HTTP method of the request.
Method string `json:"method"`
// Proto is the HTTP protocol version, e.g. "HTTP/1.1".
Proto string `json:"proto"`
// Header contains the outgoing request headers (sensitive values are redacted).
Header http.Header `json:"header"`
// CurlCmd is the equivalent curl command string, populated when curl command
// generation and debug logging are both enabled.
CurlCmd string `json:"curl_cmd"`
// Attempt is the current attempt number (1 = initial, >1 = retry).
Attempt int `json:"attempt"`
// Body is the request body as a string, truncated to DebugBodyLimit if set.
Body string `json:"body"`
}
// DebugLogResponse captures debug information about a [Response].
DebugLogResponse struct {
// StatusCode is the HTTP response status code.
StatusCode int `json:"status_code"`
// Status is the HTTP response status text, e.g. "200 OK".
Status string `json:"status"`
// Proto is the HTTP protocol version, e.g. "HTTP/1.1".
Proto string `json:"proto"`
// ReceivedAt is the time at which the response was received.
ReceivedAt time.Time `json:"received_at"`
// Duration is the time elapsed from sending the request to receiving the response.
Duration time.Duration `json:"duration"`
// Size is the number of bytes in the response body.
Size int64 `json:"size"`
// Header contains the response headers (sensitive values are redacted).
Header http.Header `json:"header"`
// Body is the response body as a string, truncated to DebugBodyLimit if set.
Body string `json:"body"`
}
)
// DebugLogFormatter formats a [DebugLog] as a human-readable multi-line string.
//
// This is the default debug log formatter used by Resty.
func DebugLogFormatter(dl *DebugLog) string {
debugLog := "\n==============================================================================\n"
req := dl.Request
if len(req.CurlCmd) > 0 {
debugLog += "~~~ REQUEST(CURL) ~~~\n" +
fmt.Sprintf(" %v\n", req.CurlCmd)
}
debugLog += "~~~ REQUEST ~~~\n" +
fmt.Sprintf("CORRELATION ID: %s\n", req.CorrelationID) +
fmt.Sprintf("%s %s %s\n", req.Method, req.URI, req.Proto) +
fmt.Sprintf("HOST : %s\n", req.Host) +
fmt.Sprintf("HEADERS:\n%s\n", composeHeaders(req.Header)) +
fmt.Sprintf("BODY :\n%v\n", req.Body) +
fmt.Sprintf("ATTEMPT : %d\n", req.Attempt) +
"------------------------------------------------------------------------------\n"
res := dl.Response
debugLog += "~~~ RESPONSE ~~~\n" +
fmt.Sprintf("STATUS : %s\n", res.Status) +
fmt.Sprintf("PROTO : %s\n", res.Proto) +
fmt.Sprintf("RECEIVED AT : %v\n", res.ReceivedAt.Format(time.RFC3339Nano)) +
fmt.Sprintf("DURATION : %v\n", res.Duration) +
"HEADERS :\n" +
composeHeaders(res.Header) + "\n" +
fmt.Sprintf("BODY :\n%v\n", res.Body)
if dl.TraceInfo != nil {
debugLog += "------------------------------------------------------------------------------\n"
debugLog += fmt.Sprintf("%v\n", dl.TraceInfo)
}
debugLog += "==============================================================================\n"
return debugLog
}
// DebugLogJSONFormatter formats a [DebugLog] as a JSON string.
func DebugLogJSONFormatter(dl *DebugLog) string {
return toJSON(dl)
}
func debugLogger(c *Client, res *Response) {
req := res.Request
if !req.IsDebug {
return
}
rdl := &DebugLogResponse{
StatusCode: res.StatusCode(),
Status: res.Status(),
Proto: res.Proto(),
ReceivedAt: res.ReceivedAt(),
Duration: res.Duration(),
Size: res.Size(),
Header: sanitizeHeaders(res.Header().Clone()),
Body: res.fmtBodyString(res.Request.DebugBodyLimit),
}
// prepareRequestDebugInfo populates this before the request is sent. Guard the
// assertion anyway so a logging path can never panic.
rql, _ := req.values[debugRequestLogKey].(*DebugLogRequest)
if rql == nil {
rql = &DebugLogRequest{}
}
dl := &DebugLog{
Request: rql,
Response: rdl,
}
if res.Request.IsTrace {
ti := req.TraceInfo()
dl.TraceInfo = &ti
}
dblCallback := c.debugLogCallbackFunc()
if dblCallback != nil {
dblCallback(dl)
}
formatterFunc := c.debugLogFormatterFunc()
if formatterFunc != nil {
debugLog := formatterFunc(dl)
req.log.Debugf("%s", debugLog)
}
}
const debugRequestLogKey = "__restyDebugRequestLog"
func prepareRequestDebugInfo(c *Client, r *Request) {
if !r.IsDebug {
return
}
rr := r.RawRequest
rh := rr.Header.Clone()
if c.Client().Jar != nil {
for _, cookie := range c.Client().Jar.Cookies(r.RawRequest.URL) {
s := fmt.Sprintf("%s=%s", cookie.Name, cookie.Value)
if c := rh.Get(hdrCookieKey); isStringEmpty(c) {
rh.Set(hdrCookieKey, s)
} else {
rh.Set(hdrCookieKey, c+"; "+s)
}
}
}
rdl := &DebugLogRequest{
CorrelationID: r.CorrelationID,
Host: rr.URL.Host,
URI: rr.URL.RequestURI(),
Method: r.Method,
Proto: rr.Proto,
Header: sanitizeHeaders(rh),
Attempt: r.Attempt,
Body: r.fmtBodyString(r.DebugBodyLimit),
}
if r.isCurlCmdGenerate && r.isCurlCmdDebugLog {
rdl.CurlCmd = r.curlCmdString
}
r.initValuesMap()
r.values[debugRequestLogKey] = rdl
}