Repository navigation
feat(apiserver): emit JSON request trace and service logs - #10184
Conversation
Windows Unit Test Results 3 files 21 suites 53s ⏱️ Results for commit 215eb43. ♻️ This comment has been updated with latest results. |
Write incoming request traces and standard library logs as JSON lines with a "source" field, so the AKS log collector can route agentbakersvc logs to the IncomingRequestTrace and FrontEndContextlessActivity Kusto tables. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The starter now configures both JSON loggers. The apiserver package no longer writes to stdout by default. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
init runs before the importing main, so its early log calls are JSON too. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Query strings may carry secrets, so targetURI logs only the path. Add clientRequestID and operationName to match the IncomingRequestTrace schema, and keep NewAPIServer from mutating the caller's Options. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Handler errors log at ERROR with operationID, correlationID, and clientRequestID, so they can be joined to the request trace. Other log calls become structured slog records. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Gorilla recovery logged panics through the package-level log, without request IDs, so a stack trace could not be joined to its request trace. Replace it with a small middleware that logs through requestLogger. Restore the log package output in tests after slog.SetDefault. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
27cbd96 to
0d8091d
Compare
The x-ms-* header names follow the Azure lowercase convention. http.Header canonicalizes keys on Get and Set, so lowercase constants behave the same. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
0d8091d to
97d568c
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
The external Kusto ingestion contract remains unverified, requiring owner-led end-to-end validation of collector routing, field names and types, coordinated pod labels, and propagation of all three request-ID headers.
No open issues 🎉
What changed in this PR
This PR prepares AgentBaker service logs for Kusto collection by emitting JSON with collector routing fields and request IDs. Ingestion still depends on the companion deployment labels described in the PR.
Changes:
- Adds configurable request tracing with Start/End records, response metrics, and health-check exclusion.
- Configures JSON service logging at startup and correlates handler errors and recovered panics with request IDs.
- Adds logging tests, removes the replaced recovery dependency, and disables the canonical-header linter.
| File | Description |
|---|---|
pkg/agent/toggles/types.go |
Migrates JSON-marshalling error logs to slog. |
go.sum |
Removes checksums for the unused recovery dependencies. |
go.mod |
Removes gorilla/handlers and indirect httpsnoop. |
cmd/starter/start.go |
Initializes JSON service and request loggers before main runs. |
apiserver/routers.go |
Adds request tracing and request-correlated panic recovery. |
apiserver/requestlog.go |
Implements JSON logger creation, request fields, and response metrics. |
apiserver/requestlog_test.go |
Tests trace records, error correlation, panic logging, and health-check exclusion. |
apiserver/getnodebootstrapdata.go |
Adds request IDs to bootstrap handler error logs. |
apiserver/getlatestsigimageconfig.go |
Adds request IDs to latest-image handler error logs. |
apiserver/getdistrosigimageconfig.go |
Adds request IDs to distro-image handler error logs. |
apiserver/apiserver.go |
Exposes Options.RequestLogger and migrates the startup log to slog. |
.golangci.yaml |
Disables the canonicalheader linter. |
Review effort: Balanced
Use method patterns from the standard library router. Wrap each API route with its own request-trace middleware, so the trace knows the route name, and /healthz and unmatched requests get no trace without a special case. Write one trace record per request: the end record has everything the start record had, plus status and duration. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Unverified external AKS collector schemas and pod-label routing require service-owner validation that both log sources reach their intended Kusto tables with request/error correlation preserved.
No open issues 🎉
Review effort: Balanced
|
Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
215eb4359c536e7e0cec72dd3f65fcaf22be1493Options.RequestLogger;cmd/starterconfigures both loggers at package initialization.↑ Unit tests cover the trace record fields, status and content length, and no trace for healthz, 404 and 405.
↑ Local binary run: 400 and recovered-panic 500 traced; all service logs emitted as JSON.
↑ golangci-lint: 0 issues on changed packages.
↑ GPT-6.2 Sol review findings addressed (client request ID, Options mutation).
↑ Handler error and recovered-panic records carry the request IDs (unit tests; handler errors also in a local binary run).
↑ Second GPT-6.2 Sol review: panic correlation and test logger leak fixed.
↑ Rebased on
dev;canonicalheaderdisabled in.golangci.yaml; lint reports 0 issues.↑ Claude Opus 5.5 review found no significant issues in this PR.
↑ After the switch to
http.ServeMux, a local binary run shows one trace per API call and none for healthz or unmatched paths.↓ Trailing-slash paths now return 404; gorilla
StrictSlashredirected them. The RP client does not use trailing slashes.↓ Kusto ingestion not verified; it also needs agentbakersvc pod labels in aks-rp.
↓ CI not yet inspected.
What this PR does / why we need it:
agentbakersvc logs do not reach Kusto. The AKS log collector on the service underlay collects only JSON stdout lines and routes them by their
sourcefield. AgentBaker writes plain-textloglines, so all of them are dropped.This PR:
apiserver/requestlog.go). It writes oneAgentBakerHttpRequestJSON record per request withsource: IncomingRequestTraceLog. Fields:operationID(x-ms-acs-operation-id),correlationID,clientRequestID,httpMethod,targetURI(path only),routeName,operationName,userAgent,clientRemoteAddr,StatusCode,contentLength,durationInMilliseconds. Field names follow the AKSIncomingRequestTraceschema./healthzis not logged.gorilla/muxwith the standard libraryhttp.ServeMuxmethod patterns. Each API route is wrapped with its own middleware chain, so the trace has the route name without router-specific lookups.slogdefault logger withsource: AcsContextlessTraceLogincmd/starterinit(). Logs in the importingmainbeforestarter.Executeare JSON too, becauseSetDefaultalso redirects the stdliblogpackage.logcalls toslog. Handler errors log atERRORwithoperationID,correlationIDandclientRequestID, so they can be joined to the request trace.gorilla/handlersdependency.canonicalheaderlinter: thex-ms-*header names follow the Azure lowercase convention, andhttp.Headercanonicalizes keys.Options.RequestLogger. Thecmd/starterdefault options set it to a JSON stdout logger withsource: IncomingRequestTraceLog; if it is nil, traces are discarded. Two loggers are necessary: the default logger already has asourceattribute, and slog does not remove duplicate keys.The aks-rp agentbakersvc deployment adds pod labels that map these sources to the
IncomingRequestTraceandFrontEndContextlessActivitytables, then bumps to the release that contains this change.Which issue(s) this PR fixes:
AB#31308152 (Augment RP Logs around ABsvc request).