Skip to content

Commit d9c3031

Browse files
heckjktoso
andauthored
Organizing documentation, cleanup and polish (#185)
Co-authored-by: Konrad `ktoso` Malawski <[email protected]> Co-authored-by: Konrad `ktoso` Malawski <[email protected]>
1 parent 0a22e4f commit d9c3031

37 files changed

+1133
-381
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# ``InMemoryTracing``
2+
3+
In-memory implementations of tracers to provide test-focused or other programmatic access to spans.
4+
5+
## Topics
6+
7+
### In-memory implementations
8+
9+
- ``InMemoryTracer``
10+
- ``InMemorySpan``
11+
- ``InMemorySpanContext``
12+
- ``FinishedInMemorySpan``

Sources/InMemoryTracing/InMemorySpan.swift

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
@_spi(Locking) import Instrumentation
1616
import Tracing
1717

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``.
2021
public struct InMemorySpan: Span {
2122

23+
/// The service context of the span.
2224
public let context: ServiceContext
25+
/// The in-memory span context.
2326
public var spanContext: InMemorySpanContext {
2427
context.inMemorySpanContext!
2528
}
@@ -33,12 +36,15 @@ public struct InMemorySpan: Span {
3336
spanContext.spanID
3437
}
3538
/// 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.
3741
public var parentSpanID: String? {
3842
spanContext.parentSpanID
3943
}
4044

45+
/// The kind of span
4146
public let kind: SpanKind
47+
/// The time instant the span started.
4248
public let startInstant: any TracerInstant
4349

4450
private let _operationName: LockedValueBox<String>
@@ -50,6 +56,14 @@ public struct InMemorySpan: Span {
5056
private let _isRecording = LockedValueBox<Bool>(true)
5157
private let onEnd: @Sendable (FinishedInMemorySpan) -> Void
5258

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.
5367
public init(
5468
operationName: String,
5569
context: ServiceContext,
@@ -67,12 +81,15 @@ public struct InMemorySpan: Span {
6781
self.onEnd = onEnd
6882
}
6983

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.
7288
public var isRecording: Bool {
7389
_isRecording.withValue { $0 }
7490
}
7591

92+
/// The operation name the span represents.
7693
public var operationName: String {
7794
get {
7895
_operationName.withValue { $0 }
@@ -83,6 +100,7 @@ public struct InMemorySpan: Span {
83100
}
84101
}
85102

103+
/// The span attributes.
86104
public var attributes: SpanAttributes {
87105
get {
88106
_attributes.withValue { $0 }
@@ -93,28 +111,40 @@ public struct InMemorySpan: Span {
93111
}
94112
}
95113

114+
/// The events associated with the span.
96115
public var events: [SpanEvent] {
97116
_events.withValue { $0 }
98117
}
99118

119+
/// Adds an event you provide to the span.
120+
/// - Parameter event: The event to record.
100121
public func addEvent(_ event: SpanEvent) {
101122
guard isRecording else { return }
102123
_events.withValue { $0.append(event) }
103124
}
104125

126+
/// The span links.
105127
public var links: [SpanLink] {
106128
_links.withValue { $0 }
107129
}
108130

131+
/// Adds a link to the span.
132+
/// - Parameter link: The link to add.
109133
public func addLink(_ link: SpanLink) {
110134
guard isRecording else { return }
111135
_links.withValue { $0.append(link) }
112136
}
113137

138+
/// The errors recorded by the span.
114139
public var errors: [RecordedError] {
115140
_errors.withValue { $0 }
116141
}
117142

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.
118148
public func recordError(
119149
_ error: any Error,
120150
attributes: SpanAttributes,
@@ -126,15 +156,20 @@ public struct InMemorySpan: Span {
126156
}
127157
}
128158

159+
/// The status of the span.
129160
public var status: SpanStatus? {
130161
_status.withValue { $0 }
131162
}
132163

164+
/// Updates the status of the span to the value you provide.
165+
/// - Parameter status: The status to set.
133166
public func setStatus(_ status: SpanStatus) {
134167
guard isRecording else { return }
135168
_status.withValue { $0 = status }
136169
}
137170

171+
/// Finishes the span.
172+
/// - Parameter instant: the time instant the span completed.
138173
public func end(at instant: @autoclosure () -> some TracerInstant) {
139174
let shouldRecord = _isRecording.withValue {
140175
let value = $0
@@ -158,19 +193,25 @@ public struct InMemorySpan: Span {
158193
onEnd(finishedSpan)
159194
}
160195

196+
/// An error recorded to a span.
161197
public struct RecordedError: Sendable {
198+
/// The recorded error.
162199
public let error: Error
200+
/// The span attributes associated with the error.
163201
public let attributes: SpanAttributes
202+
/// The time instant the error occured.
164203
public let instant: any TracerInstant
165204
}
166205
}
167206

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.
170208
public struct FinishedInMemorySpan: Sendable {
209+
/// The name of the operation the span represents.
171210
public var operationName: String
172211

212+
/// The service context of the finished span.
173213
public var context: ServiceContext
214+
/// The in-memory span context.
174215
public var spanContext: InMemorySpanContext {
175216
get {
176217
context.inMemorySpanContext!
@@ -189,7 +230,7 @@ public struct FinishedInMemorySpan: Sendable {
189230
spanContext.spanID = newValue
190231
}
191232
}
192-
/// The ID of this concrete span.
233+
/// The ID of this span.
193234
public var spanID: String {
194235
get {
195236
spanContext.spanID
@@ -199,7 +240,8 @@ public struct FinishedInMemorySpan: Sendable {
199240
}
200241
}
201242
/// 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.
203245
public var parentSpanID: String? {
204246
get {
205247
spanContext.parentSpanID
@@ -209,12 +251,20 @@ public struct FinishedInMemorySpan: Sendable {
209251
}
210252
}
211253

254+
/// The kind of span.
212255
public var kind: SpanKind
256+
/// The time instant the span started.
213257
public var startInstant: any TracerInstant
258+
/// The time instant the span ended.
214259
public var endInstant: any TracerInstant
260+
/// The span attributes.
215261
public var attributes: SpanAttributes
262+
/// A list of events recorded to the span.
216263
public var events: [SpanEvent]
264+
/// A list of links added to the span.
217265
public var links: [SpanLink]
266+
/// A list of errors recorded to the span.
218267
public var errors: [InMemorySpan.RecordedError]
268+
/// The span status.
219269
public var status: SpanStatus?
220270
}

Sources/InMemoryTracing/InMemorySpanContext.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@
1414

1515
import ServiceContextModule
1616

17-
/// Encapsulates the `traceID`, `spanID` and `parentSpanID` of an `InMemorySpan`.
17+
/// A type that encapsulates the trace ID, span ID, and parent span ID of an in-memory span.
18+
///
1819
/// Generally used through the `ServiceContext/inMemorySpanContext` task local value.
1920
public struct InMemorySpanContext: Sendable, Hashable {
20-
/// Idenfifier of top-level trace of which this span is a part of.
21+
/// The idenfifier of top-level trace of which this span is a part of.
2122
public var traceID: String
2223

23-
/// Identifier of this specific span.
24+
/// The identifier of this span.
2425
public var spanID: String
2526

26-
// Identifier of the parent of this span, if any.
27+
/// The Identifier of the parent of this span, if any.
2728
public var parentSpanID: String?
2829

30+
/// Creates a new in-memory span context.
31+
/// - Parameters:
32+
/// - traceID: The trace ID for the context.
33+
/// - spanID: The span ID for the context.
34+
/// - parentSpanID: The context's parent span ID.
2935
public init(traceID: String, spanID: String, parentSpanID: String?) {
3036
self.traceID = traceID
3137
self.spanID = spanID
@@ -34,7 +40,7 @@ public struct InMemorySpanContext: Sendable, Hashable {
3440
}
3541

3642
extension ServiceContext {
37-
/// Task-local value representing the current tracing ``Span`` as set by the ``InMemoryTracer``.
43+
/// A task-local value that represents the current tracing span as set by the in-memory tracer.
3844
public var inMemorySpanContext: InMemorySpanContext? {
3945
get {
4046
self[InMemorySpanContextKey.self]

0 commit comments

Comments
 (0)