From 53f15ec2f9e2d5cf7df548bdae362c7c98aaf024 Mon Sep 17 00:00:00 2001 From: flaneur Date: Mon, 29 Jul 2024 15:00:29 +0800 Subject: [PATCH] feat: allow Arc on EncodeLabelSet (#217) Signed-off-by: Li Yazhou --- CHANGELOG.md | 4 ++++ derive-encode/tests/lib.rs | 32 ++++++++++++++++++++++++++++++++ src/encoding.rs | 7 +++++++ 3 files changed, 43 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3930fccb..32de9b95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,8 +21,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Support `i32`/`f32` for `Gauge` and `u32`/`f32` for `Counter`/`CounterWithExemplar`. See [PR 173] and [PR 216]. +- Supoort `Arc` for `EncodeLabelValue`. + See [PR 217]. + [PR 173]: https://github.com/prometheus/client_rust/pull/173 [PR 216]: https://github.com/prometheus/client_rust/pull/216 +[PR 217]: https://github.com/prometheus/client_rust/pull/217 ## [0.22.3] diff --git a/derive-encode/tests/lib.rs b/derive-encode/tests/lib.rs index fba8412d..5c7eb54d 100644 --- a/derive-encode/tests/lib.rs +++ b/derive-encode/tests/lib.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use prometheus_client::encoding::text::encode; use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue}; use prometheus_client::metrics::counter::Counter; @@ -137,6 +139,36 @@ fn remap_keyword_identifiers() { assert_eq!(expected, buffer); } +#[test] +fn arc_string() { + #[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)] + struct Labels { + client_id: Arc, + } + + let mut registry = Registry::default(); + let family = Family::::default(); + registry.register("my_counter", "This is my counter", family.clone()); + + // Record a single HTTP GET request. + let client_id = Arc::new("client_id".to_string()); + family + .get_or_create(&Labels { + client_id: client_id.clone(), + }) + .inc(); + + // Encode all metrics in the registry in the text format. + let mut buffer = String::new(); + encode(&mut buffer, ®istry).unwrap(); + + let expected = "# HELP my_counter This is my counter.\n".to_owned() + + "# TYPE my_counter counter\n" + + "my_counter_total{client_id=\"client_id\"} 1\n" + + "# EOF\n"; + assert_eq!(expected, buffer); +} + #[test] fn flatten() { #[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)] diff --git a/src/encoding.rs b/src/encoding.rs index c206a1c9..c9c9a838 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -468,12 +468,19 @@ impl EncodeLabelValue for &str { Ok(()) } } + impl EncodeLabelValue for String { fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> { EncodeLabelValue::encode(&self.as_str(), encoder) } } +impl EncodeLabelValue for &String { + fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> { + EncodeLabelValue::encode(&self.as_str(), encoder) + } +} + impl<'a> EncodeLabelValue for Cow<'a, str> { fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> { EncodeLabelValue::encode(&self.as_ref(), encoder)