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

fix: align baggage.remove() implementation #2734

Merged
merged 5 commits into from
Mar 3, 2025
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
1 change: 1 addition & 0 deletions opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Bug Fix: `InstrumentationScope` implementation for `PartialEq` and `Hash` fixed to include Attributes also.
- *Breaking* Changed value type of `Baggage` from `Value` to `StringValue`
- Updated `Baggage` constants to reflect latest standard (`MAX_KEY_VALUE_PAIRS` - 180 -> 64, `MAX_BYTES_FOR_ONE_PAIR` - removed) and increased insert performance see #[2284](https://github.com/open-telemetry/opentelemetry-rust/pull/2284).
- *Breaking* Align `Baggage.remove()` signature with `.get()` to take the key as a reference

## 0.28.0

Expand Down
25 changes: 23 additions & 2 deletions opentelemetry/src/baggage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ impl Baggage {

/// Removes a name from the baggage, returning the value
/// corresponding to the name if the pair was previously in the map.
pub fn remove<K: Into<Key>>(&mut self, key: K) -> Option<(StringValue, BaggageMetadata)> {
self.inner.remove(&key.into())
pub fn remove<K: AsRef<str>>(&mut self, key: K) -> Option<(StringValue, BaggageMetadata)> {
self.inner.remove(key.as_ref())
}

/// Returns the number of attributes for this baggage
Expand Down Expand Up @@ -584,4 +584,25 @@ mod tests {
assert!(b.insert("c", StringValue::from("..")).is_none()); // exceeds MAX_LEN_OF_ALL_PAIRS
assert_eq!(b.insert("c", StringValue::from("!")).unwrap(), ".".into()); // replaces existing
}

#[test]
fn test_crud_operations() {
let mut baggage = Baggage::default();
assert!(baggage.is_empty());

// create
baggage.insert("foo", "1");
assert_eq!(baggage.len(), 1);

// get
assert_eq!(baggage.get("foo"), Some(&StringValue::from("1")));

// update
baggage.insert("foo", "2");
assert_eq!(baggage.get("foo"), Some(&StringValue::from("2")));

// delete
baggage.remove("foo");
assert!(baggage.is_empty());
}
}