Skip to content

Commit 53f15ec

Browse files
authored
feat: allow Arc<String> on EncodeLabelSet (#217)
Signed-off-by: Li Yazhou <[email protected]>
1 parent aeca8d8 commit 53f15ec

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Support `i32`/`f32` for `Gauge` and `u32`/`f32` for `Counter`/`CounterWithExemplar`.
2222
See [PR 173] and [PR 216].
2323

24+
- Supoort `Arc<String>` for `EncodeLabelValue`.
25+
See [PR 217].
26+
2427
[PR 173]: https://github.com/prometheus/client_rust/pull/173
2528
[PR 216]: https://github.com/prometheus/client_rust/pull/216
29+
[PR 217]: https://github.com/prometheus/client_rust/pull/217
2630

2731
## [0.22.3]
2832

derive-encode/tests/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use prometheus_client::encoding::text::encode;
24
use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue};
35
use prometheus_client::metrics::counter::Counter;
@@ -137,6 +139,36 @@ fn remap_keyword_identifiers() {
137139
assert_eq!(expected, buffer);
138140
}
139141

142+
#[test]
143+
fn arc_string() {
144+
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
145+
struct Labels {
146+
client_id: Arc<String>,
147+
}
148+
149+
let mut registry = Registry::default();
150+
let family = Family::<Labels, Counter>::default();
151+
registry.register("my_counter", "This is my counter", family.clone());
152+
153+
// Record a single HTTP GET request.
154+
let client_id = Arc::new("client_id".to_string());
155+
family
156+
.get_or_create(&Labels {
157+
client_id: client_id.clone(),
158+
})
159+
.inc();
160+
161+
// Encode all metrics in the registry in the text format.
162+
let mut buffer = String::new();
163+
encode(&mut buffer, &registry).unwrap();
164+
165+
let expected = "# HELP my_counter This is my counter.\n".to_owned()
166+
+ "# TYPE my_counter counter\n"
167+
+ "my_counter_total{client_id=\"client_id\"} 1\n"
168+
+ "# EOF\n";
169+
assert_eq!(expected, buffer);
170+
}
171+
140172
#[test]
141173
fn flatten() {
142174
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]

src/encoding.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,19 @@ impl EncodeLabelValue for &str {
468468
Ok(())
469469
}
470470
}
471+
471472
impl EncodeLabelValue for String {
472473
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
473474
EncodeLabelValue::encode(&self.as_str(), encoder)
474475
}
475476
}
476477

478+
impl EncodeLabelValue for &String {
479+
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
480+
EncodeLabelValue::encode(&self.as_str(), encoder)
481+
}
482+
}
483+
477484
impl<'a> EncodeLabelValue for Cow<'a, str> {
478485
fn encode(&self, encoder: &mut LabelValueEncoder) -> Result<(), std::fmt::Error> {
479486
EncodeLabelValue::encode(&self.as_ref(), encoder)

0 commit comments

Comments
 (0)