Skip to content

fix: write each context value once when a Handler wraps a Handler - #101

Open
iainlane wants to merge 1 commit into
chainguard-dev:mainfrom
iainlane:fix-double-context-handler
Open

iainlane wants to merge 1 commit into
chainguard-dev:mainfrom
iainlane:fix-double-context-handler

Conversation

@iainlane

@iainlane iainlane commented Nov 12, 2026 •

Copy link
Copy Markdown

Why

clog adds the values from WithValues to a record in Handler.Handle. FromContext falls back to NewLogger(nil) when the context has no logger, and NewLogger(nil) wraps slog.Default().Handler() in a Handler.

That works when the default handler is a plain slog handler, as clog/gcp/init installs. A program can also make a Handler the default, so that plain slog.InfoContext calls get the context's values as well:

slog.SetDefault(slog.New(clog.NewHandler(gcp.NewHandler(level))))

Its clog calls then go through two Handlers, and each adds every context value to the record. The JSON handler writes each key twice:

{"level":"INFO","msg":"","a":"b","a":"b"}

This is more than noise. Cloud Logging doesn't keep one of the two values: it concatenates them. A service that adds component=api with WithValues logs "component": "apiapi", and queries and alerts on that field stop matching.

The fallback is not the only way to nest two Handlers. A Handler with a nil inner handler, as clog.New(nil) builds, looks up the default handler on every call. WithAttrs and WithGroup build a new Handler around the handler returned by the inner handler's method, and that handler is a Handler when the inner handler is one. Whether two Handlers are nested is therefore known only when a method runs, not when the Handler is constructed.

What this changes

Handler skips any Handler (or *Handler) directly inside it when it looks up its inner handler. Every method uses that lookup, so when Handlers are nested directly, only the outermost one adds the context values. A program can make a Handler its default without duplicating values, and its plain slog calls keep the context's values.

Two Handlers separated by a different handler still each add the values, because slog has no way to ask a handler for the handler that it wraps. Handler's documentation now says to use one Handler, as the outermost handler.

Testing

TestContextHandlerWrappedTwice checks the line written by each of six calls:

  • a Handler around a Handler;
  • a clog call with a Handler as the default;
  • a clog call with a *Handler as the default;
  • a plain slog call with a Handler as the default;
  • clog.New(nil) with a Handler as the default;
  • clog.New(nil).With("x", "y") with a Handler as the default.

All but the plain slog call fail without this change, with "a":"b" written twice. The plain slog call passes either way, and checks that plain slog calls keep the context's values.

The test's cleanup also restores the log package's output and flags. slog.SetDefault redirects the log package to the new handler, and restoring the previous default does not undo that, so later tests in the package would otherwise write their default-logger output to this test's buffer.

go vet ./... and go test ./... pass.

@iainlane
iainlane force-pushed the fix-double-context-handler branch from 51cb594 to a789b97 Compare November 12, 2026 12:46
When a context has no logger, `FromContext` falls back to
`NewLogger(nil)`, which wraps `slog.Default().Handler()` in a
`Handler`. A program can install a `Handler` as the default so that
plain `slog.InfoContext` calls get the context's values too. Its `clog`
calls then log through two `Handler`s, and each one adds every value
from `WithValues` to the record. The JSON handler writes each key
twice:

    {"level":"INFO","msg":"","a":"b","a":"b"}

Cloud Logging does not keep one of the two values. It concatenates
them: a service that adds `component=api` with `WithValues` logs
`"component": "apiapi"`, and queries and alerts on that field stop
matching.

The fallback is not the only way to nest two `Handler`s. A `Handler`
with a nil inner handler looks up the default handler on every call.
`WithAttrs` and `WithGroup` build a new `Handler` around the handler
returned by the inner handler's method, and that handler is a `Handler`
when the inner handler is one. Whether two `Handler`s are nested is
therefore known only when a method runs, not when the `Handler` is
constructed.

`Handler` now skips any `Handler` directly inside it when it looks up
its inner handler. Every method uses that lookup, so when `Handler`s
are nested directly, only the outermost one adds the values. A program
can make a `Handler` the default without duplicating its values.

Two `Handler`s separated by a different handler still each add the
values, because slog has no way to ask a handler for the handler that
it wraps. `Handler`'s documentation now says to use one `Handler`, as
the outermost handler.
@iainlane
iainlane force-pushed the fix-double-context-handler branch from a789b97 to 2edd9bf Compare November 12, 2026 13:15
@iainlane
iainlane marked this pull request as ready for review November 12, 2026 13:19
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.

1 participant