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