-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(collector): have Registry:encode do the encoding #149
Merged
Conversation
This file contains 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
Instead of having `Registry` provide access to its inner `Metric`s, have `Registry` encode the `Metric`s directly via `Registry::encode`. In the same vain, have `Collector::encode` encode the collected `Metric`s directly. This allows for a much simpler `Collector` trait, removing all complexity due to providing access to a `Metric` externally. ``` rust /// The [`Collector`] abstraction allows users to provide additional metrics and /// their description on each scrape. /// /// An example use-case is an exporter that retrieves a set of operating system metrics /// ad-hoc on each scrape. /// /// Register a [`Collector`] with a [`Registry`](crate::registry::Registry) via /// [`Registry::register_collector`](crate::registry::Registry::register_collector). /// /// ``` /// # use prometheus_client::metrics::counter::ConstCounter; /// # use prometheus_client::collector::Collector; /// # use prometheus_client::encoding::{DescriptorEncoder, EncodeMetric}; /// # /// #[derive(Debug)] /// struct MyCollector {} /// /// impl Collector for MyCollector { /// fn encode<'a>(&'a self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> { /// let counter = ConstCounter::new(42); /// let metric_encoder = encoder.encode_descriptor( /// "my_counter", /// "some help", /// None, /// counter.metric_type(), /// )?; /// counter.encode(metric_encoder)?; /// Ok(()) /// } /// } /// ``` pub trait Collector: std::fmt::Debug + Send + Sync + 'static { /// Once the [`Collector`] is registered, this method is called on each scrape. /// /// Note that the return type allows you to either return owned (convenient) /// or borrowed (performant) descriptions and metrics. fn encode<'a>(&'a self, encoder: DescriptorEncoder) -> Result<(), std::fmt::Error>; } ```
@thomaseizinger based on your comments in #82 (comment) I think you will like this change. |
mxinden
added a commit
to mxinden/rust-libp2p
that referenced
this pull request
Jul 8, 2023
Updates to `prometheus-client` `Collector` trait refactor introduced with prometheus/client_rust#149.
4 tasks
I think I was able to simplify this a bit in mxinden#2. |
Closed
mergify bot
pushed a commit
to libp2p/rust-libp2p
that referenced
this pull request
Oct 25, 2023
Updates to `prometheus-client` `Collector` trait refactor introduced with prometheus/client_rust#149. Pull-Request: #4160.
1 task
@mxinden looks like there is no way to add custom labels with the new API. Probably we should add an additional encode_descriptor function with optional labels. WDYT? |
Does the |
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.
Let's face it, the
Collector
trait I designed in #82 is too complex.Instead of having
Registry
provide access to its innerMetric
s, haveRegistry
encode theMetric
s directly viaRegistry::encode
. In the samevain, have
Collector::encode
encode the collectedMetric
s directly.This allows for a much simpler
Collector
trait, removing all complexity due toproviding access to a
Metric
externally.