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 } ;
1818use std:: collections:: { hash_map, HashMap } ;
1919use std:: fmt;
2020use 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 ) ]
5858pub 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
242245impl < ' 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
250253impl < ' 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 {
304307impl 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 }
0 commit comments