Repository navigation
feat: custom labels in prometheus metrics - #2555
Merged
Merged
Conversation
roma-frolov
commented
Oct 7, 2025
| app_name: str = EMPTY, | ||
| metrics_prefix: str = "faststream", | ||
| received_messages_size_buckets: Sequence[float] | None = None, | ||
| custom_labels: dict[str, str | Callable[[Any], str]] | None = None, |
Collaborator
Author
There was a problem hiding this comment.
@draincoder hi!
A similar feature in litestart allows you to pass a Callable that accepts a Request object. Which object is best to pass to FS? AnyMsg?
Lancetnik
reviewed
Oct 7, 2025
| self._metrics_manager = metrics_manager | ||
| self._settings_provider = settings_provider_factory(msg) | ||
| self._custom_labels = { | ||
| k: str(v(msg) if callable(v) else v) for k, v in (custom_labels or {}).items() |
Member
There was a problem hiding this comment.
I think, we can split them in the static init method and cast here without runtime filtering
Collaborator
Author
There was a problem hiding this comment.
Did I understand you correctly, you mean this?:
def __init__(
self,
msg: AnyMsg | None,
/,
*,
...,
static_custom_labels: dict[str, str] | None,
dynamic_custom_labels: dict[str, Callable[[Any], str]] | None,
) -> None:
...
self._custom_labels = {
**(static_custom_labels or {}),
**{k: str(v(msg)) for k, v in (dynamic_custom_labels or {}).items()},
}
...
Member
There was a problem hiding this comment.
Nope, I mean
class MiddlewareBuilder:
def __init__(self, custom_labels):
self.static_labels = {}
self.dynamic_labels = {}
for k, v in custom_labels.items():
if callable(v):
self.dynamic_labesl[k] = v
else:
self.static_labesl[k] = v
def __call__(self, msg):
return RealMiddleware(msg, custom_labels = self.static_labels | {
k: v(msg) for k, v in self.dynamic_labels.items()
})
roma-frolov
marked this pull request as ready for review
October 17, 2025 19:23
Lancetnik
approved these changes
Oct 18, 2025
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.
Description
Fixes #2516
Type of change
Checklist
just lintshows no errors)just test-coveragejust static-analysis