Skip to content

Commit 9d3b98f

Browse files
committed
allow to pass iterator to with_baggage()
1 parent 9ca7b29 commit 9d3b98f

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

opentelemetry-sdk/src/propagation/baggage.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use opentelemetry::{
2-
baggage::{Baggage, BaggageExt, KeyValueMetadata},
2+
baggage::{BaggageExt, KeyValueMetadata},
33
otel_warn,
44
propagation::{text_map_propagator::FieldIter, Extractor, Injector, TextMapPropagator},
55
Context,
@@ -151,7 +151,7 @@ impl TextMapPropagator for BaggagePropagator {
151151
None
152152
}
153153
});
154-
cx.with_baggage(Baggage::from_iter(baggage))
154+
cx.with_baggage(baggage)
155155
} else {
156156
cx.clone()
157157
}
@@ -278,7 +278,7 @@ mod tests {
278278

279279
for (kvm, header_parts) in valid_inject_data() {
280280
let mut injector = HashMap::new();
281-
let cx = Context::current_with_baggage(Baggage::from_iter(kvm));
281+
let cx = Context::current_with_baggage(kvm);
282282
propagator.inject_context(&cx, &mut injector);
283283
let header_value = injector.get(BAGGAGE_HEADER).unwrap();
284284
assert_eq!(header_parts.join(",").len(), header_value.len(),);
@@ -310,7 +310,7 @@ mod tests {
310310

311311
for (kvm, header_parts) in valid_inject_data_metadata() {
312312
let mut injector = HashMap::new();
313-
let cx = Context::current_with_baggage(Baggage::from_iter(kvm));
313+
let cx = Context::current_with_baggage(kvm);
314314
propagator.inject_context(&cx, &mut injector);
315315
let header_value = injector.get(BAGGAGE_HEADER).unwrap();
316316

opentelemetry/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- Updated `Baggage` constants to reflect latest standard (`MAX_KEY_VALUE_PAIRS` - 180 -> 64, `MAX_BYTES_FOR_ONE_PAIR` - removed) and increased insert performance see #[2284](https://github.com/open-telemetry/opentelemetry-rust/pull/2284).
1313
- Align `Baggage.remove()` signature with `.get()` to take the key as a reference
1414
- `Baggage` can't be retrieved from the `Context` directly anymore and needs to be accessed via `context.baggage()`
15-
- `with_baggage()` and `current_with_baggage()` only accept a `Baggage` instance and override any existing `Baggage` in the `Context`
15+
- `with_baggage()` and `current_with_baggage()` override any existing `Baggage` in the `Context`
1616

1717
## 0.28.0
1818

opentelemetry/src/baggage.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ impl FromIterator<KeyValueMetadata> for Baggage {
273273
}
274274
}
275275

276+
impl<I> From<I> for Baggage
277+
where
278+
I: IntoIterator,
279+
I::Item: Into<KeyValueMetadata>,
280+
{
281+
fn from(value: I) -> Self {
282+
value.into_iter().map(Into::into).collect()
283+
}
284+
}
285+
276286
fn encode(s: &str) -> String {
277287
let mut encoded_string = String::with_capacity(s.len());
278288

@@ -312,21 +322,27 @@ pub trait BaggageExt {
312322
/// # Examples
313323
///
314324
/// ```
315-
/// use opentelemetry::{baggage::{Baggage, BaggageExt}, Context, StringValue};
325+
/// use opentelemetry::{baggage::{Baggage, BaggageExt}, Context, KeyValue, StringValue};
316326
///
327+
/// // Explicit `Baggage` creation
317328
/// let mut baggage = Baggage::new();
318329
/// let _ = baggage.insert("my-name", "my-value");
319330
///
320331
/// let cx = Context::map_current(|cx| {
321332
/// cx.with_baggage(baggage)
322333
/// });
323334
///
335+
/// // Passing an iterator
336+
/// let cx = Context::map_current(|cx| {
337+
/// cx.with_baggage(vec![KeyValue::new("my-name", "my-value")])
338+
/// });
339+
///
324340
/// assert_eq!(
325341
/// cx.baggage().get("my-name"),
326342
/// Some(&StringValue::from("my-value")),
327343
/// )
328344
/// ```
329-
fn with_baggage(&self, baggage: Baggage) -> Self;
345+
fn with_baggage<T: Into<Baggage>>(&self, baggage: T) -> Self;
330346

331347
/// Returns a clone of the current context with the included name/value pairs.
332348
///
@@ -345,7 +361,7 @@ pub trait BaggageExt {
345361
/// Some(&StringValue::from("my-value")),
346362
/// )
347363
/// ```
348-
fn current_with_baggage(baggage: Baggage) -> Self;
364+
fn current_with_baggage<T: Into<Baggage>>(baggage: T) -> Self;
349365

350366
/// Returns a clone of the given context with no baggage.
351367
///
@@ -370,11 +386,11 @@ pub trait BaggageExt {
370386
struct BaggageContextValue(Baggage);
371387

372388
impl BaggageExt for Context {
373-
fn with_baggage(&self, baggage: Baggage) -> Self {
374-
self.with_value(BaggageContextValue(baggage))
389+
fn with_baggage<T: Into<Baggage>>(&self, baggage: T) -> Self {
390+
self.with_value(BaggageContextValue(baggage.into()))
375391
}
376392

377-
fn current_with_baggage(baggage: Baggage) -> Self {
393+
fn current_with_baggage<T: Into<Baggage>>(baggage: T) -> Self {
378394
Context::map_current(|cx| cx.with_baggage(baggage))
379395
}
380396

opentelemetry/src/propagation/composite.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::collections::HashSet;
2323
///
2424
/// ```
2525
/// use opentelemetry::{
26-
/// baggage::{Baggage, BaggageExt},
26+
/// baggage::BaggageExt,
2727
/// propagation::{TextMapPropagator, TextMapCompositePropagator},
2828
///
2929
/// trace::{TraceContextExt, Tracer, TracerProvider},
@@ -56,7 +56,7 @@ use std::collections::HashSet;
5656
/// // with the current context, call inject to add the headers
5757
/// composite_propagator.inject_context(
5858
/// &Context::current_with_span(example_span)
59-
/// .with_baggage(Baggage::from_iter([KeyValue::new("test", "example")])),
59+
/// .with_baggage(vec![KeyValue::new("test", "example")]),
6060
/// &mut injector,
6161
/// );
6262
///
@@ -115,7 +115,7 @@ impl TextMapPropagator for TextMapCompositePropagator {
115115

116116
#[cfg(all(test, feature = "trace"))]
117117
mod tests {
118-
use crate::baggage::{Baggage, BaggageExt};
118+
use crate::baggage::BaggageExt;
119119
use crate::propagation::TextMapCompositePropagator;
120120
use crate::testing::trace::TestSpan;
121121
use crate::{
@@ -162,9 +162,7 @@ mod tests {
162162
false,
163163
TraceState::default(),
164164
)),
165-
("baggage", Some(_)) => {
166-
cx.with_baggage(Baggage::from_iter([KeyValue::new("baggagekey", "value")]))
167-
}
165+
("baggage", Some(_)) => cx.with_baggage(vec![KeyValue::new("baggagekey", "value")]),
168166
_ => cx.clone(),
169167
}
170168
}
@@ -184,7 +182,7 @@ mod tests {
184182
TraceState::default(),
185183
)));
186184
// setup for baggage propagator
187-
cx.with_baggage(Baggage::from_iter([KeyValue::new("baggagekey", "value")]))
185+
cx.with_baggage(vec![KeyValue::new("baggagekey", "value")])
188186
}
189187

190188
fn test_data() -> Vec<(&'static str, &'static str)> {

0 commit comments

Comments
 (0)