Repository navigation
feat(fastapi): pass instrumentations through to Instrumentator.add() - #29
Merged
Merged
Conversation
MetricsInstrumentatorConfig covered three of the four calls the wrapper makes; .add() was unreachable, so the library's own per-request callables (request_size, response_size, custom ones) could not be installed through servicewright. The new `instrumentations` field is applied between construction and instrument(). It replaces the default set (the library's semantics), which the docs now spell out together with the default() name clash. Closes #28
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 #28
MetricsInstrumentatorConfigfed three of the four calls the wrapper makes (Instrumentator(...),.instrument(),.expose());.add()had no field, and the instance is discarded insidesetup_metrics_instrumentator, so the library's own per-request callables —metrics.request_size(),response_size(), anything custom — could not be installed through servicewright.Design. A fourth pass-through,
instrumentations: Sequence[Any] = (), applied between construction and.instrument():Instrumentator(**init_kwargs).add(*cfg.instrumentations). TypedAnylike the other three fields, soconfig.pystill imports nothing from the extra and the reporter's snippet type-checks as is (request_size()is typedOptional[Callable]).Named
instrumentationsrather than the proposedextra_metrics: in prometheus-fastapi-instrumentator 8.x the middleware doesinstrumentations or [default()], so anything passed replaces the default set rather than extending it — "extra" would promise the wrong thing, andinstrumentationsis the library's own name for the list. The docs say so and spell out the trap that follows:default()already registershttp_request_size_bytes/http_response_size_bytes(summaries by handler), so[default(), request_size()]hits a duplicated time series, the library returnsNone, and.add()skips it silently. The documented composition is[requests(), latency(), request_size(), response_size()].Rejected. Returning the
Instrumentator— whether.add()after.instrument()takes effect depends on the middleware holding the list by reference, and it changes a public return type. Re-addingdefault()on the consumer's behalf — it takes the namespace/subsystem/buckets the middleware derives from the init kwargs, so we would be re-implementing that mapping. LettingFastApiEntrypoint(metrics=...)take the config directly — real gap, separate change.Tests. The reporter's scenario:
request_size()+response_size()through the config, one request, both_countseries with handler/method/status labels on/system/metrics; and a guard that an empty field keeps the default set. Both run on a privateCollectorRegistrywith the in-progress gauge off — the library registers that gauge on the globalREGISTRYunconditionally, so a second instrumented app in one process collides on it.Verified.
make check— ruff, mypy, import-linter: greenmake test— 705 passed, 14 skipped, coverage 92.78% (baseline before the change: 703 passed, 92.78%)metrics.pyfrom master,…instrumentations_configured__emit_their_seriesfails (the field is accepted but ignored) and the guard passeszensical build --clean: no issues