15
15
@_spi ( Locking) import Instrumentation
16
16
import Tracing
17
17
18
- public struct TestTracer : Tracer {
18
+ public struct InMemoryTracer : Tracer {
19
+
20
+ public static let traceIDKey = " test-trace-id "
21
+ public static let spanIDKey = " test-span-id "
22
+
19
23
public let idGenerator : IDGenerator
20
24
25
+ private let _activeSpans = LockedValueBox < [ InMemorySpanContext : InMemorySpan ] > ( [ : ] )
26
+ private let _finishedSpans = LockedValueBox < [ FinishedInMemorySpan ] > ( [ ] )
27
+ private let _numberOfForceFlushes = LockedValueBox < Int > ( 0 )
28
+
21
29
public init ( idGenerator: IDGenerator = . incrementing) {
22
30
self . idGenerator = idGenerator
23
31
}
@@ -32,30 +40,30 @@ public struct TestTracer: Tracer {
32
40
function: String ,
33
41
file fileID: String ,
34
42
line: UInt
35
- ) -> TestSpan where Instant: TracerInstant {
43
+ ) -> InMemorySpan where Instant: TracerInstant {
36
44
let parentContext = context ( )
37
- let spanContext : TestSpanContext
45
+ let spanContext : InMemorySpanContext
38
46
39
- if let parentSpanContext = parentContext. testSpanContext {
47
+ if let parentSpanContext = parentContext. inMemorySpanContext {
40
48
// child span
41
- spanContext = TestSpanContext (
49
+ spanContext = InMemorySpanContext (
42
50
traceID: parentSpanContext. traceID,
43
51
spanID: idGenerator. nextSpanID ( ) ,
44
52
parentSpanID: parentSpanContext. spanID
45
53
)
46
54
} else {
47
55
// root span
48
- spanContext = TestSpanContext (
56
+ spanContext = InMemorySpanContext (
49
57
traceID: idGenerator. nextTraceID ( ) ,
50
58
spanID: idGenerator. nextSpanID ( ) ,
51
59
parentSpanID: nil
52
60
)
53
61
}
54
62
55
63
var context = parentContext
56
- context. testSpanContext = spanContext
64
+ context. inMemorySpanContext = spanContext
57
65
58
- let span = TestSpan (
66
+ let span = InMemorySpan (
59
67
operationName: operationName,
60
68
context: context,
61
69
spanContext: spanContext,
@@ -69,8 +77,8 @@ public struct TestTracer: Tracer {
69
77
return span
70
78
}
71
79
72
- public func activeSpan( identifiedBy context: ServiceContext ) -> TestSpan ? {
73
- guard let spanContext = context. testSpanContext else { return nil }
80
+ public func activeSpan( identifiedBy context: ServiceContext ) -> InMemorySpan ? {
81
+ guard let spanContext = context. inMemorySpanContext else { return nil }
74
82
return _activeSpans. withValue { $0 [ spanContext] }
75
83
}
76
84
@@ -82,27 +90,20 @@ public struct TestTracer: Tracer {
82
90
_numberOfForceFlushes. withValue { $0 }
83
91
}
84
92
85
- public var finishedSpans : [ FinishedTestSpan ] {
93
+ public var finishedSpans : [ FinishedInMemorySpan ] {
86
94
_finishedSpans. withValue { $0 }
87
95
}
88
96
89
- private let _activeSpans = LockedValueBox < [ TestSpanContext : TestSpan ] > ( [ : ] )
90
- private let _finishedSpans = LockedValueBox < [ FinishedTestSpan ] > ( [ ] )
91
- private let _numberOfForceFlushes = LockedValueBox < Int > ( 0 )
92
-
93
97
// MARK: - Instrument
94
98
95
- public static let traceIDKey = " test-trace-id "
96
- public static let spanIDKey = " test-span-id "
97
-
98
99
public func inject< Carrier, Inject: Injector > (
99
100
_ context: ServiceContext ,
100
101
into carrier: inout Carrier ,
101
102
using injector: Inject
102
103
) where Carrier == Inject . Carrier {
103
104
var values = [ String: String] ( )
104
105
105
- if let spanContext = context. testSpanContext {
106
+ if let spanContext = context. inMemorySpanContext {
106
107
injector. inject ( spanContext. traceID, forKey: Self . traceIDKey, into: & carrier)
107
108
values [ Self . traceIDKey] = spanContext. traceID
108
109
injector. inject ( spanContext. spanID, forKey: Self . spanIDKey, into: & carrier)
@@ -133,7 +134,7 @@ public struct TestTracer: Tracer {
133
134
return
134
135
}
135
136
136
- context. testSpanContext = TestSpanContext ( traceID: traceID, spanID: spanID, parentSpanID: nil )
137
+ context. inMemorySpanContext = InMemorySpanContext ( traceID: traceID, spanID: spanID, parentSpanID: nil )
137
138
}
138
139
139
140
public var extractions : [ Extraction ] {
@@ -156,7 +157,7 @@ public struct TestTracer: Tracer {
156
157
157
158
// MARK: - ID Generator
158
159
159
- extension TestTracer {
160
+ extension InMemoryTracer {
160
161
public struct IDGenerator : Sendable {
161
162
public let nextTraceID : @Sendable ( ) -> String
162
163
public let nextSpanID : @Sendable ( ) -> String
0 commit comments