Skip to content

Commit cf500d6

Browse files
committed
refactor codes and add a changelog entry
Signed-off-by: Wenbo Zhang <wenbo.zhang@iomesh.com>
1 parent 4c1d8bc commit cf500d6

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3434
- Supoort `Arc<String>` for `EncodeLabelValue`.
3535
See [PR 217].
3636

37+
- Added `get` method to `Family`.
38+
See [PR 234].
39+
3740
[PR 173]: https://github.com/prometheus/client_rust/pull/173
3841
[PR 216]: https://github.com/prometheus/client_rust/pull/216
3942
[PR 217]: https://github.com/prometheus/client_rust/pull/217
43+
[PR 234]: https://github.com/prometheus/client_rust/pull/234
4044

4145
### Fixed
4246

src/metrics/family.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,7 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
226226
/// family.get_or_create(&vec![("method".to_owned(), "GET".to_owned())]).inc();
227227
/// ```
228228
pub fn get_or_create(&self, label_set: &S) -> MappedRwLockReadGuard<M> {
229-
if let Ok(metric) =
230-
RwLockReadGuard::try_map(self.metrics.read(), |metrics| metrics.get(label_set))
231-
{
229+
if let Some(metric) = self.get(label_set) {
232230
return metric;
233231
}
234232

@@ -474,34 +472,34 @@ mod tests {
474472
fn test_get() {
475473
let family = Family::<Vec<(String, String)>, Counter>::default();
476474

477-
// Test getting a non-existent metric
475+
// Test getting a non-existent metric.
478476
let non_existent = family.get(&vec![("method".to_string(), "GET".to_string())]);
479477
assert!(non_existent.is_none());
480478

481-
// Create a metric
479+
// Create a metric.
482480
family
483481
.get_or_create(&vec![("method".to_string(), "GET".to_string())])
484482
.inc();
485483

486-
// Test getting an existing metric
484+
// Test getting an existing metric.
487485
let existing = family.get(&vec![("method".to_string(), "GET".to_string())]);
488486
assert!(existing.is_some());
489487
assert_eq!(existing.unwrap().get(), 1);
490488

491-
// Test getting a different non-existent metric
489+
// Test getting a different non-existent metric.
492490
let another_non_existent = family.get(&vec![("method".to_string(), "POST".to_string())]);
493491
assert!(another_non_existent.is_none());
494492

495-
// Test modifying the metric through the returned reference
493+
// Test modifying the metric through the returned reference.
496494
if let Some(metric) = family.get(&vec![("method".to_string(), "GET".to_string())]) {
497495
metric.inc();
498496
}
499497

500-
// Verify the modification
498+
// Verify the modification.
501499
let modified = family.get(&vec![("method".to_string(), "GET".to_string())]);
502500
assert_eq!(modified.unwrap().get(), 2);
503501

504-
// Test with a different label set type
502+
// Test with a different label set type.
505503
let string_family = Family::<String, Counter>::default();
506504
string_family.get_or_create(&"test".to_string()).inc();
507505

0 commit comments

Comments
 (0)