Skip to content

Commit

Permalink
feat(encoding)!: Collector does not consume DescriptorEncoder
Browse files Browse the repository at this point in the history
see prometheus#135.

this adjusts the parameter of `encode()` so that it only mutably borrows
the encoder.

Signed-off-by: katelyn martin <[email protected]>
  • Loading branch information
cratelyn committed Nov 18, 2024
1 parent 9b7317d commit c256fd2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ use crate::encoding::DescriptorEncoder;
/// struct MyCollector {}
///
/// impl Collector for MyCollector {
/// fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
/// fn encode(&self, encoder: &mut DescriptorEncoder) -> Result<(), std::fmt::Error> {
/// let counter = ConstCounter::new(42u64);
/// let metric_encoder = encoder.encode_descriptor(
/// let mut metric_encoder = encoder.encode_descriptor(
/// "my_counter",
/// "some help",
/// None,
/// counter.metric_type(),
/// )?;
/// counter.encode(metric_encoder)?;
/// counter.encode(&mut metric_encoder)?;
/// Ok(())
/// }
/// }
/// ```
pub trait Collector: std::fmt::Debug + Send + Sync + 'static {
/// Once the [`Collector`] is registered, this method is called on each scrape.
fn encode(&self, encoder: DescriptorEncoder) -> Result<(), std::fmt::Error>;
fn encode(&self, encoder: &mut DescriptorEncoder) -> Result<(), std::fmt::Error>;
}
2 changes: 1 addition & 1 deletion src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ mod tests {
impl crate::collector::Collector for Collector {
fn encode(
&self,
mut encoder: crate::encoding::DescriptorEncoder,
encoder: &mut crate::encoding::DescriptorEncoder,
) -> Result<(), std::fmt::Error> {
let counter = crate::metrics::counter::ConstCounter::new(42u64);
let mut metric_encoder = encoder.encode_descriptor(
Expand Down
10 changes: 5 additions & 5 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ impl Registry {
/// struct MyCollector {}
///
/// impl Collector for MyCollector {
/// fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> {
/// fn encode(&self, encoder: &mut DescriptorEncoder) -> Result<(), std::fmt::Error> {
/// let counter = ConstCounter::new(42u64);
/// let metric_encoder = encoder.encode_descriptor(
/// let mut metric_encoder = encoder.encode_descriptor(
/// "my_counter",
/// "some help",
/// None,
/// counter.metric_type(),
/// )?;
/// counter.encode(metric_encoder)?;
/// counter.encode(&mut metric_encoder)?;
/// Ok(())
/// }
/// }
Expand Down Expand Up @@ -300,9 +300,9 @@ impl Registry {
}

for collector in self.collectors.iter() {
let descriptor_encoder =
let mut descriptor_encoder =
encoder.with_prefix_and_labels(self.prefix.as_ref(), &self.labels);
collector.encode(descriptor_encoder)?;
collector.encode(&mut descriptor_encoder)?;
}

for registry in self.sub_registries.iter() {
Expand Down

0 comments on commit c256fd2

Please sign in to comment.