-
Notifications
You must be signed in to change notification settings - Fork 80
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
src/collector: Introduce Collector abstraction #82
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f0aa084
encoding/: Adopt serde style encoding
mxinden 4b2d00f
src/encoding: Generate EncodeLabelValue for integers
mxinden d11c977
src/encoding: Take reference for value
mxinden c337017
src/collector: Introduce Collector abstraction
mxinden c9f59eb
src/metrics: Introduce const types
mxinden 723f86f
src/collector: Don't require Send and Sync
mxinden 83a2cb6
src/metrics/family: Implement EncodeMetric for RefCell Iterator
mxinden 5f70f0c
Merge remote-tracking branch 'prometheus/master' into collector
mxinden d90c797
*: Bump version and add changelog entry
mxinden 50ce9c9
Rename collector::Metric to LocalMetric similar to futures box_local
mxinden 3b3aef2
Merge branch 'master' of https://github.com/prometheus/client_rust in…
mxinden 5032236
Fix intra doc links
mxinden 66f2982
Fix clippy warnings
mxinden ff36682
Fix collector doc test
mxinden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//! Metric collector implementation. | ||
//! | ||
//! See [`Collector`] for details. | ||
|
||
use std::borrow::Cow; | ||
|
||
use crate::{ | ||
registry::{Descriptor, LocalMetric}, | ||
MaybeOwned, | ||
}; | ||
|
||
/// 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). | ||
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. | ||
#[allow(clippy::type_complexity)] | ||
fn collect<'a>( | ||
&'a self, | ||
) -> Box<dyn Iterator<Item = (Cow<'a, Descriptor>, MaybeOwned<'a, Box<dyn LocalMetric>>)> + 'a>; | ||
} |
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very hard to discover and doesn't feel Rust-y to me. Why do we need to depend on
RefCell
here?I think it would be more idiomatic to do:
impl EncodeMetric for Vec<T> where T: EncodeMetric { }
impl EncodeMetric for (S, M) where S: EncodeLabelSet, M: EncodeMetric { }
Then, have the user provide a
Vec
instead of being generic over something that can be iterated. That should get rid of theRefCell
and iterator dependency and would compose better with other usecases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An
Iterator::next
takes&mut self
in order to be able to e.g. track its position within aVec
. UnfortunatelyEncodeMetric::encode
takes&self
.In this pull request
RefCell
is used for interior mutability. It allows calling anIterator::next
taking&mut self
from anEncodeMetric::encode
providing&self
only.An alternative approach would be to change
EncodeMetric::encode
to take&mut self
. I deemed the change and its implications not worth it, given thatCollector
is an abstraction for advanced users only.All that said, I agree that the usage of
RefCell
is neither simple nor intuitive. @thomaseizinger do you see other alternatives to the one above?