Skip to content

Commit a599f5e

Browse files
committed
modify test to confirm the expected behavior
1 parent 4313d83 commit a599f5e

File tree

1 file changed

+13
-9
lines changed
  • opentelemetry-sdk/src/trace

1 file changed

+13
-9
lines changed

opentelemetry-sdk/src/trace/mod.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ mod tests {
7777
};
7878

7979
#[test]
80-
fn span_context_immutable() {
80+
fn span_modification_via_context() {
8181
let exporter = InMemorySpanExporterBuilder::new().build();
8282
let provider = SdkTracerProvider::builder()
8383
.with_span_processor(SimpleSpanProcessor::new(exporter.clone()))
@@ -91,32 +91,36 @@ mod tests {
9191

9292
// start with Current, which should have no span
9393
let cx = Context::current();
94-
assert!(cx.has_active_span() == false);
94+
assert!(!cx.has_active_span());
9595

9696
// add span to context
9797
let cx_with_span = cx.with_span(span);
98-
assert!(cx_with_span.has_active_span() == true);
99-
assert!(cx.has_active_span() == false);
98+
assert!(cx_with_span.has_active_span());
99+
assert!(!cx.has_active_span());
100100

101-
// modify the span in the context
101+
// modify the span by using span_ref from the context
102+
// this is the only way to modify the span as span
103+
// is moved to context.
102104
let span_ref = cx_with_span.span();
103105
span_ref.set_attribute(KeyValue::new("attribute1", "value1"));
104106

105107
// create a new context, which should not affect the original
106108
let cx_with_span_and_more = cx_with_span.with_value(ValueA(1));
107109

108-
// modify the span in the new context, which should not affect the original
110+
// modify the span again using the new context.
111+
// this should still be using the original span itself.
109112
let span_ref_new = cx_with_span_and_more.span();
110113
span_ref_new.set_attribute(KeyValue::new("attribute2", "value2"));
111114

112-
span_ref.end();
113115
span_ref_new.end();
114116

115117
let exported_spans = exporter
116118
.get_finished_spans()
117119
.expect("Spans are expected to be exported.");
118-
// There should be 2 independent spans.
119-
assert_eq!(exported_spans.len(), 2);
120+
// There should be a single span, with attributes from both modifications.
121+
assert_eq!(exported_spans.len(), 1);
122+
let span = &exported_spans[0];
123+
assert_eq!(span.attributes.len(), 2);
120124
}
121125

122126
#[test]

0 commit comments

Comments
 (0)