Skip to content

Commit

Permalink
feat: allow Arc<String> on EncodeLabelSet (#217)
Browse files Browse the repository at this point in the history
Signed-off-by: Li Yazhou <[email protected]>
  • Loading branch information
flaneur2020 authored Jul 29, 2024
1 parent aeca8d8 commit 53f15ec
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>` 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]

Expand Down
32 changes: 32 additions & 0 deletions derive-encode/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String>,
}

let mut registry = Registry::default();
let family = Family::<Labels, Counter>::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, &registry).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)]
Expand Down
7 changes: 7 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 53f15ec

Please sign in to comment.