feat(hosting): expose correlation_id on request.state + structlog recipe (#164) - #167
Merged
Merged
Conversation
…ipe (#164) CorrelationIdMiddleware already wrote a ContextVar and X-Correlation-ID header; docs claimed it also exposed request.state.correlation_id but the code never did. Write straight into scope["state"] so Request(scope).state.correlation_id is available to handlers without a Request allocation in the middleware. Fix the middleware docs (header was wrong — said X-Request-ID) and add a 3-line structlog wire-up recipe for smpy_gis T4 observability. Verified end-to-end that the recipe makes log lines carry correlation_id; documented why structlog's merge_contextvars cannot be used as a drop-in substitute (it only sees structlog_-prefixed contextvars, not stdlib ones).
Deploying simple-module-python with
|
| Latest commit: |
5decbec
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://8ee2fcdb.simple-module-python.pages.dev |
| Branch Preview URL: | https://worktree-issue-164-request-i.simple-module-python.pages.dev |
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.
Closes #164.
Summary
CorrelationIdMiddlewarenow also writes the ID torequest.state.correlation_id(the docs already claimed it did — making it true). Written by directscope["state"]assignment so the hot path doesn't allocate aRequestper call.docs/framework/middleware.mdcorrected: the header isX-Correlation-ID, notX-Request-ID, and the existingCorrelationIdMiddlewaresatisfies the intent of simple_module_hosting: ship request_id middleware that propagates into log records #164 (smpy_gis T4 will adoptcorrelation_idinstead of inventing a parallelrequest_id).structlog.contextvars.merge_contextvarsis not a drop-in substitute (it only seesstructlog_-prefixed contextvars).test_state_contextvar_and_header_agreespins up a minimal Starlette app with the middleware and asserts 3-way parity betweenrequest.state.correlation_id, thecorrelation_idContextVar, and theX-Correlation-IDresponse header.Verification
/vfStage 2a, see.verify/smoke.txt&.verify/structlog_recipe.txt):GET /health(no header) → 32-char UUID4 hex generated and echoedGET /healthwithX-Correlation-ID: vf-smoke-trace-001→ echoed verbatimGET /healths → distinct IDs (per-request)GET /dashboard(Inertia HTML path) → 302 withX-Correlation-IDset, proving the middleware fires across the whole pipeline"correlation_id": "vf-recipe-7"test_toggle_host_multi_tenant_persists) is unrelated to hosting-middleware behavior and would only verify "did I break the request pipeline", which the unit suite + live smoke already covered.Test plan
curl -i http://localhost:8000/healthaftermake devand confirms anX-Correlation-IDheader is present in the response.docs/framework/middleware.mdinto a scratch script and confirms log events carrycorrelation_id.