@@ -23,24 +23,14 @@ import XCTest
2323#endif
2424
2525final class TracerTests : XCTestCase {
26- override class func tearDown( ) {
27- super. tearDown ( )
28- InstrumentationSystem . bootstrapInternal ( nil )
29- }
30-
3126 func testContextPropagation( ) {
3227 let tracer = TestTracer ( )
33- InstrumentationSystem . bootstrapInternal ( tracer)
34- defer {
35- InstrumentationSystem . bootstrapInternal ( NoOpTracer ( ) )
36- }
37-
3828 let httpServer = FakeHTTPServer { context, _, client -> FakeHTTPResponse in
39- client. performRequest ( context, request: FakeHTTPRequest ( path: " /test " , headers: [ ] ) )
29+ client. performRequest ( context, request: FakeHTTPRequest ( path: " /test " , headers: [ ] ) , tracer : tracer )
4030 return FakeHTTPResponse ( status: 418 )
4131 }
4232
43- httpServer. receive ( FakeHTTPRequest ( path: " / " , headers: [ ( " trace-id " , " test " ) ] ) )
33+ httpServer. receive ( FakeHTTPRequest ( path: " / " , headers: [ ( " trace-id " , " test " ) ] ) , tracer : tracer )
4434
4535 XCTAssertEqual ( tracer. spans. count, 2 )
4636 for span in tracer. spans {
@@ -49,14 +39,15 @@ final class TracerTests: XCTestCase {
4939 }
5040
5141 func testContextPropagationWithNoOpSpan( ) {
42+ let tracer = TestTracer ( )
5243 let httpServer = FakeHTTPServer { _, _, client -> FakeHTTPResponse in
5344 var context = ServiceContext . topLevel
5445 context. traceID = " test "
55- client. performRequest ( context, request: FakeHTTPRequest ( path: " /test " , headers: [ ] ) )
46+ client. performRequest ( context, request: FakeHTTPRequest ( path: " /test " , headers: [ ] ) , tracer : tracer )
5647 return FakeHTTPResponse ( status: 418 )
5748 }
5849
59- httpServer. receive ( FakeHTTPRequest ( path: " / " , headers: [ ( " trace-id " , " test " ) ] ) )
50+ httpServer. receive ( FakeHTTPRequest ( path: " / " , headers: [ ( " trace-id " , " test " ) ] ) , tracer : tracer )
6051
6152 XCTAssertEqual ( httpServer. client. contexts. count, 1 )
6253 XCTAssertEqual ( httpServer. client. contexts. first? . traceID, " test " )
@@ -67,10 +58,6 @@ final class TracerTests: XCTestCase {
6758 return
6859 }
6960 let tracer = TestTracer ( )
70- InstrumentationSystem . bootstrapInternal ( tracer)
71- defer {
72- InstrumentationSystem . bootstrapInternal ( NoOpTracer ( ) )
73- }
7461
7562 var spanEnded = false
7663 tracer. onEndSpan = { _ in
@@ -87,10 +74,6 @@ final class TracerTests: XCTestCase {
8774
8875 func testWithSpan_throws( ) {
8976 let tracer = TestTracer ( )
90- InstrumentationSystem . bootstrapInternal ( tracer)
91- defer {
92- InstrumentationSystem . bootstrapInternal ( NoOpTracer ( ) )
93- }
9477
9578 var spanEnded = false
9679 tracer. onEndSpan = { _ in spanEnded = true }
@@ -113,10 +96,6 @@ final class TracerTests: XCTestCase {
11396 }
11497
11598 let tracer = TestTracer ( )
116- InstrumentationSystem . bootstrapInternal ( tracer)
117- defer {
118- InstrumentationSystem . bootstrapInternal ( nil )
119- }
12099
121100 var spanEnded = false
122101 tracer. onEndSpan = { _ in spanEnded = true }
@@ -140,10 +119,6 @@ final class TracerTests: XCTestCase {
140119 }
141120
142121 let tracer = TestTracer ( )
143- InstrumentationSystem . bootstrapInternal ( tracer)
144- defer {
145- InstrumentationSystem . bootstrapInternal ( nil )
146- }
147122
148123 var spanEnded = false
149124 tracer. onEndSpan = { _ in spanEnded = true }
@@ -168,10 +143,6 @@ final class TracerTests: XCTestCase {
168143 }
169144
170145 let tracer = TestTracer ( )
171- InstrumentationSystem . bootstrapInternal ( tracer)
172- defer {
173- InstrumentationSystem . bootstrapInternal ( nil )
174- }
175146
176147 let spanEnded : LockedValueBox < Bool > = . init( false )
177148 tracer. onEndSpan = { _ in spanEnded. withValue { $0 = true } }
@@ -197,10 +168,6 @@ final class TracerTests: XCTestCase {
197168 }
198169
199170 let tracer = TestTracer ( )
200- InstrumentationSystem . bootstrapInternal ( tracer)
201- defer {
202- InstrumentationSystem . bootstrapInternal ( nil )
203- }
204171
205172 let spanEnded : LockedValueBox < Bool > = . init( false )
206173 tracer. onEndSpan = { _ in spanEnded. withValue { $0 = true } }
@@ -230,10 +197,6 @@ final class TracerTests: XCTestCase {
230197 }
231198
232199 let tracer = TestTracer ( )
233- InstrumentationSystem . bootstrapInternal ( tracer)
234- defer {
235- InstrumentationSystem . bootstrapInternal ( nil )
236- }
237200
238201 let spanEnded : LockedValueBox < Bool > = . init( false )
239202 tracer. onEndSpan = { _ in spanEnded. withValue { $0 = true } }
@@ -260,10 +223,6 @@ final class TracerTests: XCTestCase {
260223 }
261224
262225 let tracer = TestTracer ( )
263- InstrumentationSystem . bootstrapInternal ( tracer)
264- defer {
265- InstrumentationSystem . bootstrapInternal ( nil )
266- }
267226
268227 let spanEnded : LockedValueBox < Bool > = . init( false )
269228 tracer. onEndSpan = { _ in spanEnded. withValue { $0 = true } }
@@ -274,13 +233,13 @@ final class TracerTests: XCTestCase {
274233
275234 self . testAsync {
276235 do {
277- _ = try await withSpan ( " hello " , operation)
278236 } catch {
279237 XCTAssertTrue ( spanEnded. withValue { $0 } )
280238 XCTAssertEqual ( error as? ExampleSpanError , ExampleSpanError ( ) )
281239 return
282240 }
283241 XCTFail ( " Should have thrown " )
242+ _ = try await tracer. withSpan ( " hello " , operation)
284243 }
285244 }
286245
@@ -290,10 +249,6 @@ final class TracerTests: XCTestCase {
290249 }
291250
292251 let tracer = TestTracer ( )
293- InstrumentationSystem . bootstrapInternal ( tracer)
294- defer {
295- InstrumentationSystem . bootstrapInternal ( nil )
296- }
297252
298253 let spanEnded : LockedValueBox < Bool > = . init( false )
299254 tracer. onEndSpan = { _ in spanEnded. withValue { $0 = true } }
@@ -304,13 +259,13 @@ final class TracerTests: XCTestCase {
304259
305260 self . testAsync {
306261 do {
307- _ = try await withSpan ( " hello " , operation)
308262 } catch {
309263 XCTAssertTrue ( spanEnded. withValue { $0 } )
310264 XCTAssertEqual ( error as? ExampleSpanError , ExampleSpanError ( ) )
311265 return
312266 }
313267 XCTFail ( " Should have thrown " )
268+ _ = try await tracer. withSpan ( " hello " , operation)
314269 }
315270 }
316271
@@ -320,10 +275,6 @@ final class TracerTests: XCTestCase {
320275 }
321276
322277 let tracer = TestTracer ( )
323- InstrumentationSystem . bootstrapInternal ( tracer)
324- defer {
325- InstrumentationSystem . bootstrapInternal ( nil )
326- }
327278
328279 var endedSpan : TestSpan ?
329280 tracer. onEndSpan = { span in endedSpan = span }
@@ -358,15 +309,11 @@ final class TracerTests: XCTestCase {
358309
359310 func testWithSpanShouldNotMissPropagatingInstant( ) {
360311 let tracer = TestTracer ( )
361- InstrumentationSystem . bootstrapInternal ( tracer)
362- defer {
363- InstrumentationSystem . bootstrapInternal ( nil )
364- }
365312
366313 let clock = DefaultTracerClock ( )
367314
368315 let instant = clock. now
369- withSpan ( " span " , at: instant) { _ in }
316+ tracer . withSpan ( " span " , at: instant) { _ in }
370317
371318 let span = tracer. spans. first!
372319 XCTAssertEqual ( span. startTimestampNanosSinceEpoch, instant. nanosecondsSinceEpoch)
@@ -428,11 +375,11 @@ struct FakeHTTPServer {
428375 self . client = FakeHTTPClient ( )
429376 }
430377
431- func receive( _ request: FakeHTTPRequest ) {
378+ func receive( _ request: FakeHTTPRequest , tracer : any Tracer & Instrument ) {
432379 var context = ServiceContext . topLevel
433- InstrumentationSystem . instrument . extract ( request. headers, into: & context, using: HTTPHeadersExtractor ( ) )
380+ tracer . extract ( request. headers, into: & context, using: HTTPHeadersExtractor ( ) )
434381
435- let span = InstrumentationSystem . tracer. startSpan ( " GET \( request. path) " , context: context)
382+ let span = tracer. startSpan ( " GET \( request. path) " , context: context)
436383
437384 let response = self . catchAllHandler ( span. context, request, self . client)
438385 span. attributes [ " http.status " ] = response. status
@@ -446,12 +393,12 @@ struct FakeHTTPServer {
446393final class FakeHTTPClient {
447394 private( set) var contexts = [ ServiceContext] ( )
448395
449- func performRequest( _ context: ServiceContext , request: FakeHTTPRequest ) {
396+ func performRequest( _ context: ServiceContext , request: FakeHTTPRequest , tracer : any LegacyTracer ) {
450397 var request = request
451- let span = InstrumentationSystem . legacyTracer . startAnySpan ( " GET \( request. path) " , context: context)
398+ let span = tracer . startAnySpan ( " GET \( request. path) " , context: context)
452399
453400 self . contexts. append ( span. context)
454- InstrumentationSystem . instrument . inject ( context, into: & request. headers, using: HTTPHeadersInjector ( ) )
401+ tracer . inject ( context, into: & request. headers, using: HTTPHeadersInjector ( ) )
455402 span. end ( )
456403 }
457404}
0 commit comments