Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add test showing Baggage and Context interation with Tracing #2732

Merged
merged 8 commits into from
Mar 6, 2025
95 changes: 93 additions & 2 deletions opentelemetry-sdk/src/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@
trace::span_limit::{DEFAULT_MAX_EVENT_PER_SPAN, DEFAULT_MAX_LINKS_PER_SPAN},
trace::{InMemorySpanExporter, InMemorySpanExporterBuilder},
};
use opentelemetry::trace::{
SamplingDecision, SamplingResult, SpanKind, Status, TraceContextExt, TraceState,
use opentelemetry::{
baggage::BaggageExt,
trace::{SamplingDecision, SamplingResult, SpanKind, Status, TraceContextExt, TraceState},
};
use opentelemetry::{testing::trace::TestSpan, InstrumentationScope};
use opentelemetry::{
Expand Down Expand Up @@ -123,6 +124,96 @@
assert_eq!(span.attributes.len(), 2);
}

#[derive(Debug)]
struct BaggageInspectingSpanProcessor;
impl SpanProcessor for BaggageInspectingSpanProcessor {
fn on_start(&self, span: &mut crate::trace::Span, cx: &Context) {
let baggage = cx.baggage();
if let Some(baggage_value) = baggage.get("bag-key") {
span.set_attribute(KeyValue::new("bag-key", baggage_value.to_string()));
} else {
unreachable!("Baggage should be present in the context");

Check warning on line 135 in opentelemetry-sdk/src/trace/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/mod.rs#L135

Added line #L135 was not covered by tests
}
}

fn on_end(&self, _span: SpanData) {}

fn force_flush(&self) -> crate::error::OTelSdkResult {
Ok(())
}

Check warning on line 143 in opentelemetry-sdk/src/trace/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/mod.rs#L141-L143

Added lines #L141 - L143 were not covered by tests

fn shutdown(&self) -> crate::error::OTelSdkResult {
Ok(())
}
}

#[test]
fn span_and_baggage() {
let provider = SdkTracerProvider::builder()
.with_span_processor(BaggageInspectingSpanProcessor)
.build();

let cx_with_baggage =
Context::current_with_baggage(vec![KeyValue::new("bag-key", "bag-value")]);

// assert baggage is in the context
assert_eq!(
cx_with_baggage
.baggage()
.get("bag-key")
.unwrap()
.to_string(),
"bag-value"
);

// Attach context to current
let _cx_guard1 = cx_with_baggage.attach();
// now Current should have the baggage
assert_eq!(
Context::current()
.baggage()
.get("bag-key")
.unwrap()
.to_string(),
"bag-value"
);

let tracer = provider.tracer("test_tracer");
let mut span = tracer
.span_builder("span-name")
.start_with_context(&tracer, &Context::current());
span.set_attribute(KeyValue::new("attribute1", "value1"));

// We have not added span to the context yet
// so the current context should not have any span.
let cx = Context::current();
assert!(!cx.has_active_span());

// Now add span to context which already has baggage.
let cx_with_baggage_and_span = cx.with_span(span);
assert!(cx_with_baggage_and_span.has_active_span());
assert_eq!(
cx_with_baggage_and_span
.baggage()
.get("bag-key")
.unwrap()
.to_string(),
"bag-value"
);

let _cx_guard2 = cx_with_baggage_and_span.attach();
// Now current context should have both baggage and span.
assert!(Context::current().has_active_span());
assert_eq!(
Context::current()
.baggage()
.get("bag-key")
.unwrap()
.to_string(),
"bag-value"
);
}

#[test]
fn tracer_in_span() {
// Arrange
Expand Down