@@ -33,12 +33,15 @@ public struct InMemoryTracer: Tracer {
33
33
public let recordInjections : Bool
34
34
public let recordExtractions : Bool
35
35
36
- private let _activeSpans = LockedValueBox < [ InMemorySpanContext : InMemorySpan ] > ( [ : ] )
37
- private let _finishedSpans = LockedValueBox < [ FinishedInMemorySpan ] > ( [ ] )
38
- private let _numberOfForceFlushes = LockedValueBox < Int > ( 0 )
36
+ struct State {
37
+ var activeSpans : [ InMemorySpanContext : InMemorySpan ] = [ : ]
38
+ var finishedSpans : [ FinishedInMemorySpan ] = [ ]
39
+ var numberOfForceFlushes : Int = 0
39
40
40
- private let _injections = LockedValueBox < [ Injection ] > ( [ ] )
41
- private let _extractions = LockedValueBox < [ Extraction ] > ( [ ] )
41
+ var injections : [ Injection ] = [ ]
42
+ var extractions : [ Extraction ] = [ ]
43
+ }
44
+ var _state = LockedValueBox < State > ( . init( ) )
42
45
43
46
/// Create a new ``InMemoryTracer``.
44
47
///
@@ -98,15 +101,17 @@ extension InMemoryTracer {
98
101
kind: kind,
99
102
startInstant: instant ( )
100
103
) { finishedSpan in
101
- _activeSpans. withValue { $0 [ spanContext] = nil }
102
- _finishedSpans. withValue { $0. append ( finishedSpan) }
104
+ _state. withValue {
105
+ $0. activeSpans [ spanContext] = nil
106
+ $0. finishedSpans. append ( finishedSpan)
107
+ }
103
108
}
104
- _activeSpans . withValue { $0 [ spanContext] = span }
109
+ _state . withValue { $0. activeSpans [ spanContext] = span }
105
110
return span
106
111
}
107
112
108
113
public func forceFlush( ) {
109
- _numberOfForceFlushes . withValue { $0 += 1 }
114
+ _state . withValue { $0. numberOfForceFlushes += 1 }
110
115
}
111
116
}
112
117
@@ -116,48 +121,50 @@ extension InMemoryTracer {
116
121
117
122
/// Array of active spans, i.e. spans which have been started but have not yet finished (by calling `Span/end()`).
118
123
public var activeSpans : [ InMemorySpan ] {
119
- _activeSpans . withValue { active in Array ( active . values) }
124
+ _state . withValue { Array ( $0 . activeSpans . values) }
120
125
}
121
126
122
127
/// Retrives a specific _active_ span, identified by the specific span, trace, and parent ID's
123
128
/// stored in the `inMemorySpanContext`
124
129
public func activeSpan( identifiedBy context: ServiceContext ) -> InMemorySpan ? {
125
130
guard let spanContext = context. inMemorySpanContext else { return nil }
126
- return _activeSpans . withValue { $0 [ spanContext] }
131
+ return _state . withValue { $0. activeSpans [ spanContext] }
127
132
}
128
133
129
134
/// Count of the number of times ``Tracer/forceFlush()`` was called on this tracer.
130
135
public var numberOfForceFlushes : Int {
131
- _numberOfForceFlushes . withValue { $0 }
136
+ _state . withValue { $0. numberOfForceFlushes }
132
137
}
133
138
134
139
/// Gets, without removing, all the finished spans recorded by this tracer.
135
140
///
136
141
/// - SeeAlso: `popFinishedSpans()`
137
142
public var finishedSpans : [ FinishedInMemorySpan ] {
138
- _finishedSpans . withValue { $0 }
143
+ _state . withValue { $0. finishedSpans }
139
144
}
140
145
141
146
/// Returns, and removes, all finished spans recorded by this tracer.
142
147
public func popFinishedSpans( ) -> [ FinishedInMemorySpan ] {
143
- _finishedSpans . withValue { spans in
144
- defer { spans = [ ] }
145
- return spans
148
+ _state . withValue { state in
149
+ defer { state . finishedSpans = [ ] }
150
+ return state . finishedSpans
146
151
}
147
152
}
148
153
149
154
/// Atomically clears any stored finished spans in this tracer.
150
155
public func clearFinishedSpans( ) {
151
- _finishedSpans . withValue { $0 = [ ] }
156
+ _state . withValue { $0. finishedSpans = [ ] }
152
157
}
153
158
154
159
/// Clears all registered finished spans, as well as injections/extractions performed by this tracer.
155
160
public func clearAll( includingActive: Bool = false ) {
156
- _finishedSpans. withValue { $0 = [ ] }
157
- _injections. withValue { $0 = [ ] }
158
- _extractions. withValue { $0 = [ ] }
159
- if includingActive {
160
- _activeSpans. withValue { $0 = [ : ] }
161
+ _state. withValue {
162
+ $0. finishedSpans = [ ]
163
+ $0. injections = [ ]
164
+ $0. extractions = [ ]
165
+ if includingActive {
166
+ $0. activeSpans = [ : ]
167
+ }
161
168
}
162
169
}
163
170
}
@@ -185,19 +192,19 @@ extension InMemoryTracer {
185
192
186
193
if recordInjections {
187
194
let injection = Injection ( context: context, values: values)
188
- _injections . withValue { $0. append ( injection) }
195
+ _state . withValue { $0. injections . append ( injection) }
189
196
}
190
197
}
191
198
192
199
/// Lists all recorded calls to this tracer's ``Instrument/inject(_:into:using:)`` method.
193
200
/// This may be used to inspect what span identifiers are being propagated by this tracer.
194
201
public var performedContextInjections : [ Injection ] {
195
- _injections . withValue { $0 }
202
+ _state . withValue { $0. injections }
196
203
}
197
204
198
205
/// Clear the list of recorded context injections (calls to ``Instrument/inject(_:into:using:)``).
199
206
public func clearPerformedContextInjections( ) {
200
- _injections . withValue { $0 = [ ] }
207
+ _state . withValue { $0. injections = [ ] }
201
208
}
202
209
203
210
/// Represents a recorded call to the InMemoryTracer's ``Instrument/inject(_:into:using:)`` method.
@@ -219,7 +226,7 @@ extension InMemoryTracer {
219
226
defer {
220
227
if self . recordExtractions {
221
228
let extraction = Extraction ( carrier: carrier, context: context)
222
- _extractions . withValue { $0. append ( extraction) }
229
+ _state . withValue { $0. extractions . append ( extraction) }
223
230
}
224
231
}
225
232
@@ -235,7 +242,7 @@ extension InMemoryTracer {
235
242
/// Lists all recorded calls to this tracer's ``Instrument/extract(_:into:using:)`` method.
236
243
/// This may be used to inspect what span identifiers were extracted from an incoming carrier object into ``ServiceContext``.
237
244
public var performedContextExtractions : [ Extraction ] {
238
- _extractions . withValue { $0 }
245
+ _state . withValue { $0. extractions }
239
246
}
240
247
241
248
/// Represents a recorded call to the InMemoryTracer's ``Instrument/extract(_:into:using:)`` method.
0 commit comments