@@ -64,8 +64,9 @@ mod tests {
64
64
trace:: span_limit:: { DEFAULT_MAX_EVENT_PER_SPAN , DEFAULT_MAX_LINKS_PER_SPAN } ,
65
65
trace:: { InMemorySpanExporter , InMemorySpanExporterBuilder } ,
66
66
} ;
67
- use opentelemetry:: trace:: {
68
- SamplingDecision , SamplingResult , SpanKind , Status , TraceContextExt , TraceState ,
67
+ use opentelemetry:: {
68
+ baggage:: BaggageExt ,
69
+ trace:: { SamplingDecision , SamplingResult , SpanKind , Status , TraceContextExt , TraceState } ,
69
70
} ;
70
71
use opentelemetry:: { testing:: trace:: TestSpan , InstrumentationScope } ;
71
72
use opentelemetry:: {
@@ -123,6 +124,96 @@ mod tests {
123
124
assert_eq ! ( span. attributes. len( ) , 2 ) ;
124
125
}
125
126
127
+ #[ derive( Debug ) ]
128
+ struct BaggageInspectingSpanProcessor ;
129
+ impl SpanProcessor for BaggageInspectingSpanProcessor {
130
+ fn on_start ( & self , span : & mut crate :: trace:: Span , cx : & Context ) {
131
+ let baggage = cx. baggage ( ) ;
132
+ if let Some ( baggage_value) = baggage. get ( "bag-key" ) {
133
+ span. set_attribute ( KeyValue :: new ( "bag-key" , baggage_value. to_string ( ) ) ) ;
134
+ } else {
135
+ unreachable ! ( "Baggage should be present in the context" ) ;
136
+ }
137
+ }
138
+
139
+ fn on_end ( & self , _span : SpanData ) { }
140
+
141
+ fn force_flush ( & self ) -> crate :: error:: OTelSdkResult {
142
+ Ok ( ( ) )
143
+ }
144
+
145
+ fn shutdown ( & self ) -> crate :: error:: OTelSdkResult {
146
+ Ok ( ( ) )
147
+ }
148
+ }
149
+
150
+ #[ test]
151
+ fn span_and_baggage ( ) {
152
+ let provider = SdkTracerProvider :: builder ( )
153
+ . with_span_processor ( BaggageInspectingSpanProcessor )
154
+ . build ( ) ;
155
+
156
+ let cx_with_baggage =
157
+ Context :: current_with_baggage ( vec ! [ KeyValue :: new( "bag-key" , "bag-value" ) ] ) ;
158
+
159
+ // assert baggage is in the context
160
+ assert_eq ! (
161
+ cx_with_baggage
162
+ . baggage( )
163
+ . get( "bag-key" )
164
+ . unwrap( )
165
+ . to_string( ) ,
166
+ "bag-value"
167
+ ) ;
168
+
169
+ // Attach context to current
170
+ let _cx_guard1 = cx_with_baggage. attach ( ) ;
171
+ // now Current should have the baggage
172
+ assert_eq ! (
173
+ Context :: current( )
174
+ . baggage( )
175
+ . get( "bag-key" )
176
+ . unwrap( )
177
+ . to_string( ) ,
178
+ "bag-value"
179
+ ) ;
180
+
181
+ let tracer = provider. tracer ( "test_tracer" ) ;
182
+ let mut span = tracer
183
+ . span_builder ( "span-name" )
184
+ . start_with_context ( & tracer, & Context :: current ( ) ) ;
185
+ span. set_attribute ( KeyValue :: new ( "attribute1" , "value1" ) ) ;
186
+
187
+ // We have not added span to the context yet
188
+ // so the current context should not have any span.
189
+ let cx = Context :: current ( ) ;
190
+ assert ! ( !cx. has_active_span( ) ) ;
191
+
192
+ // Now add span to context which already has baggage.
193
+ let cx_with_baggage_and_span = cx. with_span ( span) ;
194
+ assert ! ( cx_with_baggage_and_span. has_active_span( ) ) ;
195
+ assert_eq ! (
196
+ cx_with_baggage_and_span
197
+ . baggage( )
198
+ . get( "bag-key" )
199
+ . unwrap( )
200
+ . to_string( ) ,
201
+ "bag-value"
202
+ ) ;
203
+
204
+ let _cx_guard2 = cx_with_baggage_and_span. attach ( ) ;
205
+ // Now current context should have both baggage and span.
206
+ assert ! ( Context :: current( ) . has_active_span( ) ) ;
207
+ assert_eq ! (
208
+ Context :: current( )
209
+ . baggage( )
210
+ . get( "bag-key" )
211
+ . unwrap( )
212
+ . to_string( ) ,
213
+ "bag-value"
214
+ ) ;
215
+ }
216
+
126
217
#[ test]
127
218
fn tracer_in_span ( ) {
128
219
// Arrange
0 commit comments