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

Implement methods on Registry for managing prefix and common labels #400

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
61 changes: 61 additions & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,67 @@ impl Registry {
Ok(reg)
}

/// `prefix` returns a copy of the prefix for the registry, if any.
pub fn prefix(&self) -> Option<String> {
self.r.read().prefix.clone()
}

/// `set_prefix` sets the prefix for the registry, and returns the previous
/// prefix, if any. If `prefix` is empty, the function returns an error.
pub fn set_prefix(&self, prefix: &str) -> Result<Option<String>> {
if prefix.is_empty() {
return Err(Error::Msg("empty prefix namespace".to_string()));
}

let mut core = self.r.write();
let result = core.prefix.take();
core.prefix = Some(prefix.to_string());
Ok(result)
}

/// `clear_prefix` removes the prefix from the registry and returns it to
/// the caller, if a prefix was set.
pub fn clear_prefix(&self) -> Option<String> {
let mut core = self.r.write();
core.prefix.take()
}

/// `labels` returns a copy of the common labels for the registry, if any.
pub fn labels(&self) -> Option<HashMap<String, String>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and in prefix() above, can we return a reference instead (and let the consumers clone if they want)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because the borrow would outlive the function call.

self.r.read().labels.clone()
}

/// `insert_label` adds or updates a label in the set of common labels for
/// the registry, and returns the previous value of that label, if any.
/// If either `key` or `value` are empty, the function returns an error.
pub fn insert_label(&self, key: &str, value: &str) -> Result<Option<String>> {
if key.is_empty() {
return Err(Error::Msg("empty label key".to_string()));
}
if value.is_empty() {
return Err(Error::Msg("empty label value".to_string()));
}

let mut core = self.r.write();

if core.labels.is_none() {
core.labels = Some(HashMap::default());
}

Ok(core
.labels
.as_mut()
.unwrap()
.insert(key.to_string(), value.to_string()))
}

/// `clear_labels` removes all common labels from the registry, if any, and
/// returns them to the caller.
pub fn clear_labels(&self) -> Option<HashMap<String, String>> {
let mut core = self.r.write();
core.labels.take()
}

/// `register` registers a new [`Collector`] to be included in metrics
/// collection. It returns an error if the descriptors provided by the
/// [`Collector`] are invalid or if they — in combination with descriptors of
Expand Down