15
15
@_spi ( Locking) import Instrumentation
16
16
import Tracing
17
17
18
- /// A ``Span`` created by the ``InMemoryTracer`` that will be retained in memory when ended.
19
- /// See ``InMemoryTracer/
18
+ /// A span created by the in-memory tracer that is retained in memory when the trace ends.
19
+ ///
20
+ /// An `InMemorySpan` is created by a ``InMemoryTracer``.
20
21
public struct InMemorySpan : Span {
21
22
23
+ /// The service context of the span.
22
24
public let context : ServiceContext
25
+ /// The in-memory span context.
23
26
public var spanContext : InMemorySpanContext {
24
27
context. inMemorySpanContext!
25
28
}
@@ -33,12 +36,15 @@ public struct InMemorySpan: Span {
33
36
spanContext. spanID
34
37
}
35
38
/// The ID of the parent span of this span, if there was any.
36
- /// When this is `nil` it means this is the top-level span of this trace.
39
+ ///
40
+ /// When `nil`, this is the top-level span of this trace.
37
41
public var parentSpanID : String ? {
38
42
spanContext. parentSpanID
39
43
}
40
44
45
+ /// The kind of span
41
46
public let kind : SpanKind
47
+ /// The time instant the span started.
42
48
public let startInstant : any TracerInstant
43
49
44
50
private let _operationName : LockedValueBox < String >
@@ -50,6 +56,14 @@ public struct InMemorySpan: Span {
50
56
private let _isRecording = LockedValueBox < Bool > ( true )
51
57
private let onEnd : @Sendable ( FinishedInMemorySpan ) -> Void
52
58
59
+ /// Creates a new in-memory span
60
+ /// - Parameters:
61
+ /// - operationName: The operation name this span represents.
62
+ /// - context: The service context for the span.
63
+ /// - spanContext: The in-memory span context.
64
+ /// - kind: The kind of span.
65
+ /// - startInstant: The time instant the span started.
66
+ /// - onEnd: A closure invoked when the span completes, providing access to the finished span.
53
67
public init (
54
68
operationName: String ,
55
69
context: ServiceContext ,
@@ -67,12 +81,15 @@ public struct InMemorySpan: Span {
67
81
self . onEnd = onEnd
68
82
}
69
83
70
- /// The in memory span stops recording (storing mutations performed on the span) when it is ended.
71
- /// In other words, a finished span no longer is mutable and will ignore all subsequent attempts to mutate.
84
+ /// A Boolean value that indicates whether the span is still recording mutations.
85
+ ///
86
+ /// The in memory span stops recording mutations performed on the span when it is ended.
87
+ /// In other words, a finished span is not mutable and ignores all subsequent attempts to mutate.
72
88
public var isRecording : Bool {
73
89
_isRecording. withValue { $0 }
74
90
}
75
91
92
+ /// The operation name the span represents.
76
93
public var operationName : String {
77
94
get {
78
95
_operationName. withValue { $0 }
@@ -83,6 +100,7 @@ public struct InMemorySpan: Span {
83
100
}
84
101
}
85
102
103
+ /// The span attributes.
86
104
public var attributes : SpanAttributes {
87
105
get {
88
106
_attributes. withValue { $0 }
@@ -93,28 +111,40 @@ public struct InMemorySpan: Span {
93
111
}
94
112
}
95
113
114
+ /// The events associated with the span.
96
115
public var events : [ SpanEvent ] {
97
116
_events. withValue { $0 }
98
117
}
99
118
119
+ /// Adds an event you provide to the span.
120
+ /// - Parameter event: The event to record.
100
121
public func addEvent( _ event: SpanEvent ) {
101
122
guard isRecording else { return }
102
123
_events. withValue { $0. append ( event) }
103
124
}
104
125
126
+ /// The span links.
105
127
public var links : [ SpanLink ] {
106
128
_links. withValue { $0 }
107
129
}
108
130
131
+ /// Adds a link to the span.
132
+ /// - Parameter link: The link to add.
109
133
public func addLink( _ link: SpanLink ) {
110
134
guard isRecording else { return }
111
135
_links. withValue { $0. append ( link) }
112
136
}
113
137
138
+ /// The errors recorded by the span.
114
139
public var errors : [ RecordedError ] {
115
140
_errors. withValue { $0 }
116
141
}
117
142
143
+ /// Records an error to the span.
144
+ /// - Parameters:
145
+ /// - error: The error to record.
146
+ /// - attributes: Span attributes associated with the error.
147
+ /// - instant: The time instant of the error.
118
148
public func recordError(
119
149
_ error: any Error ,
120
150
attributes: SpanAttributes ,
@@ -126,15 +156,20 @@ public struct InMemorySpan: Span {
126
156
}
127
157
}
128
158
159
+ /// The status of the span.
129
160
public var status : SpanStatus ? {
130
161
_status. withValue { $0 }
131
162
}
132
163
164
+ /// Updates the status of the span to the value you provide.
165
+ /// - Parameter status: The status to set.
133
166
public func setStatus( _ status: SpanStatus ) {
134
167
guard isRecording else { return }
135
168
_status. withValue { $0 = status }
136
169
}
137
170
171
+ /// Finishes the span.
172
+ /// - Parameter instant: the time instant the span completed.
138
173
public func end( at instant: @autoclosure ( ) -> some TracerInstant ) {
139
174
let shouldRecord = _isRecording. withValue {
140
175
let value = $0
@@ -158,19 +193,25 @@ public struct InMemorySpan: Span {
158
193
onEnd ( finishedSpan)
159
194
}
160
195
196
+ /// An error recorded to a span.
161
197
public struct RecordedError : Sendable {
198
+ /// The recorded error.
162
199
public let error : Error
200
+ /// The span attributes associated with the error.
163
201
public let attributes : SpanAttributes
202
+ /// The time instant the error occured.
164
203
public let instant : any TracerInstant
165
204
}
166
205
}
167
206
168
- /// Represents a finished span (a ``Span`` that `end()` was called on)
169
- /// that was recorded by the ``InMemoryTracer``.
207
+ /// A type that represents a completed span recorded by the in-memory tracer.
170
208
public struct FinishedInMemorySpan : Sendable {
209
+ /// The name of the operation the span represents.
171
210
public var operationName : String
172
211
212
+ /// The service context of the finished span.
173
213
public var context : ServiceContext
214
+ /// The in-memory span context.
174
215
public var spanContext : InMemorySpanContext {
175
216
get {
176
217
context. inMemorySpanContext!
@@ -189,7 +230,7 @@ public struct FinishedInMemorySpan: Sendable {
189
230
spanContext. spanID = newValue
190
231
}
191
232
}
192
- /// The ID of this concrete span.
233
+ /// The ID of this span.
193
234
public var spanID : String {
194
235
get {
195
236
spanContext. spanID
@@ -199,7 +240,8 @@ public struct FinishedInMemorySpan: Sendable {
199
240
}
200
241
}
201
242
/// The ID of the parent span of this span, if there was any.
202
- /// When this is `nil` it means this is the top-level span of this trace.
243
+ ///
244
+ /// When `nil`, this is the top-level span of this trace.
203
245
public var parentSpanID : String ? {
204
246
get {
205
247
spanContext. parentSpanID
@@ -209,12 +251,20 @@ public struct FinishedInMemorySpan: Sendable {
209
251
}
210
252
}
211
253
254
+ /// The kind of span.
212
255
public var kind : SpanKind
256
+ /// The time instant the span started.
213
257
public var startInstant : any TracerInstant
258
+ /// The time instant the span ended.
214
259
public var endInstant : any TracerInstant
260
+ /// The span attributes.
215
261
public var attributes : SpanAttributes
262
+ /// A list of events recorded to the span.
216
263
public var events : [ SpanEvent ]
264
+ /// A list of links added to the span.
217
265
public var links : [ SpanLink ]
266
+ /// A list of errors recorded to the span.
218
267
public var errors : [ InMemorySpan . RecordedError ]
268
+ /// The span status.
219
269
public var status : SpanStatus ?
220
270
}
0 commit comments