Skip to content
Closed
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
41 changes: 41 additions & 0 deletions lib/vector-core/src/event/metric/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,17 @@ impl MetricTags {
self.0.remove(name).and_then(TagValueSet::into_single)
}

/// Rename a tag from `old_name` to `new_name`, overwriting `new_name` if it already exists.
/// Returns `true` if the rename was performed.
pub fn rename_with_replacement(&mut self, old_name: &str, new_name: String) -> bool {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could this be accomplished better by adding another remove function that doesn't call TagValueSet::into_single and then calling set_multi_value in the code that needs this method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah that's a good suggestion. Will go with that instead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will close this PR to have an accurate branch name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

if let Some(values) = self.0.remove(old_name) {
self.0.insert(new_name, values);
true
} else {
false
}
}

pub fn keys(&self) -> impl Iterator<Item = &str> {
self.0.keys().map(String::as_str)
}
Expand Down Expand Up @@ -640,6 +651,36 @@ mod tests {

use super::*;

fn make_tags(pairs: &[(&str, &str)]) -> MetricTags {
pairs
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect()
}

#[test]
fn rename_basic() {
let mut tags = make_tags(&[("old", "v1")]);
assert!(tags.rename_with_replacement("old", "new".to_string()));
assert_eq!(tags.get("new"), Some("v1"));
assert!(!tags.contains_key("old"));
}

#[test]
fn rename_missing_old_returns_false() {
let mut tags = make_tags(&[("a", "1")]);
assert!(!tags.rename_with_replacement("missing", "b".to_string()));
assert!(tags.contains_key("a"));
}

#[test]
fn rename_overwrites_existing_destination() {
let mut tags = make_tags(&[("old", "v1"), ("new", "v2")]);
assert!(tags.rename_with_replacement("old", "new".to_string()));
assert_eq!(tags.get("new"), Some("v1"));
assert!(!tags.contains_key("old"));
}

proptest! {
#[test]
fn reduces_set_to_simple(mut values: TagValueSet) {
Expand Down
Loading