Repository navigation
Description
Noticed while reviewing #226. Same shape, but not the same fix: deleting the override here would be wrong.
The gate
@classmethod
def is_configured(cls, bootstrap_config: "FastStreamConfig") -> bool: # ty: ignore[invalid-method-override]
return super().is_configured(bootstrap_config) and bool(bootstrap_config.prometheus_middleware_cls)while bootstrap() mounts the endpoint unconditionally and only the middleware is guarded:
def bootstrap(self) -> None:
config = self.bootstrap_config
config.application.mount(
config.prometheus_metrics_path, prometheus_client.make_asgi_app(self.collector_registry)
)
if config.prometheus_middleware_cls and config.application.broker:
config.application.broker.add_middleware(config.prometheus_middleware_cls(registry=self.collector_registry))So without prometheus_middleware_cls the instrument is never instantiated and the /metrics mount
never happens, silently (_select_instruments records config-level skips without warning, by design).
The case that is actually lost
FastStreamConfig accepts prometheus_collector_registry, and __post_init__ uses an injected
registry in preference to a fresh one. A service that injects a registry holding its own collectors
and does not use the broker middleware gets no endpoint to scrape them from. The registry is accepted
and does nothing, which is the #226 complaint in a different instrument.
Why the #226 fix does not transfer
PrometheusConfig.prometheus_metrics_path defaults to "/metrics", and the base gate is
bool(path) and is_valid_path(path). That is true by default. The prometheus_middleware_cls
override is therefore the only thing stopping every FastStream application from mounting a
/metrics endpoint. Deleting it the way #226 deleted the OpenTelemetry override would change the
HTTP surface of every FastStream service that never asked for Prometheus.
The OpenTelemetry case had no such problem: its base gate is
bool(opentelemetry_endpoint or opentelemetry_log_traces), both defaulting to off, so the user had
to opt in before anything happened.
Options, none obviously right
- Gate on the registry instead: configured when
prometheus_middleware_clsor
prometheus_collector_registryis set. Narrow, fixes exactly the lost case, leaves default apps
untouched. Slightly odd as a rule to explain. - Make the path opt-in on FastStream by defaulting
prometheus_metrics_pathto""there, then
delete the override for real symmetry with FastStream skips the whole OpenTelemetry instrument whenopentelemetry_middleware_clsis unset #226. Cleanest rule, but a breaking change for anyone
relying on the default path today. - Leave it and document it, on the grounds that FastStream Prometheus support means the broker
middleware and nothing else. Thenprometheus_collector_registryshould say so.
Worth deciding rather than defaulting, which is why this is filed rather than fixed.
Activity
fix: mount FastStream metrics for an injected collector registry (#238)