Skip to content

feat(grpc): choose which metadata keys bind the request context - #118

Merged
AlexeyShalaev merged 1 commit into
masterfrom
feat/grpc-context-metadata
Nov 13, 2026
Merged

AlexeyShalaev merged 1 commit into
masterfrom
feat/grpc-context-metadata

Conversation

@AlexeyShalaev

@AlexeyShalaev AlexeyShalaev commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Summary

UnitScopeInterceptor builds the RPC context from get_correlation_ids, which always read x-request-id, x-user-id, x-tenant-id and x-trace-id from the incoming metadata. The mapping was a module constant, and neither the interceptor nor GrpcConfig could change it. So a client that reaches the gRPC port chose the user_id and tenant_id in every log line, in OTel baggage, and in propagation_metadata(), which forwards them to downstream services.

This mirrors #115:

  • GrpcConfig.context_metadata: dict[str, str] | None = None, as {metadata_key: context_key}. None reads the four keys as today. {} reads none, and the request id is then always minted.
  • GrpcConfig.from_settings(..., context_metadata=None) passes it through.
  • UnitScopeInterceptor(container, *, context_setters=(), context_metadata=None), and GrpcEntrypoint passes config.context_metadata to it.
  • get_correlation_ids(context, keys=None). None uses the existing constant, so the defaults still live in one place, and single-argument calls behave as before. Mapping keys match case-insensitively, like header.lower() on the HTTP side, and values go through the same is_safe_context_id filter.

A service exposed to clients sets GrpcConfig(context_metadata={"x-request-id": "request_id"}).

Things worth a look:

  • The default is unchanged, for the same reason as feat(fastapi): choose which request headers ContextMiddleware reads #115: services that get x-user-id from their gateway would silently lose user_id.
  • There is no environment variable out of the box. The settings model is grpc-server-kit's BaseGrpcServerSettings, so context_metadata is a from_settings keyword like health_service_names, and the test that checks every GrpcConfig field is either read from settings or listed with a reason now lists it. The gRPC page shows the route: subclass the kit model with a context_metadata field and pass it through, which gives GRPC__CONTEXT_METADATA='{"x-request-id":"request_id"}'. I ran that snippet against the env var with a mapping, with {} and unset. If we want the variable without a subclass, the field belongs in grpc-server-kit.
  • Private changes: _CORRELATION_METADATA_KEYS is a dict now instead of a tuple of pairs, and _build_context is an instance method instead of a staticmethod.
  • The idempotency key, client ip and user agent extraction is untouched.

Docs: a "Which metadata keys are read" section next to the metadata table on the gRPC page, plus the new row in the GrpcConfig and from_settings keyword tables. On the request context page, the sentence from #115 saying gRPC has no such setting is replaced with the gRPC route. Rule 21 on the agents page and the Security item in the production checklist now cover gRPC too.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Refactoring / internal

Checklist

  • Tests added or updated
  • make check passes locally (ruff + mypy)
  • CHANGELOG.md left alone: Release Please generates it from the Conventional Commit subjects (and from the PR title on a squash merge)
  • Documentation updated (if the public API changed), docs/agents.md included

Tests: with context_metadata={"x-request-id": "request_id"} and an RPC carrying x-user-id, x-tenant-id and x-trace-id, the context store holds only grpc_method and request_id, and propagation_metadata() inside the handler returns only x-request-id. The same tests run with None as the control and see all four. {} binds nothing from the client and mints a UUID request id. A unit test drives the interceptor that GrpcEntrypoint.bind actually built, so the wiring from the config is covered, and an integration test sends real metadata to an in-process gRPC server for both None and the request-id-only mapping. get_correlation_ids is tested with None, a mapping, a mixed-case key, {} and a log-unsafe value; from_settings with the new keyword. Every new case fails on feat/context-headers. I also checked two mutations: ignoring keys in get_correlation_ids fails 8 of them, and dropping the argument in GrpcEntrypoint fails the wiring and integration tests.

Related issues

Closes #112

@AlexeyShalaev
AlexeyShalaev force-pushed the feat/grpc-context-metadata branch from 06f205a to 6bf2f4f Compare November 13, 2026 08:46
@AlexeyShalaev
AlexeyShalaev merged commit 2948c13 into master Nov 13, 2026
7 checks passed
@AlexeyShalaev
AlexeyShalaev deleted the feat/grpc-context-metadata branch November 13, 2026 08:48
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.

gRPC: choose which metadata keys bind the request context; client-sent x-user-id/x-tenant-id are trusted and propagated

1 participant