Skip to content

Commit 60fe5a8

Browse files
feat(httputil): add debug logging to HTTP utility functions
Add a logger (httputil:httputil) and debug logging calls to the three utility functions in internal/httputil/httputil.go: - WriteJSONResponse: log status code, body size, and marshal errors - ParseRateLimitResetHeader: log parsed reset time or parse failures - IsTransientHTTPError: log when a transient error code is detected These helpers are called widely across the server and proxy packages, so debug logging here makes it easier to trace HTTP response behaviour and rate-limit handling during troubleshooting. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8f964e0 commit 60fe5a8

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

internal/httputil/httputil.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,24 @@ import (
88
"strconv"
99
"strings"
1010
"time"
11+
12+
"github.com/github/gh-aw-mcpg/internal/logger"
1113
)
1214

15+
var logHTTP = logger.New("httputil:httputil")
16+
1317
// WriteJSONResponse sets the Content-Type header, writes the status code, and encodes
1418
// body as JSON. It centralises the three-line pattern used across HTTP handlers.
1519
func WriteJSONResponse(w http.ResponseWriter, statusCode int, body interface{}) {
20+
logHTTP.Printf("Writing JSON response: statusCode=%d", statusCode)
1621
w.Header().Set("Content-Type", "application/json")
1722
w.WriteHeader(statusCode)
1823
data, err := json.Marshal(body)
1924
if err != nil {
25+
logHTTP.Printf("Failed to marshal JSON response body: %v", err)
2026
return
2127
}
28+
logHTTP.Printf("JSON response body size: %d bytes", len(data))
2229
w.Write(data)
2330
}
2431

@@ -31,15 +38,22 @@ func ParseRateLimitResetHeader(value string) time.Time {
3138
}
3239
unix, err := strconv.ParseInt(strings.TrimSpace(value), 10, 64)
3340
if err != nil {
41+
logHTTP.Printf("Failed to parse X-RateLimit-Reset header value=%q: %v", value, err)
3442
return time.Time{}
3543
}
36-
return time.Unix(unix, 0)
44+
reset := time.Unix(unix, 0)
45+
logHTTP.Printf("Parsed X-RateLimit-Reset: resetAt=%s", reset.UTC().Format(time.RFC3339))
46+
return reset
3747
}
3848

3949
// IsTransientHTTPError returns true for status codes that indicate a temporary
4050
// server-side condition (rate-limiting or transient failure) worth retrying.
4151
func IsTransientHTTPError(statusCode int) bool {
42-
return statusCode == http.StatusTooManyRequests ||
52+
transient := statusCode == http.StatusTooManyRequests ||
4353
statusCode == http.StatusServiceUnavailable ||
4454
(statusCode >= 500 && statusCode < 600)
55+
if transient {
56+
logHTTP.Printf("Transient HTTP error detected: statusCode=%d", statusCode)
57+
}
58+
return transient
4559
}

0 commit comments

Comments
 (0)