@@ -17,14 +17,14 @@ import Tracing
17
17
18
18
/// An in-memory implementation of the ``Tracer`` protocol which can be used either in testing,
19
19
/// or in manual collecting and interrogating traces within a process, and acting on them programatically.
20
- ///
20
+ ///
21
21
/// ### Span lifecycle
22
22
/// This tracer does _not_ automatically remove spans once they end.
23
23
/// Finished spans are retained and available for inspection using the `finishedSpans` property.
24
- /// Spans which have been started but have not yet been called `Span/end()` on are also available
24
+ /// Spans which have been started but have not yet been called `Span/end()` on are also available
25
25
/// for inspection using the ``activeSpans`` property.
26
- ///
27
- /// Spans are retained by the `InMemoryTracer` until they are explicitly removed, e.g. by using
26
+ ///
27
+ /// Spans are retained by the `InMemoryTracer` until they are explicitly removed, e.g. by using
28
28
/// ``popFinishedSpans()`` or any of the `clear...` methods (e.g. ``clearFinishedSpans()``)
29
29
public struct InMemoryTracer : Tracer {
30
30
@@ -41,15 +41,15 @@ public struct InMemoryTracer: Tracer {
41
41
private let _extractions = LockedValueBox < [ Extraction ] > ( [ ] )
42
42
43
43
/// Create a new ``InMemoryTracer``.
44
- ///
45
- /// - Parameters:
44
+ ///
45
+ /// - Parameters:
46
46
/// - Parameter idGenerator: strategy for generating trace and span identifiers
47
47
/// - Parameter idGenerator: strategy for generating trace and span identifiers
48
48
public init (
49
49
idGenerator: IDGenerator = . incrementing,
50
50
recordInjections: Bool = true ,
51
51
recordExtractions: Bool = true
52
- ) {
52
+ ) {
53
53
self . idGenerator = idGenerator
54
54
self . recordInjections = recordInjections
55
55
self . recordExtractions = recordExtractions
@@ -114,7 +114,7 @@ extension InMemoryTracer {
114
114
115
115
extension InMemoryTracer {
116
116
117
- /// Array of active spans, i.e. spans which have been started by have not yet finished (by calling `Span/end()`).
117
+ /// Array of active spans, i.e. spans which have been started but have not yet finished (by calling `Span/end()`).
118
118
public var activeSpans : [ InMemorySpan ] {
119
119
_activeSpans. withValue { active in Array ( active. values) }
120
120
}
@@ -132,15 +132,15 @@ extension InMemoryTracer {
132
132
}
133
133
134
134
/// Gets, without removing, all the finished spans recorded by this tracer.
135
- ///
135
+ ///
136
136
/// - SeeAlso: `popFinishedSpans()`
137
137
public var finishedSpans : [ FinishedInMemorySpan ] {
138
138
_finishedSpans. withValue { $0 }
139
139
}
140
-
140
+
141
141
/// Returns, and removes, all finished spans recorded by this tracer.
142
142
public func popFinishedSpans( ) -> [ FinishedInMemorySpan ] {
143
- _finishedSpans. withValue { spans in
143
+ _finishedSpans. withValue { spans in
144
144
defer { spans = [ ] }
145
145
return spans
146
146
}
@@ -152,10 +152,13 @@ extension InMemoryTracer {
152
152
}
153
153
154
154
/// Clears all registered finished spans, as well as injections/extractions performed by this tracer.
155
- public func clearAll( ) {
155
+ public func clearAll( includingActive : Bool = false ) {
156
156
_finishedSpans. withValue { $0 = [ ] }
157
157
_injections. withValue { $0 = [ ] }
158
158
_extractions. withValue { $0 = [ ] }
159
+ if includingActive {
160
+ _activeSpans. withValue { $0 = [ : ] }
161
+ }
159
162
}
160
163
}
161
164
@@ -235,13 +238,12 @@ extension InMemoryTracer {
235
238
_extractions. withValue { $0 }
236
239
}
237
240
238
-
239
241
/// Represents a recorded call to the InMemoryTracer's ``Instrument/extract(_:into:using:)`` method.
240
242
public struct Extraction : Sendable {
241
243
/// The carrier object from which the context values were extracted from,
242
244
/// e.g. this frequently is an HTTP request or similar.
243
245
public let carrier : any Sendable
244
- /// The constructed service context, containing the extracted ``ServiceContext/inMemoryTraceContext ``.
246
+ /// The constructed service context, containing the extracted ``ServiceContext/inMemorySpanContext ``.
245
247
public let context : ServiceContext
246
248
}
247
249
}
@@ -251,7 +253,7 @@ extension InMemoryTracer {
251
253
extension InMemoryTracer {
252
254
253
255
/// Can be used to customize how trace and span IDs are generated by the ``InMemoryTracer``.
254
- ///
256
+ ///
255
257
/// Defaults to a simple sequential numeric scheme (`span-1`, `span-2`, `trace-1`, `trace-2` etc).
256
258
public struct IDGenerator : Sendable {
257
259
public let nextTraceID : @Sendable ( ) -> String
0 commit comments