Skip to content

feat(fastapi): pass instrumentations through to Instrumentator.add() - #29

Merged
AlexeyShalaev merged 1 commit into
masterfrom
feat/metrics-instrumentations
Sep 5, 2026
Merged

AlexeyShalaev merged 1 commit into
masterfrom
feat/metrics-instrumentations

Conversation

@AlexeyShalaev

Copy link
Copy Markdown
Member

Closes #28

MetricsInstrumentatorConfig fed three of the four calls the wrapper makes (Instrumentator(...), .instrument(), .expose()); .add() had no field, and the instance is discarded inside setup_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). Typed Any like the other three fields, so config.py still imports nothing from the extra and the reporter's snippet type-checks as is (request_size() is typed Optional[Callable]).

Named instrumentations rather than the proposed extra_metrics: in prometheus-fastapi-instrumentator 8.x the middleware does instrumentations or [default()], so anything passed replaces the default set rather than extending it — "extra" would promise the wrong thing, and instrumentations is the library's own name for the list. The docs say so and spell out the trap that follows: default() already registers http_request_size_bytes / http_response_size_bytes (summaries by handler), so [default(), request_size()] hits a duplicated time series, the library returns None, 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-adding default() 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. Letting FastApiEntrypoint(metrics=...) take the config directly — real gap, separate change.

Tests. The reporter's scenario: request_size() + response_size() through the config, one request, both _count series with handler/method/status labels on /system/metrics; and a guard that an empty field keeps the default set. Both run on a private CollectorRegistry with the in-progress gauge off — the library registers that gauge on the global REGISTRY unconditionally, so a second instrumented app in one process collides on it.

Verified.

  • make check — ruff, mypy, import-linter: green
  • make test — 705 passed, 14 skipped, coverage 92.78% (baseline before the change: 703 passed, 92.78%)
  • negative control — with metrics.py from master, …instrumentations_configured__emit_their_series fails (the field is accepted but ignored) and the guard passes
  • docs — zensical build --clean: no issues

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
@AlexeyShalaev
AlexeyShalaev merged commit 1b48f25 into master Sep 5, 2026
6 checks passed
@AlexeyShalaev
AlexeyShalaev deleted the feat/metrics-instrumentations branch September 5, 2026 21:05
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.

No seam for Instrumentator.add(): the library's own extra metrics are unreachable

1 participant