Skip to content

Commit

Permalink
fix: align baggage.remove() implementation (#2734)
Browse files Browse the repository at this point in the history
  • Loading branch information
gruebel authored Mar 3, 2025
1 parent 46a7cd6 commit 2493fec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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());
}
}

0 comments on commit 2493fec

Please sign in to comment.