Skip to content

Commit e5a71f9

Browse files
committed
rename tracer types -> InMemory...
<!-- After your change, what will change. -->
1 parent b80efc1 commit e5a71f9

File tree

5 files changed

+107
-102
lines changed

5 files changed

+107
-102
lines changed

Package.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let package = Package(
66
products: [
77
.library(name: "Instrumentation", targets: ["Instrumentation"]),
88
.library(name: "Tracing", targets: ["Tracing"]),
9-
.library(name: "TracingTestKit", targets: ["TracingTestKit"]),
9+
.library(name: "InMemoryTracing", targets: ["InMemoryTracing"]),
1010
],
1111
dependencies: [
1212
.package(url: "https://github.com/apple/swift-service-context.git", from: "1.1.0")
@@ -46,15 +46,15 @@ let package = Package(
4646
]
4747
),
4848
.target(
49-
name: "TracingTestKit",
49+
name: "InMemoryTracing",
5050
dependencies: [
5151
.target(name: "Tracing")
5252
]
5353
),
5454
.testTarget(
55-
name: "TracingTestKitTests",
55+
name: "InMemoryTracingTests",
5656
dependencies: [
57-
.target(name: "TracingTestKit")
57+
.target(name: "InMemoryTracing")
5858
]
5959
),
6060

Sources/TracingTestKit/TestSpan.swift renamed to Sources/InMemoryTracing/InMemorySpan.swift

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,30 @@
1515
@_spi(Locking) import Instrumentation
1616
import Tracing
1717

18-
public struct TestSpan: Span {
18+
/// A ``Span`` created by the ``InMemoryTracer`` that will be retained in memory when ended.
19+
/// See ``InMemoryTracer/
20+
public struct InMemorySpan: Span {
1921
public let context: ServiceContext
20-
public let spanContext: TestSpanContext
22+
public let spanContext: InMemorySpanContext
2123
public let kind: SpanKind
2224
public let startInstant: any TracerInstant
2325

24-
init(
26+
private let _operationName: LockedValueBox<String>
27+
private let _attributes = LockedValueBox<SpanAttributes>([:])
28+
private let _events = LockedValueBox<[SpanEvent]>([])
29+
private let _links = LockedValueBox<[SpanLink]>([])
30+
private let _errors = LockedValueBox<[RecordedError]>([])
31+
private let _status = LockedValueBox<SpanStatus?>(nil)
32+
private let _isRecording = LockedValueBox<Bool>(true)
33+
private let onEnd: @Sendable (FinishedInMemorySpan) -> Void
34+
35+
public init(
2536
operationName: String,
2637
context: ServiceContext,
27-
spanContext: TestSpanContext,
38+
spanContext: InMemorySpanContext,
2839
kind: SpanKind,
2940
startInstant: any TracerInstant,
30-
onEnd: @escaping @Sendable (FinishedTestSpan) -> Void
41+
onEnd: @escaping @Sendable (FinishedInMemorySpan) -> Void
3142
) {
3243
self._operationName = LockedValueBox(operationName)
3344
self.context = context
@@ -105,7 +116,7 @@ public struct TestSpan: Span {
105116

106117
public func end(at instant: @autoclosure () -> some TracerInstant) {
107118
assertIsRecording()
108-
let finishedSpan = FinishedTestSpan(
119+
let finishedSpan = FinishedInMemorySpan(
109120
operationName: operationName,
110121
context: context,
111122
kind: kind,
@@ -128,15 +139,6 @@ public struct TestSpan: Span {
128139
public let instant: any TracerInstant
129140
}
130141

131-
private let _operationName: LockedValueBox<String>
132-
private let _attributes = LockedValueBox<SpanAttributes>([:])
133-
private let _events = LockedValueBox<[SpanEvent]>([])
134-
private let _links = LockedValueBox<[SpanLink]>([])
135-
private let _errors = LockedValueBox<[RecordedError]>([])
136-
private let _status = LockedValueBox<SpanStatus?>(nil)
137-
private let _isRecording = LockedValueBox<Bool>(true)
138-
private let onEnd: @Sendable (FinishedTestSpan) -> Void
139-
140142
private func assertIsRecording(
141143
file: StaticString = #file,
142144
line: UInt = #line
@@ -150,16 +152,18 @@ public struct TestSpan: Span {
150152
}
151153
}
152154

153-
public struct FinishedTestSpan: Sendable {
154-
public let operationName: String
155-
public let context: ServiceContext
156-
public let kind: SpanKind
157-
public let spanContext: TestSpanContext
158-
public let startInstant: any TracerInstant
159-
public let endInstant: any TracerInstant
160-
public let attributes: SpanAttributes
161-
public let events: [SpanEvent]
162-
public let links: [SpanLink]
163-
public let errors: [TestSpan.RecordedError]
164-
public let status: SpanStatus?
155+
/// Represents a finished span (a ``Span`` that `end()` was called on)
156+
/// that was recorded by the ``InMemoryTracer``.
157+
public struct FinishedInMemorySpan: Sendable {
158+
public var operationName: String
159+
public var context: ServiceContext
160+
public var kind: SpanKind
161+
public var spanContext: InMemorySpanContext
162+
public var startInstant: any TracerInstant
163+
public var endInstant: any TracerInstant
164+
public var attributes: SpanAttributes
165+
public var events: [SpanEvent]
166+
public var links: [SpanLink]
167+
public var errors: [InMemorySpan.RecordedError]
168+
public var status: SpanStatus?
165169
}

Sources/TracingTestKit/TestSpanContext.swift renamed to Sources/InMemoryTracing/InMemorySpanContext.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import ServiceContextModule
1616

17-
public struct TestSpanContext: Sendable, Hashable {
17+
public struct InMemorySpanContext: Sendable, Hashable {
1818
public let traceID: String
1919
public let spanID: String
2020
public let parentSpanID: String?
@@ -27,16 +27,16 @@ public struct TestSpanContext: Sendable, Hashable {
2727
}
2828

2929
extension ServiceContext {
30-
var testSpanContext: TestSpanContext? {
30+
var inMemorySpanContext: InMemorySpanContext? {
3131
get {
32-
self[TestSpanContextKey.self]
32+
self[InMemorySpanContextKey.self]
3333
}
3434
set {
35-
self[TestSpanContextKey.self] = newValue
35+
self[InMemorySpanContextKey.self] = newValue
3636
}
3737
}
3838
}
3939

40-
private struct TestSpanContextKey: ServiceContextKey {
41-
typealias Value = TestSpanContext
40+
struct InMemorySpanContextKey: ServiceContextKey {
41+
typealias Value = InMemorySpanContext
4242
}

Sources/TracingTestKit/TestTracer.swift renamed to Sources/InMemoryTracing/InMemoryTracer.swift

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

18-
public struct TestTracer: Tracer {
18+
public struct InMemoryTracer: Tracer {
19+
20+
public static let traceIDKey = "test-trace-id"
21+
public static let spanIDKey = "test-span-id"
22+
1923
public let idGenerator: IDGenerator
2024

25+
private let _activeSpans = LockedValueBox<[InMemorySpanContext: InMemorySpan]>([:])
26+
private let _finishedSpans = LockedValueBox<[FinishedInMemorySpan]>([])
27+
private let _numberOfForceFlushes = LockedValueBox<Int>(0)
28+
2129
public init(idGenerator: IDGenerator = .incrementing) {
2230
self.idGenerator = idGenerator
2331
}
@@ -32,30 +40,30 @@ public struct TestTracer: Tracer {
3240
function: String,
3341
file fileID: String,
3442
line: UInt
35-
) -> TestSpan where Instant: TracerInstant {
43+
) -> InMemorySpan where Instant: TracerInstant {
3644
let parentContext = context()
37-
let spanContext: TestSpanContext
45+
let spanContext: InMemorySpanContext
3846

39-
if let parentSpanContext = parentContext.testSpanContext {
47+
if let parentSpanContext = parentContext.inMemorySpanContext {
4048
// child span
41-
spanContext = TestSpanContext(
49+
spanContext = InMemorySpanContext(
4250
traceID: parentSpanContext.traceID,
4351
spanID: idGenerator.nextSpanID(),
4452
parentSpanID: parentSpanContext.spanID
4553
)
4654
} else {
4755
// root span
48-
spanContext = TestSpanContext(
56+
spanContext = InMemorySpanContext(
4957
traceID: idGenerator.nextTraceID(),
5058
spanID: idGenerator.nextSpanID(),
5159
parentSpanID: nil
5260
)
5361
}
5462

5563
var context = parentContext
56-
context.testSpanContext = spanContext
64+
context.inMemorySpanContext = spanContext
5765

58-
let span = TestSpan(
66+
let span = InMemorySpan(
5967
operationName: operationName,
6068
context: context,
6169
spanContext: spanContext,
@@ -69,8 +77,8 @@ public struct TestTracer: Tracer {
6977
return span
7078
}
7179

72-
public func activeSpan(identifiedBy context: ServiceContext) -> TestSpan? {
73-
guard let spanContext = context.testSpanContext else { return nil }
80+
public func activeSpan(identifiedBy context: ServiceContext) -> InMemorySpan? {
81+
guard let spanContext = context.inMemorySpanContext else { return nil }
7482
return _activeSpans.withValue { $0[spanContext] }
7583
}
7684

@@ -82,27 +90,20 @@ public struct TestTracer: Tracer {
8290
_numberOfForceFlushes.withValue { $0 }
8391
}
8492

85-
public var finishedSpans: [FinishedTestSpan] {
93+
public var finishedSpans: [FinishedInMemorySpan] {
8694
_finishedSpans.withValue { $0 }
8795
}
8896

89-
private let _activeSpans = LockedValueBox<[TestSpanContext: TestSpan]>([:])
90-
private let _finishedSpans = LockedValueBox<[FinishedTestSpan]>([])
91-
private let _numberOfForceFlushes = LockedValueBox<Int>(0)
92-
9397
// MARK: - Instrument
9498

95-
public static let traceIDKey = "test-trace-id"
96-
public static let spanIDKey = "test-span-id"
97-
9899
public func inject<Carrier, Inject: Injector>(
99100
_ context: ServiceContext,
100101
into carrier: inout Carrier,
101102
using injector: Inject
102103
) where Carrier == Inject.Carrier {
103104
var values = [String: String]()
104105

105-
if let spanContext = context.testSpanContext {
106+
if let spanContext = context.inMemorySpanContext {
106107
injector.inject(spanContext.traceID, forKey: Self.traceIDKey, into: &carrier)
107108
values[Self.traceIDKey] = spanContext.traceID
108109
injector.inject(spanContext.spanID, forKey: Self.spanIDKey, into: &carrier)
@@ -133,7 +134,7 @@ public struct TestTracer: Tracer {
133134
return
134135
}
135136

136-
context.testSpanContext = TestSpanContext(traceID: traceID, spanID: spanID, parentSpanID: nil)
137+
context.inMemorySpanContext = InMemorySpanContext(traceID: traceID, spanID: spanID, parentSpanID: nil)
137138
}
138139

139140
public var extractions: [Extraction] {
@@ -156,7 +157,7 @@ public struct TestTracer: Tracer {
156157

157158
// MARK: - ID Generator
158159

159-
extension TestTracer {
160+
extension InMemoryTracer {
160161
public struct IDGenerator: Sendable {
161162
public let nextTraceID: @Sendable () -> String
162163
public let nextSpanID: @Sendable () -> String

0 commit comments

Comments
 (0)