14
14
//! accordance with the [W3C Baggage] specification.
15
15
//!
16
16
//! [W3C Baggage]: https://w3c.github.io/baggage
17
- use crate :: { Context , Key , KeyValue , Value } ;
17
+ use crate :: { Context , Key , KeyValue , StringValue } ;
18
18
use std:: collections:: { hash_map, HashMap } ;
19
19
use std:: fmt;
20
20
use std:: sync:: OnceLock ;
@@ -56,7 +56,7 @@ fn get_default_baggage() -> &'static Baggage {
56
56
/// [RFC2616, Section 2.2]: https://tools.ietf.org/html/rfc2616#section-2.2
57
57
#[ derive( Debug , Default ) ]
58
58
pub struct Baggage {
59
- inner : HashMap < Key , ( Value , BaggageMetadata ) > ,
59
+ inner : HashMap < Key , ( StringValue , BaggageMetadata ) > ,
60
60
kv_content_len : usize , // the length of key-value-metadata string in `inner`
61
61
}
62
62
@@ -74,30 +74,33 @@ impl Baggage {
74
74
/// # Examples
75
75
///
76
76
/// ```
77
- /// use opentelemetry::{baggage::Baggage, Value };
77
+ /// use opentelemetry::{baggage::Baggage, StringValue };
78
78
///
79
79
/// let mut cc = Baggage::new();
80
80
/// let _ = cc.insert("my-name", "my-value");
81
81
///
82
- /// assert_eq!(cc.get("my-name"), Some(&Value ::from("my-value")))
82
+ /// assert_eq!(cc.get("my-name"), Some(&StringValue ::from("my-value")))
83
83
/// ```
84
- pub fn get < K : AsRef < str > > ( & self , key : K ) -> Option < & Value > {
84
+ pub fn get < K : AsRef < str > > ( & self , key : K ) -> Option < & StringValue > {
85
85
self . inner . get ( key. as_ref ( ) ) . map ( |( value, _metadata) | value)
86
86
}
87
87
88
88
/// Returns a reference to the value and metadata associated with a given name
89
89
///
90
90
/// # Examples
91
91
/// ```
92
- /// use opentelemetry::{baggage::{Baggage, BaggageMetadata}, Value };
92
+ /// use opentelemetry::{baggage::{Baggage, BaggageMetadata}, StringValue };
93
93
///
94
94
/// let mut cc = Baggage::new();
95
95
/// let _ = cc.insert("my-name", "my-value");
96
96
///
97
97
/// // 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(""))))
99
99
/// ```
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 ) > {
101
104
self . inner . get ( key. as_ref ( ) )
102
105
}
103
106
@@ -109,17 +112,17 @@ impl Baggage {
109
112
/// # Examples
110
113
///
111
114
/// ```
112
- /// use opentelemetry::{baggage::Baggage, Value };
115
+ /// use opentelemetry::{baggage::Baggage, StringValue };
113
116
///
114
117
/// let mut cc = Baggage::new();
115
118
/// let _ = cc.insert("my-name", "my-value");
116
119
///
117
- /// assert_eq!(cc.get("my-name"), Some(&Value ::from("my-value")))
120
+ /// assert_eq!(cc.get("my-name"), Some(&StringValue ::from("my-value")))
118
121
/// ```
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 >
120
123
where
121
124
K : Into < Key > ,
122
- V : Into < Value > ,
125
+ V : Into < StringValue > ,
123
126
{
124
127
self . insert_with_metadata ( key, value, BaggageMetadata :: default ( ) )
125
128
. map ( |pair| pair. 0 )
@@ -133,22 +136,22 @@ impl Baggage {
133
136
/// # Examples
134
137
///
135
138
/// ```
136
- /// use opentelemetry::{baggage::{Baggage, BaggageMetadata}, Value };
139
+ /// use opentelemetry::{baggage::{Baggage, BaggageMetadata}, StringValue };
137
140
///
138
141
/// let mut cc = Baggage::new();
139
142
/// let _ = cc.insert_with_metadata("my-name", "my-value", "test");
140
143
///
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"))))
142
145
/// ```
143
146
pub fn insert_with_metadata < K , V , S > (
144
147
& mut self ,
145
148
key : K ,
146
149
value : V ,
147
150
metadata : S ,
148
- ) -> Option < ( Value , BaggageMetadata ) >
151
+ ) -> Option < ( StringValue , BaggageMetadata ) >
149
152
where
150
153
K : Into < Key > ,
151
- V : Into < Value > ,
154
+ V : Into < StringValue > ,
152
155
S : Into < BaggageMetadata > ,
153
156
{
154
157
let ( key, value, metadata) = ( key. into ( ) , value. into ( ) , metadata. into ( ) ) ;
@@ -161,7 +164,7 @@ impl Baggage {
161
164
162
165
/// Removes a name from the baggage, returning the value
163
166
/// 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 ) > {
165
168
self . inner . remove ( & key. into ( ) )
166
169
}
167
170
@@ -182,12 +185,12 @@ impl Baggage {
182
185
183
186
/// Determine whether the key value pair exceed one of the [limits](https://w3c.github.io/baggage/#limits).
184
187
/// 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 {
186
189
if !key. as_str ( ) . is_ascii ( ) {
187
190
return false ;
188
191
}
189
192
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 ( ) )
191
194
< MAX_BYTES_FOR_ONE_PAIR
192
195
{
193
196
match self . inner . get ( key) {
@@ -237,27 +240,27 @@ fn key_value_metadata_bytes_size(key: &str, value: &str, metadata: &str) -> usiz
237
240
238
241
/// An iterator over the entries of a [`Baggage`].
239
242
#[ 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 ) > ) ;
241
244
242
245
impl < ' a > Iterator for Iter < ' a > {
243
- type Item = ( & ' a Key , & ' a ( Value , BaggageMetadata ) ) ;
246
+ type Item = ( & ' a Key , & ' a ( StringValue , BaggageMetadata ) ) ;
244
247
245
248
fn next ( & mut self ) -> Option < Self :: Item > {
246
249
self . 0 . next ( )
247
250
}
248
251
}
249
252
250
253
impl < ' a > IntoIterator for & ' a Baggage {
251
- type Item = ( & ' a Key , & ' a ( Value , BaggageMetadata ) ) ;
254
+ type Item = ( & ' a Key , & ' a ( StringValue , BaggageMetadata ) ) ;
252
255
type IntoIter = Iter < ' a > ;
253
256
254
257
fn into_iter ( self ) -> Self :: IntoIter {
255
258
Iter ( self . inner . iter ( ) )
256
259
}
257
260
}
258
261
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 {
261
264
let mut baggage = Baggage :: default ( ) ;
262
265
for ( key, ( value, metadata) ) in iter. into_iter ( ) {
263
266
baggage. insert_with_metadata ( key, value, metadata) ;
@@ -304,7 +307,7 @@ fn encode(s: &str) -> String {
304
307
impl fmt:: Display for Baggage {
305
308
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
306
309
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( ) ) ) ?;
308
311
if !v. 1 . as_str ( ) . is_empty ( ) {
309
312
write ! ( f, ";{}" , v. 1 ) ?;
310
313
}
@@ -325,15 +328,15 @@ pub trait BaggageExt {
325
328
/// # Examples
326
329
///
327
330
/// ```
328
- /// use opentelemetry::{baggage::BaggageExt, Context, KeyValue, Value };
331
+ /// use opentelemetry::{baggage::BaggageExt, Context, KeyValue, StringValue };
329
332
///
330
333
/// let cx = Context::map_current(|cx| {
331
334
/// cx.with_baggage(vec![KeyValue::new("my-name", "my-value")])
332
335
/// });
333
336
///
334
337
/// assert_eq!(
335
338
/// cx.baggage().get("my-name"),
336
- /// Some(&Value ::from("my-value")),
339
+ /// Some(&StringValue ::from("my-value")),
337
340
/// )
338
341
/// ```
339
342
fn with_baggage < T : IntoIterator < Item = I > , I : Into < KeyValueMetadata > > (
@@ -346,13 +349,13 @@ pub trait BaggageExt {
346
349
/// # Examples
347
350
///
348
351
/// ```
349
- /// use opentelemetry::{baggage::BaggageExt, Context, KeyValue, Value };
352
+ /// use opentelemetry::{baggage::BaggageExt, Context, KeyValue, StringValue };
350
353
///
351
354
/// let cx = Context::current_with_baggage(vec![KeyValue::new("my-name", "my-value")]);
352
355
///
353
356
/// assert_eq!(
354
357
/// cx.baggage().get("my-name"),
355
- /// Some(&Value ::from("my-value")),
358
+ /// Some(&StringValue ::from("my-value")),
356
359
/// )
357
360
/// ```
358
361
fn current_with_baggage < T : IntoIterator < Item = I > , I : Into < KeyValueMetadata > > (
@@ -364,7 +367,7 @@ pub trait BaggageExt {
364
367
/// # Examples
365
368
///
366
369
/// ```
367
- /// use opentelemetry::{baggage::BaggageExt, Context, KeyValue, Value };
370
+ /// use opentelemetry::{baggage::BaggageExt, Context};
368
371
///
369
372
/// let cx = Context::map_current(|cx| cx.with_cleared_baggage());
370
373
///
@@ -448,7 +451,7 @@ pub struct KeyValueMetadata {
448
451
/// Dimension or event key
449
452
pub key : Key ,
450
453
/// Dimension or event value
451
- pub value : Value ,
454
+ pub value : StringValue ,
452
455
/// Metadata associate with this key value pair
453
456
pub metadata : BaggageMetadata ,
454
457
}
@@ -458,7 +461,7 @@ impl KeyValueMetadata {
458
461
pub fn new < K , V , S > ( key : K , value : V , metadata : S ) -> Self
459
462
where
460
463
K : Into < Key > ,
461
- V : Into < Value > ,
464
+ V : Into < StringValue > ,
462
465
S : Into < BaggageMetadata > ,
463
466
{
464
467
KeyValueMetadata {
@@ -473,7 +476,7 @@ impl From<KeyValue> for KeyValueMetadata {
473
476
fn from ( kv : KeyValue ) -> Self {
474
477
KeyValueMetadata {
475
478
key : kv. key ,
476
- value : kv. value ,
479
+ value : kv. value . into ( ) ,
477
480
metadata : BaggageMetadata :: default ( ) ,
478
481
}
479
482
}
@@ -546,7 +549,7 @@ mod tests {
546
549
baggage. insert ( pair. key . clone ( ) , pair. value ) ;
547
550
assert_eq ! (
548
551
baggage. get( pair. key) ,
549
- Some ( & Value :: from( "value" ) ) ,
552
+ Some ( & StringValue :: from( "value" ) ) ,
550
553
"If the input pair is too long, then don't replace entry with same key"
551
554
)
552
555
}
0 commit comments