Skip to content

Commit 80ecf22

Browse files
authored
[log] feat(httputil): add debug logging to HTTP utility functions (#4324)
Add a `logHTTP` debug logger (`httputil:httputil`) and meaningful logging calls to the three utility functions in `internal/httputil/httputil.go`. ## Changes **File modified:** `internal/httputil/httputil.go` (1 file, focused PR) ### Logger added ```go var logHTTP = logger.New("httputil:httputil") ``` ### Logging calls added | Function | What's logged | |---|---| | `WriteJSONResponse` | Status code, serialised body size, and marshal errors | | `ParseRateLimitResetHeader` | Parsed reset timestamp (RFC 3339) or parse failure | | `IsTransientHTTPError` | Status code when a transient error is detected | ## Why this is useful These helpers are called across the `server` and `proxy` packages. Having debug output here makes it straightforward to trace: - Which status codes are being written to clients - Whether `X-RateLimit-Reset` headers are being parsed correctly (relevant to the circuit-breaker reset logic) - When the gateway decides a backend response is transient and eligible for retry Enable with: ```bash DEBUG=httputil:* ./awmg --config config.toml ``` ## Validation - `go build ./...` ✅ - `go vet ./internal/httputil/...` ✅ - `go test ./internal/httputil/...` ✅ > Generated by [Go Logger Enhancement](https://github.com/github/gh-aw-mcpg/actions/runs/24761251508/agentic_workflow) · ● 2M · [◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+go-logger%22&type=pullrequests) <!-- gh-aw-agentic-workflow: Go Logger Enhancement, engine: copilot, model: auto, id: 24761251508, workflow_id: go-logger, run: https://github.com/github/gh-aw-mcpg/actions/runs/24761251508 --> <!-- gh-aw-workflow-id: go-logger -->
2 parents 388c54f + 707dc37 commit 80ecf22

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

internal/httputil/httputil.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,32 @@ 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
}
22-
w.Write(data)
28+
logHTTP.Printf("JSON response body size: %d bytes", len(data))
29+
n, err := w.Write(data)
30+
if err != nil {
31+
logHTTP.Printf("Failed to write JSON response body: wrote=%d expected=%d err=%v", n, len(data), err)
32+
return
33+
}
34+
if n != len(data) {
35+
logHTTP.Printf("Short write for JSON response body: wrote=%d expected=%d", n, len(data))
36+
}
2337
}
2438

2539
// ParseRateLimitResetHeader parses the Unix-timestamp value of the
@@ -31,15 +45,22 @@ func ParseRateLimitResetHeader(value string) time.Time {
3145
}
3246
unix, err := strconv.ParseInt(strings.TrimSpace(value), 10, 64)
3347
if err != nil {
48+
logHTTP.Printf("Failed to parse X-RateLimit-Reset header value=%q: %v", value, err)
3449
return time.Time{}
3550
}
36-
return time.Unix(unix, 0)
51+
reset := time.Unix(unix, 0)
52+
logHTTP.Printf("Parsed X-RateLimit-Reset: resetAt=%s", reset.UTC().Format(time.RFC3339))
53+
return reset
3754
}
3855

3956
// IsTransientHTTPError returns true for status codes that indicate a temporary
4057
// server-side condition (rate-limiting or transient failure) worth retrying.
4158
func IsTransientHTTPError(statusCode int) bool {
42-
return statusCode == http.StatusTooManyRequests ||
59+
transient := statusCode == http.StatusTooManyRequests ||
4360
statusCode == http.StatusServiceUnavailable ||
4461
(statusCode >= 500 && statusCode < 600)
62+
if transient {
63+
logHTTP.Printf("Transient HTTP error detected: statusCode=%d", statusCode)
64+
}
65+
return transient
4566
}

0 commit comments

Comments
 (0)