Skip to content

Commit

Permalink
[Baggage] Simplify Get APIs (open-telemetry#1902)
Browse files Browse the repository at this point in the history
Co-authored-by: Zhongyang Wu <[email protected]>
  • Loading branch information
utpilla and TommyCpp authored Jul 2, 2024
1 parent d5c86d9 commit ad990d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
8 changes: 4 additions & 4 deletions opentelemetry/src/baggage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl Baggage {
///
/// assert_eq!(cc.get("my-name"), Some(&Value::from("my-value")))
/// ```
pub fn get<T: Into<Key>>(&self, key: T) -> Option<&Value> {
self.inner.get(&key.into()).map(|(value, _metadata)| value)
pub fn get<K: AsRef<str>>(&self, key: K) -> Option<&Value> {
self.inner.get(key.as_ref()).map(|(value, _metadata)| value)
}

/// Returns a reference to the value and metadata associated with a given name
Expand All @@ -91,8 +91,8 @@ impl Baggage {
/// // By default, the metadata is empty
/// assert_eq!(cc.get_with_metadata("my-name"), Some(&(Value::from("my-value"), BaggageMetadata::from(""))))
/// ```
pub fn get_with_metadata<T: Into<Key>>(&self, key: T) -> Option<&(Value, BaggageMetadata)> {
self.inner.get(&key.into())
pub fn get_with_metadata<K: AsRef<str>>(&self, key: K) -> Option<&(Value, BaggageMetadata)> {
self.inner.get(key.as_ref())
}

/// Inserts a name/value pair into the baggage.
Expand Down
14 changes: 13 additions & 1 deletion opentelemetry/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::borrow::Cow;
use std::borrow::{Borrow, Cow};
use std::sync::Arc;
use std::{fmt, hash};

Expand Down Expand Up @@ -135,6 +135,18 @@ impl fmt::Display for Key {
}
}

impl Borrow<str> for Key {
fn borrow(&self) -> &str {
self.0.as_str()
}
}

impl AsRef<str> for Key {
fn as_ref(&self) -> &str {
self.0.as_str()
}
}

#[derive(Clone, Debug, Eq)]
enum OtelString {
Owned(Box<str>),
Expand Down

0 comments on commit ad990d6

Please sign in to comment.