Skip to content

feat(apiserver): emit JSON request trace and service logs - #10184

Merged
Artur Khantimirov (r2k1) merged 8 commits into
devfrom
akhantimirov/absvc-json-logging
Nov 25, 2026
Merged

Artur Khantimirov (r2k1) merged 8 commits into
devfrom
akhantimirov/absvc-json-logging

Conversation

@r2k1

@r2k1 Artur Khantimirov (r2k1) commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 215eb4359c536e7e0cec72dd3f65fcaf22be1493

Dimension Assessment Rationale
Change confidence 8.5/10 ↑ Request logger is injected via Options.RequestLogger; cmd/starter configures 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; canonicalheader disabled 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 StrictSlash redirected 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 source field. AgentBaker writes plain-text log lines, so all of them are dropped.

This PR:

  • Adds a request-trace middleware (apiserver/requestlog.go). It writes one AgentBakerHttpRequest JSON record per request with source: 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 AKS IncomingRequestTrace schema. /healthz is not logged.
  • Puts the middleware outside the timeout and recovery handlers, so 503 timeouts and 500s from recovered panics are also recorded. Unmatched routes (404, 405) are not logged.
  • Replaces gorilla/mux with the standard library http.ServeMux method patterns. Each API route is wrapped with its own middleware chain, so the trace has the route name without router-specific lookups.
  • Sets a JSON slog default logger with source: AcsContextlessTraceLog in cmd/starter init(). Logs in the importing main before starter.Execute are JSON too, because SetDefault also redirects the stdlib log package.
  • Migrates the service log calls to slog. Handler errors log at ERROR with operationID, correlationID and clientRequestID, so they can be joined to the request trace.
  • Replaces the gorilla recovery handler with a small middleware that logs the panic and stack with the same request IDs. This removes the gorilla/handlers dependency.
  • Disables the canonicalheader linter: the x-ms-* header names follow the Azure lowercase convention, and http.Header canonicalizes keys.
  • Adds Options.RequestLogger. The cmd/starter default options set it to a JSON stdout logger with source: IncomingRequestTraceLog; if it is nil, traces are discarded. Two loggers are necessary: the default logger already has a source attribute, and slog does not remove duplicate keys.

The aks-rp agentbakersvc deployment adds pod labels that map these sources to the IncomingRequestTrace and FrontEndContextlessActivity tables, then bumps to the release that contains this change.

Which issue(s) this PR fixes:

AB#31308152 (Augment RP Logs around ABsvc request).

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Windows Unit Test Results

  3 files   21 suites   53s ⏱️
817 tests 817 ✅ 0 💤 0 ❌
844 runs  844 ✅ 0 💤 0 ❌

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>
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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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>
@github-actions

Copy link
Copy Markdown
Contributor

Code Coverage

Package Line Rate Health
test 0% ❌
windows 27% ❌
parts_windows Package 1 74% ➖
staging_cse_windows Package 1 72% ➖
debug 0% ❌
provisioningscripts 2% ❌
Summary 40% (2077 / 7284) ❌

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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

@github-actions

Copy link
Copy Markdown
Contributor

Code Coverage

Package Line Rate Health
shellspec spec 30% ❌
Summary 30% (5112 / 16917) ❌

@github-actions

Copy link
Copy Markdown
Contributor

Code Coverage

Package Line Rate Health
github.com/Azure/agentbaker/aks-live-patching/pkg/status 100% ✔
github.com/Azure/agentbaker/apiserver 52% ❌
github.com/Azure/agentbaker/cmd 0% ❌
github.com/Azure/agentbaker/cmd/starter 0% ❌
github.com/Azure/agentbaker/fuzz/api 0% ❌
github.com/Azure/agentbaker/hotfix/render-nodecustomdata 35% ❌
github.com/Azure/agentbaker/pkg/agent 79% ➖
github.com/Azure/agentbaker/pkg/agent/datamodel 78% ➖
github.com/Azure/agentbaker/pkg/agent/toggles 0% ❌
github.com/Azure/agentbaker/pkg/vhdbuilder/datamodel 87% ✔
github.com/Azure/agentbaker/aks-node-controller 73% ➖
github.com/Azure/agentbaker/aks-node-controller/common 100% ✔
github.com/Azure/agentbaker/aks-node-controller/helpers 68% ➖
github.com/Azure/agentbaker/aks-node-controller/parser 88% ✔
github.com/Azure/agentbaker/aks-node-controller/pkg/gpu 100% ✔
github.com/Azure/agentbaker/aks-node-controller/pkg/nodeconfigutils 91% ✔
github.com/Azure/agentbaker/aks-node-controller/utils 0% ❌
Summary 84% (6297 / 8227) ✔

@r2k1
Artur Khantimirov (r2k1) merged commit 17fb06c into dev Nov 25, 2026
53 of 55 checks passed
@r2k1
Artur Khantimirov (r2k1) deleted the akhantimirov/absvc-json-logging branch November 25, 2026 20:52

This branch was successfully deployed

1 active deployment
test — 215eb435 Deployed Nov 25, 2026 by r2k1 via auto-tidy #864
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants