Skip to content

Commit 6b2ae42

Browse files
committed
Add logging for outgoing and incoming cookies
1 parent c96bbe5 commit 6b2ae42

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

httpclient/httpclient_mocklogger.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@ func (m *MockLogger) LogRateLimiting(event string, method string, url string, re
9696
func (m *MockLogger) LogResponse(event string, method string, url string, statusCode int, responseBody string, responseHeaders map[string][]string, duration time.Duration) {
9797
m.Called(event, method, url, statusCode, responseBody, responseHeaders, duration)
9898
}
99+
100+
func (m *MockLogger) LogCookies(direction string, obj interface{}, method, url string) {
101+
m.Called(direction, obj, method, url)
102+
}

httpclient/httpclient_request.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,16 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
160160
var retryCount int
161161
for time.Now().Before(totalRetryDeadline) { // Check if the current time is before the total retry deadline
162162
req = req.WithContext(ctx)
163+
164+
// Log outgoing cookies
165+
log.LogCookies("outgoing", req, method, endpoint)
166+
167+
// Execute the HTTP request
163168
resp, err = c.do(req, log, method, endpoint)
169+
170+
// Log outgoing cookies
171+
log.LogCookies("incoming", req, method, endpoint)
172+
164173
// Check for successful status code
165174
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 400 {
166175
if resp.StatusCode >= 300 {
@@ -301,12 +310,18 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
301310

302311
req = req.WithContext(ctx)
303312

313+
// Log outgoing cookies
314+
log.LogCookies("outgoing", req, method, endpoint)
315+
304316
// Execute the HTTP request
305317
resp, err := c.do(req, log, method, endpoint)
306318
if err != nil {
307319
return nil, err
308320
}
309321

322+
// Log incoming cookies
323+
log.LogCookies("incoming", req, method, endpoint)
324+
310325
// Checks for the presence of a deprecation header in the HTTP response and logs if found.
311326
CheckDeprecationHeader(resp, log)
312327

@@ -341,7 +356,9 @@ func (c *Client) executeRequest(method, endpoint string, body, out interface{})
341356
// This function should be used whenever the client needs to send an HTTP request. It abstracts away the common logic of
342357
// request execution and error handling, providing detailed logs for debugging and monitoring.
343358
func (c *Client) do(req *http.Request, log logger.Logger, method, endpoint string) (*http.Response, error) {
359+
344360
resp, err := c.httpClient.Do(req)
361+
345362
if err != nil {
346363
// Log the error with structured logging, including method, endpoint, and the error itself
347364
log.Error("Failed to send request",

logger/zaplogger_logfields.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package logger
22

33
import (
4+
"net/http"
45
"time"
56

67
"go.uber.org/zap"
@@ -115,3 +116,37 @@ func (d *defaultLogger) LogResponse(event string, method string, url string, sta
115116
d.logger.Info("HTTP response details", fields...)
116117
}
117118
}
119+
120+
// LogCookies logs the cookies associated with an HTTP request or response.
121+
// `direction` indicates whether the cookies are being sent ("outgoing") or received ("incoming").
122+
// `obj` can be either *http.Request or *http.Response.
123+
func (d *defaultLogger) LogCookies(direction string, obj interface{}, method, url string) {
124+
var cookies []*http.Cookie
125+
var objectType string
126+
127+
// Determine the type and extract cookies
128+
switch v := obj.(type) {
129+
case *http.Request:
130+
cookies = v.Cookies()
131+
objectType = "request"
132+
case *http.Response:
133+
cookies = v.Cookies()
134+
objectType = "response"
135+
default:
136+
// Log a warning if the object is not a request or response
137+
d.logger.Warn("Invalid object type for cookie logging", zap.Any("object", obj))
138+
return
139+
}
140+
141+
// Log the cookies if any are present
142+
if len(cookies) > 0 {
143+
fields := []zap.Field{
144+
zap.String("direction", direction),
145+
zap.String("object_type", objectType),
146+
zap.String("method", method),
147+
zap.String("url", url),
148+
zap.Any("cookies", cookies),
149+
}
150+
d.logger.Debug("Cookies logged", fields...)
151+
}
152+
}

logger/zaplogger_logger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type Logger interface {
3939
LogRetryAttempt(event string, method string, url string, attempt int, reason string, waitDuration time.Duration, err error)
4040
LogRateLimiting(event string, method string, url string, retryAfter string, waitDuration time.Duration)
4141
LogResponse(event string, method string, url string, statusCode int, responseBody string, responseHeaders map[string][]string, duration time.Duration)
42+
LogCookies(direction string, obj interface{}, method, url string)
4243
}
4344

4445
// GetLogLevel returns the current logging level of the logger. This allows for checking the logger's

0 commit comments

Comments
 (0)