Skip to content

Commit 71aa61e

Browse files
committed
Make traceID, spanID accessible directly on InMemorySpan
1 parent cccfbe3 commit 71aa61e

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

Sources/InMemoryTracing/InMemorySpan.swift

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,26 @@
1616
import Tracing
1717

1818
/// A ``Span`` created by the ``InMemoryTracer`` that will be retained in memory when ended.
19-
///
20-
/// - SeeAlso: ``InMemoryTracer``
19+
/// See ``InMemoryTracer/
2120
public struct InMemorySpan: Span {
21+
2222
public let context: ServiceContext
2323
public let spanContext: InMemorySpanContext
24+
25+
/// The ID of the overall trace this span belongs to.
26+
public var traceID: String {
27+
spanContext.spanID
28+
}
29+
/// The ID of this concrete span.
30+
public var spanID: String {
31+
spanContext.spanID
32+
}
33+
/// The ID of the parent span of this span, if there was any.
34+
/// When this is `nil` it means this is the top-level span of this trace.
35+
public var parentSpanID: String? {
36+
spanContext.parentSpanID
37+
}
38+
2439
public let kind: SpanKind
2540
public let startInstant: any TracerInstant
2641

@@ -49,6 +64,8 @@ public struct InMemorySpan: Span {
4964
self.onEnd = onEnd
5065
}
5166

67+
/// The in memory span stops recording (storing mutations performed on the span) when it is ended.
68+
/// In other words, a finished span no longer is mutable and will ignore all subsequent attempts to mutate.
5269
public var isRecording: Bool {
5370
_isRecording.withValue { $0 }
5471
}
@@ -120,8 +137,8 @@ public struct InMemorySpan: Span {
120137
let finishedSpan = FinishedInMemorySpan(
121138
operationName: operationName,
122139
context: context,
123-
kind: kind,
124140
spanContext: spanContext,
141+
kind: kind,
125142
startInstant: startInstant,
126143
endInstant: instant(),
127144
attributes: attributes,
@@ -145,9 +162,40 @@ public struct InMemorySpan: Span {
145162
/// that was recorded by the ``InMemoryTracer``.
146163
public struct FinishedInMemorySpan: Sendable {
147164
public var operationName: String
165+
148166
public var context: ServiceContext
149-
public var kind: SpanKind
150167
public var spanContext: InMemorySpanContext
168+
169+
/// The ID of the overall trace this span belongs to.
170+
public var traceID: String {
171+
get {
172+
spanContext.spanID
173+
}
174+
set {
175+
spanContext.spanID = newValue
176+
}
177+
}
178+
/// The ID of this concrete span.
179+
public var spanID: String {
180+
get {
181+
spanContext.spanID
182+
}
183+
set {
184+
spanContext.spanID = newValue
185+
}
186+
}
187+
/// The ID of the parent span of this span, if there was any.
188+
/// When this is `nil` it means this is the top-level span of this trace.
189+
public var parentSpanID: String? {
190+
get {
191+
spanContext.parentSpanID
192+
}
193+
set {
194+
spanContext.parentSpanID = newValue
195+
}
196+
}
197+
198+
public var kind: SpanKind
151199
public var startInstant: any TracerInstant
152200
public var endInstant: any TracerInstant
153201
public var attributes: SpanAttributes

Sources/InMemoryTracing/InMemorySpanContext.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import ServiceContextModule
1818
/// Generally used through the `ServiceContext/inMemorySpanContext` task local value.
1919
public struct InMemorySpanContext: Sendable, Hashable {
2020
/// Idenfifier of top-level trace of which this span is a part of.
21-
public let traceID: String
21+
public var traceID: String
2222

2323
/// Identifier of this specific span.
24-
public let spanID: String
24+
public var spanID: String
2525

2626
// Identifier of the parent of this span, if any.
27-
public let parentSpanID: String?
27+
public var parentSpanID: String?
2828

2929
public init(traceID: String, spanID: String, parentSpanID: String?) {
3030
self.traceID = traceID

Sources/InMemoryTracing/InMemoryTracer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ extension InMemoryTracer {
156156
_finishedSpans.withValue { $0 = [] }
157157
_injections.withValue { $0 = [] }
158158
_extractions.withValue { $0 = [] }
159-
if includingActive {
159+
if includingActive {
160160
_activeSpans.withValue { $0 = [:] }
161161
}
162162
}

0 commit comments

Comments
 (0)