Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/metrics/family.rs: Add remove and clear methods, and tests #85

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Added support for the OpenMetrics protobuf format. See [PR 83].
- Added a `remove` method to `Family` to allow the removal of a specified label
set from a family. See [PR 85].
- Added a `clear` method to `Family` to allow the removal of all label sets
from a family. See [PR 85].

### Changed

- Move`Encode` trait from `prometheus_client::encoding::text` to `prometheus_client::encoding`. See [PR 83].

[PR 83]: https://github.com/prometheus/client_rust/pull/83
[PR 85]: https://github.com/prometheus/client_rust/pull/85

## [0.18.0]

Expand Down
124 changes: 124 additions & 0 deletions src/metrics/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,47 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
})
}

/// Remove a label set from the metric family.
///
/// Returns a bool indicating if a label set was removed or not.
///
/// ```
/// # use prometheus_client::metrics::counter::{Atomic, Counter};
/// # use prometheus_client::metrics::family::Family;
/// #
/// let family = Family::<Vec<(String, String)>, Counter>::default();
///
/// // Will create the metric with label `method="GET"` on first call and
/// // return a reference.
/// family.get_or_create(&vec![("method".to_owned(), "GET".to_owned())]).inc();
///
/// // Will return `true`, indicating that the `method="GET"` label set was
/// // removed.
/// assert!(family.remove(&vec![("method".to_owned(), "GET".to_owned())]));
/// ```
pub fn remove(&self, label_set: &S) -> bool {
self.metrics.write().remove(label_set).is_some()
}

/// Clear all label sets from the metric family.
///
/// ```
/// # use prometheus_client::metrics::counter::{Atomic, Counter};
/// # use prometheus_client::metrics::family::Family;
/// #
/// let family = Family::<Vec<(String, String)>, Counter>::default();
///
/// // Will create the metric with label `method="GET"` on first call and
/// // return a reference.
/// family.get_or_create(&vec![("method".to_owned(), "GET".to_owned())]).inc();
///
/// // Clear the family of all label sets.
/// family.clear();
/// ```
pub fn clear(&self) {
self.metrics.write().clear()
}

pub(crate) fn read(&self) -> RwLockReadGuard<HashMap<S, M>> {
self.metrics.read()
}
Expand Down Expand Up @@ -295,4 +336,87 @@ mod tests {
let custom_builder = CustomBuilder { custom_start: 1.0 };
Family::<(), Histogram, CustomBuilder>::new_with_constructor(custom_builder);
}

#[test]
fn counter_family_remove() {
let family = Family::<Vec<(String, String)>, Counter>::default();

family
.get_or_create(&vec![("method".to_string(), "GET".to_string())])
.inc();

assert_eq!(
1,
family
.get_or_create(&vec![("method".to_string(), "GET".to_string())])
.get()
);

family
.get_or_create(&vec![("method".to_string(), "POST".to_string())])
.inc_by(2);

assert_eq!(
2,
family
.get_or_create(&vec![("method".to_string(), "POST".to_string())])
.get()
);

// Attempt to remove it twice, showing it really was removed on the
// first attempt.
assert!(family.remove(&vec![("method".to_string(), "POST".to_string())]));
assert!(!family.remove(&vec![("method".to_string(), "POST".to_string())]));

// This should make a new POST label.
family
.get_or_create(&vec![("method".to_string(), "POST".to_string())])
.inc();

assert_eq!(
1,
family
.get_or_create(&vec![("method".to_string(), "POST".to_string())])
.get()
);

// GET label should have be untouched.
assert_eq!(
1,
family
.get_or_create(&vec![("method".to_string(), "GET".to_string())])
.get()
);
}

#[test]
fn counter_family_clear() {
let family = Family::<Vec<(String, String)>, Counter>::default();

// Create a label and check it.
family
.get_or_create(&vec![("method".to_string(), "GET".to_string())])
.inc();

assert_eq!(
1,
family
.get_or_create(&vec![("method".to_string(), "GET".to_string())])
.get()
);

// Clear it, then try recreating and checking it again.
family.clear();

family
.get_or_create(&vec![("method".to_string(), "GET".to_string())])
.inc();

assert_eq!(
1,
family
.get_or_create(&vec![("method".to_string(), "GET".to_string())])
.get()
);
}
}