@@ -39,6 +39,9 @@ class URLSessionInstrumentationTests: XCTestCase {
3939 static var requestCopy : URLRequest !
4040 static var responseCopy : HTTPURLResponse !
4141
42+ static var activeBaggage : Baggage !
43+ static var defaultBaggage : Baggage !
44+
4245 static var config = URLSessionInstrumentationConfiguration ( shouldRecordPayload: nil ,
4346 shouldInstrument: { req in
4447 checker. shouldInstrumentCalled = true
@@ -76,25 +79,31 @@ class URLSessionInstrumentationTests: XCTestCase {
7679 } ,
7780 receivedError: { _, _, _, _ in
7881 URLSessionInstrumentationTests . checker. receivedErrorCalled = true
82+ } ,
83+ defaultBaggageProvider: {
84+ defaultBaggage
7985 } )
8086
8187 static var checker = Check ( )
8288 static var semaphore : DispatchSemaphore !
8389 var sessionDelegate : SessionDelegate !
8490 static var instrumentation : URLSessionInstrumentation !
85- static var baggage : Baggage !
8691
8792 static let server = HttpTestServer ( url: URL ( string: " http://localhost:33333 " ) , config: nil )
8893
8994 override class func setUp( ) {
9095 OpenTelemetry . registerPropagators ( textPropagators: [ W3CTraceContextPropagator ( ) ] , baggagePropagator: W3CBaggagePropagator ( ) )
9196 OpenTelemetry . registerTracerProvider ( tracerProvider: TracerProviderSdk ( ) )
9297
93- baggage = DefaultBaggageManager . instance. baggageBuilder ( )
98+ defaultBaggage = DefaultBaggageManager . instance. baggageBuilder ( )
99+ . put ( key: EntryKey ( name: " bar " ) !, value: EntryValue ( string: " baz " ) !, metadata: nil )
100+ . build ( )
101+
102+ activeBaggage = DefaultBaggageManager . instance. baggageBuilder ( )
94103 . put ( key: EntryKey ( name: " foo " ) !, value: EntryValue ( string: " bar " ) !, metadata: nil )
95104 . build ( )
96105
97- OpenTelemetry . instance. contextProvider. setActiveBaggage ( baggage )
106+ OpenTelemetry . instance. contextProvider. setActiveBaggage ( activeBaggage )
98107
99108 let sem = DispatchSemaphore ( value: 0 )
100109 DispatchQueue . global ( qos: . default) . async {
@@ -111,7 +120,8 @@ class URLSessionInstrumentationTests: XCTestCase {
111120
112121 override class func tearDown( ) {
113122 server. stop ( )
114- OpenTelemetry . instance. contextProvider. removeContextForBaggage ( baggage)
123+ defaultBaggage = nil
124+ OpenTelemetry . instance. contextProvider. removeContextForBaggage ( activeBaggage)
115125 }
116126
117127 override func setUp( ) {
@@ -262,13 +272,63 @@ class URLSessionInstrumentationTests: XCTestCase {
262272
263273 XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInstrumentCalled)
264274
275+ XCTAssertEqual ( 1 , URLSessionLogger . runningSpans. count)
276+
277+ let span = try XCTUnwrap ( URLSessionLogger . runningSpans [ " 111 " ] )
278+ XCTAssertEqual ( " HTTP GET " , span. name)
279+ }
280+
281+ public func testShouldInstrumentRequest_PropagateCombinedActiveAndDefaultBaggages( ) throws {
282+ let request1 = URLRequest ( url: URL ( string: " http://defaultName.com " ) !)
283+ let request2 = URLRequest ( url: URL ( string: " http://dontinstrument.com " ) !)
284+
285+ let processedRequest1 = try XCTUnwrap ( URLSessionLogger . processAndLogRequest ( request1, sessionTaskId: " 111 " , instrumentation: URLSessionInstrumentationTests . instrumentation, shouldInjectHeaders: true ) )
286+ let processedRequest2 = URLSessionLogger . processAndLogRequest ( request2, sessionTaskId: " 222 " , instrumentation: URLSessionInstrumentationTests . instrumentation, shouldInjectHeaders: true )
287+
288+ // `processedRequest2` is expected to be nil, because its URL was marked as not to be instrumented.
289+ XCTAssertNil ( processedRequest2)
290+
291+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInstrumentCalled)
292+
293+ let processedHeaders1 = try XCTUnwrap ( processedRequest1. allHTTPHeaderFields)
294+
295+ // headers injected from `TextMapPropagator` implementation
296+ XCTAssertTrue ( processedHeaders1. contains ( where: { $0. key == W3CTraceContextPropagator . traceparent } ) )
297+
298+ // headers injected from `TextMapBaggagePropagator` implementation
299+ let baggageHeaderValue = try XCTUnwrap ( processedHeaders1 [ W3CBaggagePropagator . headerBaggage] )
300+
301+ // foo=bar propagated through active baggage defined in `setUp`
302+ XCTAssertTrue ( baggageHeaderValue. contains ( " foo=bar " ) )
303+
304+ // bar=baz propagated through default baggage provided in `URLSessionInstrumentationConfiguration`
305+ XCTAssertTrue ( baggageHeaderValue. contains ( " bar=baz " ) )
306+
307+ XCTAssertEqual ( 1 , URLSessionLogger . runningSpans. count)
308+
309+ let span = try XCTUnwrap ( URLSessionLogger . runningSpans [ " 111 " ] )
310+ XCTAssertEqual ( " HTTP GET " , span. name)
311+ }
312+
313+ public func testShouldInstrumentRequest_PropagateOnlyActiveBaggage( ) throws {
314+ Self . defaultBaggage = nil
315+
316+ let request1 = URLRequest ( url: URL ( string: " http://defaultName.com " ) !)
317+
318+ let processedRequest1 = try XCTUnwrap ( URLSessionLogger . processAndLogRequest ( request1, sessionTaskId: " 111 " , instrumentation: URLSessionInstrumentationTests . instrumentation, shouldInjectHeaders: true ) )
319+
320+ XCTAssertTrue ( URLSessionInstrumentationTests . checker. shouldInstrumentCalled)
321+
265322 let processedHeaders1 = try XCTUnwrap ( processedRequest1. allHTTPHeaderFields)
266323
267324 // headers injected from `TextMapPropagator` implementation
268325 XCTAssertTrue ( processedHeaders1. contains ( where: { $0. key == W3CTraceContextPropagator . traceparent } ) )
269326
270327 // headers injected from `TextMapBaggagePropagator` implementation
271- XCTAssertTrue ( processedHeaders1. contains ( where: { $0. key == W3CBaggagePropagator . headerBaggage && $0. value == " foo=bar " } ) )
328+ let baggageHeaderValue = try XCTUnwrap ( processedHeaders1 [ W3CBaggagePropagator . headerBaggage] )
329+
330+ // bar=baz propagated through default baggage provided in `URLSessionInstrumentationConfiguration`
331+ XCTAssertEqual ( baggageHeaderValue, " foo=bar " )
272332
273333 XCTAssertEqual ( 1 , URLSessionLogger . runningSpans. count)
274334
0 commit comments