@@ -77,7 +77,7 @@ mod tests {
77
77
} ;
78
78
79
79
#[ test]
80
- fn span_context_immutable ( ) {
80
+ fn span_modification_via_context ( ) {
81
81
let exporter = InMemorySpanExporterBuilder :: new ( ) . build ( ) ;
82
82
let provider = SdkTracerProvider :: builder ( )
83
83
. with_span_processor ( SimpleSpanProcessor :: new ( exporter. clone ( ) ) )
@@ -91,32 +91,36 @@ mod tests {
91
91
92
92
// start with Current, which should have no span
93
93
let cx = Context :: current ( ) ;
94
- assert ! ( cx. has_active_span( ) == false ) ;
94
+ assert ! ( ! cx. has_active_span( ) ) ;
95
95
96
96
// add span to context
97
97
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( ) ) ;
100
100
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.
102
104
let span_ref = cx_with_span. span ( ) ;
103
105
span_ref. set_attribute ( KeyValue :: new ( "attribute1" , "value1" ) ) ;
104
106
105
107
// create a new context, which should not affect the original
106
108
let cx_with_span_and_more = cx_with_span. with_value ( ValueA ( 1 ) ) ;
107
109
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.
109
112
let span_ref_new = cx_with_span_and_more. span ( ) ;
110
113
span_ref_new. set_attribute ( KeyValue :: new ( "attribute2" , "value2" ) ) ;
111
114
112
- span_ref. end ( ) ;
113
115
span_ref_new. end ( ) ;
114
116
115
117
let exported_spans = exporter
116
118
. get_finished_spans ( )
117
119
. 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 ) ;
120
124
}
121
125
122
126
#[ test]
0 commit comments