Skip to content

Commit

Permalink
Implement Display on Baggage (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
wperron authored Dec 12, 2022
1 parent eaa33d4 commit bf5ca2f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions opentelemetry-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ once_cell = "1.12.0"
pin-project-lite = { version = "0.2", optional = true }
thiserror = "1"
tokio-stream = { version = "0.1", optional = true }
urlencoding = "2.1.2"

[package.metadata.docs.rs]
all-features = true
Expand Down
70 changes: 70 additions & 0 deletions opentelemetry-api/src/baggage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use crate::{Context, Key, KeyValue, Value};
use once_cell::sync::Lazy;
use std::collections::{hash_map, HashMap};
use std::fmt;
use std::iter::FromIterator;
use urlencoding::encode;

static DEFAULT_BAGGAGE: Lazy<Baggage> = Lazy::new(Baggage::default);

Expand Down Expand Up @@ -280,6 +282,23 @@ impl FromIterator<KeyValueMetadata> for Baggage {
}
}

impl fmt::Display for Baggage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (i, (k, v)) in self.into_iter().enumerate() {
write!(f, "{}={}", k, encode(&v.0.as_str()))?;
if !v.1.as_str().is_empty() {
write!(f, ";{}", v.1)?;
}

if i < self.len() - 1 {
write!(f, ",")?;
}
}

Ok(())
}
}

/// Methods for sorting and retrieving baggage data in a context.
pub trait BaggageExt {
/// Returns a clone of the given context with the included name/value pairs.
Expand Down Expand Up @@ -397,6 +416,12 @@ impl From<&str> for BaggageMetadata {
}
}

impl fmt::Display for BaggageMetadata {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(write!(f, "{}", self.as_str())?)
}
}

/// [`Baggage`] name/value pairs with their associated metadata.
#[derive(Clone, Debug, PartialEq)]
pub struct KeyValueMetadata {
Expand Down Expand Up @@ -436,6 +461,8 @@ impl From<KeyValue> for KeyValueMetadata {

#[cfg(test)]
mod tests {
use crate::StringValue;

use super::*;

#[test]
Expand Down Expand Up @@ -494,4 +521,47 @@ mod tests {
let baggage = data.into_iter().collect::<Baggage>();
assert_eq!(baggage.len(), 3)
}

#[test]
fn serialize_baggage_as_string() {
// Empty baggage
let b = Baggage::default();
assert_eq!("", b.to_string());

// "single member empty value no properties"
let mut b = Baggage::default();
b.insert("foo", StringValue::from(""));
assert_eq!("foo=", b.to_string());

// "single member no properties"
let mut b = Baggage::default();
b.insert("foo", StringValue::from("1"));
assert_eq!("foo=1", b.to_string());

// "URL encoded value"
let mut b = Baggage::default();
b.insert("foo", StringValue::from("1=1"));
assert_eq!("foo=1%3D1", b.to_string());

// "single member empty value with properties"
let mut b = Baggage::default();
b.insert_with_metadata(
"foo",
StringValue::from(""),
BaggageMetadata::from("red;state=on"),
);
assert_eq!("foo=;red;state=on", b.to_string());

// "single member with properties"
let mut b = Baggage::default();
b.insert_with_metadata("foo", StringValue::from("1"), "red;state=on;z=z=z");
assert_eq!("foo=1;red;state=on;z=z=z", b.to_string());

// "two members with properties"
let mut b = Baggage::default();
b.insert_with_metadata("foo", StringValue::from("1"), "red;state=on");
b.insert_with_metadata("bar", StringValue::from("2"), "yellow");
assert!(b.to_string().contains("bar=2;yellow"));
assert!(b.to_string().contains("foo=1;red;state=on"));
}
}

0 comments on commit bf5ca2f

Please sign in to comment.