Skip to content

Generate request IDs when none is set - #182

Open
tire-fire wants to merge 2 commits into
dbaseqp:mainfrom
tire-fire:fix/request-id
Open

tire-fire wants to merge 2 commits into
dbaseqp:mainfrom
tire-fire:fix/request-id

Conversation

@tire-fire

@tire-fire tire-fire commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Logging is meant to give every request an ID, which handlers then log through WriteInternalError and the other slog.Error calls. It never does: r.Context().Value("request_id") returns a nil any when the key is unset, and nil == "" is false, so the ID is never generated. Every request_id field in the logs is nil.

Changes:

  • The request ID is stored under a typed context key, through api.WithRequestID and api.RequestID, next to the existing identity key. RequestID always returns a string, "" when unset, so the nil comparison that caused this bug can't happen again. This also resolves the staticcheck TODO in Logging and removes its nolint comment.
  • IDs are 8 characters from crypto/rand.Text() instead of a 36-character UUID, so log lines stay short. That's enough to tell apart requests that are near each other in the logs.
  • An ID that is already set is kept, as before.

Added TestLoggingSetsRequestID. It checks that the handler sees an 8-character ID, that each request gets a different one, and that an existing ID is kept. It fails on the current code.

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