Repository navigation
feat(grpc): choose which metadata keys bind the request context - #118
Merged
Merged
Conversation
AlexeyShalaev
force-pushed
the
feat/grpc-context-metadata
branch
from
November 13, 2026 08:46
06f205a to
6bf2f4f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UnitScopeInterceptorbuilds the RPC context fromget_correlation_ids, which always readx-request-id,x-user-id,x-tenant-idandx-trace-idfrom the incoming metadata. The mapping was a module constant, and neither the interceptor norGrpcConfigcould change it. So a client that reaches the gRPC port chose theuser_idandtenant_idin every log line, in OTel baggage, and inpropagation_metadata(), which forwards them to downstream services.This mirrors #115:
GrpcConfig.context_metadata: dict[str, str] | None = None, as{metadata_key: context_key}.Nonereads 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), andGrpcEntrypointpassesconfig.context_metadatato it.get_correlation_ids(context, keys=None).Noneuses the existing constant, so the defaults still live in one place, and single-argument calls behave as before. Mapping keys match case-insensitively, likeheader.lower()on the HTTP side, and values go through the sameis_safe_context_idfilter.A service exposed to clients sets
GrpcConfig(context_metadata={"x-request-id": "request_id"}).Things worth a look:
x-user-idfrom their gateway would silently loseuser_id.BaseGrpcServerSettings, socontext_metadatais afrom_settingskeyword likehealth_service_names, and the test that checks everyGrpcConfigfield is either read from settings or listed with a reason now lists it. The gRPC page shows the route: subclass the kit model with acontext_metadatafield and pass it through, which givesGRPC__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._CORRELATION_METADATA_KEYSis a dict now instead of a tuple of pairs, and_build_contextis an instance method instead of a staticmethod.Docs: a "Which metadata keys are read" section next to the metadata table on the gRPC page, plus the new row in the
GrpcConfigandfrom_settingskeyword 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
Checklist
make checkpasses locally (ruff+mypy)CHANGELOG.mdleft alone: Release Please generates it from the Conventional Commit subjects (and from the PR title on a squash merge)docs/agents.mdincludedTests: with
context_metadata={"x-request-id": "request_id"}and an RPC carryingx-user-id,x-tenant-idandx-trace-id, the context store holds onlygrpc_methodandrequest_id, andpropagation_metadata()inside the handler returns onlyx-request-id. The same tests run withNoneas the control and see all four.{}binds nothing from the client and mints a UUID request id. A unit test drives the interceptor thatGrpcEntrypoint.bindactually built, so the wiring from the config is covered, and an integration test sends real metadata to an in-process gRPC server for bothNoneand the request-id-only mapping.get_correlation_idsis tested withNone, a mapping, a mixed-case key,{}and a log-unsafe value;from_settingswith the new keyword. Every new case fails onfeat/context-headers. I also checked two mutations: ignoringkeysinget_correlation_idsfails 8 of them, and dropping the argument inGrpcEntrypointfails the wiring and integration tests.Related issues
Closes #112