Repository navigation
fix: instrument every FastStream broker, with per-type middleware classes - #299
Merged
Merged
Conversation
This was referenced Oct 24, 2026
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 #295.
The FastStream OpenTelemetry, Prometheus and logging instruments used
application.broker, which FastStream defines asbrokers[0]. Every other broker ran without spans or metrics and kept FastStream's default logger.What changes
The logging instrument now replaces the logger params storage on every broker. Teardown restores each broker's own previous storage.
The OpenTelemetry and Prometheus instruments now add middleware to every broker. FastStream's middleware classes each handle one broker type:
KafkaTelemetryMiddlewarewires in a settings provider that readsmsg.raw_message.topic, and a Redis raw message is a dict. So one class can't simply go on every broker. Two rules cover this:opentelemetry_middleware_clsandprometheus_middleware_clsalso accept a mapping from broker class to middleware class. Lookup walks the broker's MRO, so the most specific entry wins.A broker with no matching class is left uninstrumented, and
bootstrap()warns throughwarn_at_caller, naming the broker types and the config field.All brokers share the instrument's single Prometheus registry. FastStream's
MetricsContainerreuses a metric that already exists in the registry, and every metric has abrokerlabel, so brokers of different types write separate series under one name. OpenTelemetry returns the existing instrument for an identical name, unit and description, so per-broker middleware instances don't conflict there either.Brokers added after
bootstrap()are not instrumented. The FastStream guide now says to add brokers first. A startup hook would only catch brokers added by hooks registered before it, so documenting this is more honest than a partial fix.Tests
Written first and failing before the change:
The mixed-type tests use a
RedisBrokersubclass as the second type, since the dev dependencies don't include Kafka. I also ran the new docs example on a realKafkaBrokerplusRedisBrokerwithfaststream[kafka]==0.7.8, and each broker got its own Kafka or Redis middleware pair.brokersexists from FastStream 0.6, so thefaststream>=0.6floor stays.