Skip to content

Commit 5e47487

Browse files
gruebelcijothomas
andauthored
fix: change value type of Baggage to StringValue (open-telemetry#2729)
Co-authored-by: Cijo Thomas <cijo.thomas@gmail.com>
1 parent 382bad4 commit 5e47487

File tree

4 files changed

+63
-47
lines changed

4 files changed

+63
-47
lines changed

opentelemetry-sdk/src/propagation/baggage.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -166,34 +166,34 @@ mod tests {
166166
use std::collections::HashMap;
167167

168168
#[rustfmt::skip]
169-
fn valid_extract_data() -> Vec<(&'static str, HashMap<Key, Value>)> {
169+
fn valid_extract_data() -> Vec<(&'static str, HashMap<Key, StringValue>)> {
170170
vec![
171171
// "valid w3cHeader"
172-
("key1=val1,key2=val2", vec![(Key::new("key1"), Value::from("val1")), (Key::new("key2"), Value::from("val2"))].into_iter().collect()),
172+
("key1=val1,key2=val2", vec![(Key::new("key1"), StringValue::from("val1")), (Key::new("key2"), StringValue::from("val2"))].into_iter().collect()),
173173
// "valid w3cHeader with spaces"
174-
("key1 = val1, key2 =val2 ", vec![(Key::new("key1"), Value::from("val1")), (Key::new("key2"), Value::from("val2"))].into_iter().collect()),
174+
("key1 = val1, key2 =val2 ", vec![(Key::new("key1"), StringValue::from("val1")), (Key::new("key2"), StringValue::from("val2"))].into_iter().collect()),
175175
// "valid header with url-escaped comma"
176-
("key1=val1,key2=val2%2Cval3", vec![(Key::new("key1"), Value::from("val1")), (Key::new("key2"), Value::from("val2,val3"))].into_iter().collect()),
176+
("key1=val1,key2=val2%2Cval3", vec![(Key::new("key1"), StringValue::from("val1")), (Key::new("key2"), StringValue::from("val2,val3"))].into_iter().collect()),
177177
// "valid header with an invalid header"
178-
("key1=val1,key2=val2,a,val3", vec![(Key::new("key1"), Value::from("val1")), (Key::new("key2"), Value::from("val2"))].into_iter().collect()),
178+
("key1=val1,key2=val2,a,val3", vec![(Key::new("key1"), StringValue::from("val1")), (Key::new("key2"), StringValue::from("val2"))].into_iter().collect()),
179179
// "valid header with no value"
180-
("key1=,key2=val2", vec![(Key::new("key1"), Value::from("")), (Key::new("key2"), Value::from("val2"))].into_iter().collect()),
180+
("key1=,key2=val2", vec![(Key::new("key1"), StringValue::from("")), (Key::new("key2"), StringValue::from("val2"))].into_iter().collect()),
181181
]
182182
}
183183

184184
#[rustfmt::skip]
185185
#[allow(clippy::type_complexity)]
186-
fn valid_extract_data_with_metadata() -> Vec<(&'static str, HashMap<Key, (Value, BaggageMetadata)>)> {
186+
fn valid_extract_data_with_metadata() -> Vec<(&'static str, HashMap<Key, (StringValue, BaggageMetadata)>)> {
187187
vec![
188188
// "valid w3cHeader with properties"
189-
("key1=val1,key2=val2;prop=1", vec![(Key::new("key1"), (Value::from("val1"), BaggageMetadata::default())), (Key::new("key2"), (Value::from("val2"), BaggageMetadata::from("prop=1")))].into_iter().collect()),
189+
("key1=val1,key2=val2;prop=1", vec![(Key::new("key1"), (StringValue::from("val1"), BaggageMetadata::default())), (Key::new("key2"), (StringValue::from("val2"), BaggageMetadata::from("prop=1")))].into_iter().collect()),
190190
// prop can don't need to be key value pair
191-
("key1=val1,key2=val2;prop1", vec![(Key::new("key1"), (Value::from("val1"), BaggageMetadata::default())), (Key::new("key2"), (Value::from("val2"), BaggageMetadata::from("prop1")))].into_iter().collect()),
191+
("key1=val1,key2=val2;prop1", vec![(Key::new("key1"), (StringValue::from("val1"), BaggageMetadata::default())), (Key::new("key2"), (StringValue::from("val2"), BaggageMetadata::from("prop1")))].into_iter().collect()),
192192
("key1=value1;property1;property2, key2 = value2, key3=value3; propertyKey=propertyValue",
193193
vec![
194-
(Key::new("key1"), (Value::from("value1"), BaggageMetadata::from("property1;property2"))),
195-
(Key::new("key2"), (Value::from("value2"), BaggageMetadata::default())),
196-
(Key::new("key3"), (Value::from("value3"), BaggageMetadata::from("propertyKey=propertyValue"))),
194+
(Key::new("key1"), (StringValue::from("value1"), BaggageMetadata::from("property1;property2"))),
195+
(Key::new("key2"), (StringValue::from("value2"), BaggageMetadata::default())),
196+
(Key::new("key3"), (StringValue::from("value3"), BaggageMetadata::from("propertyKey=propertyValue"))),
197197
].into_iter().collect()),
198198
]
199199
}

opentelemetry/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- *Breaking* Moved `TraceResult` type alias from `opentelemetry::trace::TraceResult` to `opentelemetry_sdk::trace::TraceResult`
88
- {PLACEHOLDER} - Remove the above completely. // TODO fill this when changes are actually in.
99
- Bug Fix: `InstrumentationScope` implementation for `PartialEq` and `Hash` fixed to include Attributes also.
10+
- *Breaking* Changed value type of `Baggage` from `Value` to `StringValue`
1011

1112
## 0.28.0
1213

opentelemetry/src/baggage.rs

+37-34
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! accordance with the [W3C Baggage] specification.
1515
//!
1616
//! [W3C Baggage]: https://w3c.github.io/baggage
17-
use crate::{Context, Key, KeyValue, Value};
17+
use crate::{Context, Key, KeyValue, StringValue};
1818
use std::collections::{hash_map, HashMap};
1919
use std::fmt;
2020
use std::sync::OnceLock;
@@ -56,7 +56,7 @@ fn get_default_baggage() -> &'static Baggage {
5656
/// [RFC2616, Section 2.2]: https://tools.ietf.org/html/rfc2616#section-2.2
5757
#[derive(Debug, Default)]
5858
pub struct Baggage {
59-
inner: HashMap<Key, (Value, BaggageMetadata)>,
59+
inner: HashMap<Key, (StringValue, BaggageMetadata)>,
6060
kv_content_len: usize, // the length of key-value-metadata string in `inner`
6161
}
6262

@@ -74,30 +74,33 @@ impl Baggage {
7474
/// # Examples
7575
///
7676
/// ```
77-
/// use opentelemetry::{baggage::Baggage, Value};
77+
/// use opentelemetry::{baggage::Baggage, StringValue};
7878
///
7979
/// let mut cc = Baggage::new();
8080
/// let _ = cc.insert("my-name", "my-value");
8181
///
82-
/// assert_eq!(cc.get("my-name"), Some(&Value::from("my-value")))
82+
/// assert_eq!(cc.get("my-name"), Some(&StringValue::from("my-value")))
8383
/// ```
84-
pub fn get<K: AsRef<str>>(&self, key: K) -> Option<&Value> {
84+
pub fn get<K: AsRef<str>>(&self, key: K) -> Option<&StringValue> {
8585
self.inner.get(key.as_ref()).map(|(value, _metadata)| value)
8686
}
8787

8888
/// Returns a reference to the value and metadata associated with a given name
8989
///
9090
/// # Examples
9191
/// ```
92-
/// use opentelemetry::{baggage::{Baggage, BaggageMetadata}, Value};
92+
/// use opentelemetry::{baggage::{Baggage, BaggageMetadata}, StringValue};
9393
///
9494
/// let mut cc = Baggage::new();
9595
/// let _ = cc.insert("my-name", "my-value");
9696
///
9797
/// // By default, the metadata is empty
98-
/// assert_eq!(cc.get_with_metadata("my-name"), Some(&(Value::from("my-value"), BaggageMetadata::from(""))))
98+
/// assert_eq!(cc.get_with_metadata("my-name"), Some(&(StringValue::from("my-value"), BaggageMetadata::from(""))))
9999
/// ```
100-
pub fn get_with_metadata<K: AsRef<str>>(&self, key: K) -> Option<&(Value, BaggageMetadata)> {
100+
pub fn get_with_metadata<K: AsRef<str>>(
101+
&self,
102+
key: K,
103+
) -> Option<&(StringValue, BaggageMetadata)> {
101104
self.inner.get(key.as_ref())
102105
}
103106

@@ -109,17 +112,17 @@ impl Baggage {
109112
/// # Examples
110113
///
111114
/// ```
112-
/// use opentelemetry::{baggage::Baggage, Value};
115+
/// use opentelemetry::{baggage::Baggage, StringValue};
113116
///
114117
/// let mut cc = Baggage::new();
115118
/// let _ = cc.insert("my-name", "my-value");
116119
///
117-
/// assert_eq!(cc.get("my-name"), Some(&Value::from("my-value")))
120+
/// assert_eq!(cc.get("my-name"), Some(&StringValue::from("my-value")))
118121
/// ```
119-
pub fn insert<K, V>(&mut self, key: K, value: V) -> Option<Value>
122+
pub fn insert<K, V>(&mut self, key: K, value: V) -> Option<StringValue>
120123
where
121124
K: Into<Key>,
122-
V: Into<Value>,
125+
V: Into<StringValue>,
123126
{
124127
self.insert_with_metadata(key, value, BaggageMetadata::default())
125128
.map(|pair| pair.0)
@@ -133,22 +136,22 @@ impl Baggage {
133136
/// # Examples
134137
///
135138
/// ```
136-
/// use opentelemetry::{baggage::{Baggage, BaggageMetadata}, Value};
139+
/// use opentelemetry::{baggage::{Baggage, BaggageMetadata}, StringValue};
137140
///
138141
/// let mut cc = Baggage::new();
139142
/// let _ = cc.insert_with_metadata("my-name", "my-value", "test");
140143
///
141-
/// assert_eq!(cc.get_with_metadata("my-name"), Some(&(Value::from("my-value"), BaggageMetadata::from("test"))))
144+
/// assert_eq!(cc.get_with_metadata("my-name"), Some(&(StringValue::from("my-value"), BaggageMetadata::from("test"))))
142145
/// ```
143146
pub fn insert_with_metadata<K, V, S>(
144147
&mut self,
145148
key: K,
146149
value: V,
147150
metadata: S,
148-
) -> Option<(Value, BaggageMetadata)>
151+
) -> Option<(StringValue, BaggageMetadata)>
149152
where
150153
K: Into<Key>,
151-
V: Into<Value>,
154+
V: Into<StringValue>,
152155
S: Into<BaggageMetadata>,
153156
{
154157
let (key, value, metadata) = (key.into(), value.into(), metadata.into());
@@ -161,7 +164,7 @@ impl Baggage {
161164

162165
/// Removes a name from the baggage, returning the value
163166
/// corresponding to the name if the pair was previously in the map.
164-
pub fn remove<K: Into<Key>>(&mut self, key: K) -> Option<(Value, BaggageMetadata)> {
167+
pub fn remove<K: Into<Key>>(&mut self, key: K) -> Option<(StringValue, BaggageMetadata)> {
165168
self.inner.remove(&key.into())
166169
}
167170

@@ -182,12 +185,12 @@ impl Baggage {
182185

183186
/// Determine whether the key value pair exceed one of the [limits](https://w3c.github.io/baggage/#limits).
184187
/// If not, update the total length of key values
185-
fn insertable(&mut self, key: &Key, value: &Value, metadata: &BaggageMetadata) -> bool {
188+
fn insertable(&mut self, key: &Key, value: &StringValue, metadata: &BaggageMetadata) -> bool {
186189
if !key.as_str().is_ascii() {
187190
return false;
188191
}
189192
let value = value.as_str();
190-
if key_value_metadata_bytes_size(key.as_str(), value.as_ref(), metadata.as_str())
193+
if key_value_metadata_bytes_size(key.as_str(), value, metadata.as_str())
191194
< MAX_BYTES_FOR_ONE_PAIR
192195
{
193196
match self.inner.get(key) {
@@ -237,27 +240,27 @@ fn key_value_metadata_bytes_size(key: &str, value: &str, metadata: &str) -> usiz
237240

238241
/// An iterator over the entries of a [`Baggage`].
239242
#[derive(Debug)]
240-
pub struct Iter<'a>(hash_map::Iter<'a, Key, (Value, BaggageMetadata)>);
243+
pub struct Iter<'a>(hash_map::Iter<'a, Key, (StringValue, BaggageMetadata)>);
241244

242245
impl<'a> Iterator for Iter<'a> {
243-
type Item = (&'a Key, &'a (Value, BaggageMetadata));
246+
type Item = (&'a Key, &'a (StringValue, BaggageMetadata));
244247

245248
fn next(&mut self) -> Option<Self::Item> {
246249
self.0.next()
247250
}
248251
}
249252

250253
impl<'a> IntoIterator for &'a Baggage {
251-
type Item = (&'a Key, &'a (Value, BaggageMetadata));
254+
type Item = (&'a Key, &'a (StringValue, BaggageMetadata));
252255
type IntoIter = Iter<'a>;
253256

254257
fn into_iter(self) -> Self::IntoIter {
255258
Iter(self.inner.iter())
256259
}
257260
}
258261

259-
impl FromIterator<(Key, (Value, BaggageMetadata))> for Baggage {
260-
fn from_iter<I: IntoIterator<Item = (Key, (Value, BaggageMetadata))>>(iter: I) -> Self {
262+
impl FromIterator<(Key, (StringValue, BaggageMetadata))> for Baggage {
263+
fn from_iter<I: IntoIterator<Item = (Key, (StringValue, BaggageMetadata))>>(iter: I) -> Self {
261264
let mut baggage = Baggage::default();
262265
for (key, (value, metadata)) in iter.into_iter() {
263266
baggage.insert_with_metadata(key, value, metadata);
@@ -304,7 +307,7 @@ fn encode(s: &str) -> String {
304307
impl fmt::Display for Baggage {
305308
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
306309
for (i, (k, v)) in self.into_iter().enumerate() {
307-
write!(f, "{}={}", k, encode(v.0.as_str().as_ref()))?;
310+
write!(f, "{}={}", k, encode(v.0.as_str()))?;
308311
if !v.1.as_str().is_empty() {
309312
write!(f, ";{}", v.1)?;
310313
}
@@ -325,15 +328,15 @@ pub trait BaggageExt {
325328
/// # Examples
326329
///
327330
/// ```
328-
/// use opentelemetry::{baggage::BaggageExt, Context, KeyValue, Value};
331+
/// use opentelemetry::{baggage::BaggageExt, Context, KeyValue, StringValue};
329332
///
330333
/// let cx = Context::map_current(|cx| {
331334
/// cx.with_baggage(vec![KeyValue::new("my-name", "my-value")])
332335
/// });
333336
///
334337
/// assert_eq!(
335338
/// cx.baggage().get("my-name"),
336-
/// Some(&Value::from("my-value")),
339+
/// Some(&StringValue::from("my-value")),
337340
/// )
338341
/// ```
339342
fn with_baggage<T: IntoIterator<Item = I>, I: Into<KeyValueMetadata>>(
@@ -346,13 +349,13 @@ pub trait BaggageExt {
346349
/// # Examples
347350
///
348351
/// ```
349-
/// use opentelemetry::{baggage::BaggageExt, Context, KeyValue, Value};
352+
/// use opentelemetry::{baggage::BaggageExt, Context, KeyValue, StringValue};
350353
///
351354
/// let cx = Context::current_with_baggage(vec![KeyValue::new("my-name", "my-value")]);
352355
///
353356
/// assert_eq!(
354357
/// cx.baggage().get("my-name"),
355-
/// Some(&Value::from("my-value")),
358+
/// Some(&StringValue::from("my-value")),
356359
/// )
357360
/// ```
358361
fn current_with_baggage<T: IntoIterator<Item = I>, I: Into<KeyValueMetadata>>(
@@ -364,7 +367,7 @@ pub trait BaggageExt {
364367
/// # Examples
365368
///
366369
/// ```
367-
/// use opentelemetry::{baggage::BaggageExt, Context, KeyValue, Value};
370+
/// use opentelemetry::{baggage::BaggageExt, Context};
368371
///
369372
/// let cx = Context::map_current(|cx| cx.with_cleared_baggage());
370373
///
@@ -448,7 +451,7 @@ pub struct KeyValueMetadata {
448451
/// Dimension or event key
449452
pub key: Key,
450453
/// Dimension or event value
451-
pub value: Value,
454+
pub value: StringValue,
452455
/// Metadata associate with this key value pair
453456
pub metadata: BaggageMetadata,
454457
}
@@ -458,7 +461,7 @@ impl KeyValueMetadata {
458461
pub fn new<K, V, S>(key: K, value: V, metadata: S) -> Self
459462
where
460463
K: Into<Key>,
461-
V: Into<Value>,
464+
V: Into<StringValue>,
462465
S: Into<BaggageMetadata>,
463466
{
464467
KeyValueMetadata {
@@ -473,7 +476,7 @@ impl From<KeyValue> for KeyValueMetadata {
473476
fn from(kv: KeyValue) -> Self {
474477
KeyValueMetadata {
475478
key: kv.key,
476-
value: kv.value,
479+
value: kv.value.into(),
477480
metadata: BaggageMetadata::default(),
478481
}
479482
}
@@ -546,7 +549,7 @@ mod tests {
546549
baggage.insert(pair.key.clone(), pair.value);
547550
assert_eq!(
548551
baggage.get(pair.key),
549-
Some(&Value::from("value")),
552+
Some(&StringValue::from("value")),
550553
"If the input pair is too long, then don't replace entry with same key"
551554
)
552555
}

opentelemetry/src/common.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,22 @@ impl From<Cow<'static, str>> for StringValue {
302302
}
303303
}
304304

305+
impl From<Value> for StringValue {
306+
fn from(s: Value) -> Self {
307+
match s {
308+
Value::Bool(v) => format!("{}", v).into(),
309+
Value::I64(v) => format!("{}", v).into(),
310+
Value::F64(v) => format!("{}", v).into(),
311+
Value::String(v) => v,
312+
Value::Array(v) => format!("{}", v).into(),
313+
}
314+
}
315+
}
316+
305317
impl Value {
306318
/// String representation of the `Value`
307319
///
308-
/// This will allocate iff the underlying value is not a `String`.
320+
/// This will allocate if the underlying value is not a `String`.
309321
pub fn as_str(&self) -> Cow<'_, str> {
310322
match self {
311323
Value::Bool(v) => format!("{}", v).into(),

0 commit comments

Comments
 (0)