Skip to content

Commit 8ed9307

Browse files
committed
compat fixes
1 parent 12d7326 commit 8ed9307

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let package = Package(
88
.library(name: "Tracing", targets: ["Tracing"]),
99
],
1010
dependencies: [
11-
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")),
11+
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")),
1212
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
1313
],
1414
targets: [

Sources/Tracing/Tracer.swift

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ public protocol Tracer: Instrument {
5353
}
5454

5555
extension Tracer {
56-
/// Start a new ``Span`` with the given `Baggage` starting at `DispatchWallTime.now()`.
56+
57+
58+
#if swift(>=5.3.0)
59+
/// Start a new ``Span`` with the given `Baggage` starting "now".
5760
///
5861
/// - Parameters:
5962
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
@@ -80,12 +83,43 @@ extension Tracer {
8083
line: line
8184
)
8285
}
86+
#else
87+
/// Start a new ``Span`` with the given `Baggage` starting "now".
88+
///
89+
/// - Parameters:
90+
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
91+
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
92+
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
93+
/// - function: The function name in which the span was started
94+
/// - file: The `file` where the span was started.
95+
/// - line: The file line where the span was started.
96+
public func startSpan(
97+
_ operationName: String,
98+
baggage: Baggage,
99+
ofKind kind: SpanKind = .internal,
100+
function: String = #function,
101+
file: String = #file,
102+
line: UInt = #line
103+
) -> Span {
104+
self.startSpan(
105+
operationName,
106+
baggage: baggage,
107+
ofKind: kind,
108+
at: .now(),
109+
function: function,
110+
file: file,
111+
line: line
112+
)
113+
}
114+
#endif
115+
83116
}
84117

85118
// ==== ----------------------------------------------------------------------------------------------------------------
86119
// MARK: Starting spans: `withSpan`
87120

88121
extension Tracer {
122+
#if swift(>=5.3.0)
89123
/// Execute a specific task within a newly created ``Span``.
90124
///
91125
/// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns.
@@ -126,6 +160,48 @@ extension Tracer {
126160
throw error // rethrow
127161
}
128162
}
163+
#else
164+
/// Execute a specific task within a newly created ``Span``.
165+
///
166+
/// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns.
167+
///
168+
/// - Parameters:
169+
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
170+
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
171+
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
172+
/// - operation: operation to wrap in a span start/end and execute immediately
173+
/// - function: The function name in which the span was started
174+
/// - file: The `#file` where the span was started.
175+
/// - line: The file line where the span was started.
176+
/// - Returns: the value returned by `operation`
177+
/// - Throws: the error the `operation` has thrown (if any)
178+
public func withSpan<T>(
179+
_ operationName: String,
180+
baggage: Baggage,
181+
ofKind kind: SpanKind = .internal,
182+
function: String = #function,
183+
file: String = #file,
184+
line: UInt = #line,
185+
_ operation: (Span) throws -> T
186+
) rethrows -> T {
187+
let span = self.startSpan(
188+
operationName,
189+
baggage: baggage,
190+
ofKind: kind,
191+
at: .now(),
192+
function: function,
193+
file: file,
194+
line: line
195+
)
196+
defer { span.end() }
197+
do {
198+
return try operation(span)
199+
} catch {
200+
span.recordError(error)
201+
throw error // rethrow
202+
}
203+
}
204+
#endif
129205
}
130206

131207
// ==== ----------------------------------------------------------------------------------------------------------------
@@ -202,7 +278,7 @@ extension Tracer {
202278
)
203279
defer { span.end() }
204280
do {
205-
return try await Baggage.$current.withValue(span.baggage) {
281+
return try await Baggage.withValue(span.baggage) {
206282
try await operation(span)
207283
}
208284
} catch {
@@ -239,7 +315,7 @@ extension Tracer {
239315
let span = self.startSpan(operationName, baggage: baggage, ofKind: kind, function: function, file: fileID, line: line)
240316
defer { span.end() }
241317
do {
242-
return try await Baggage.$current.withValue(span.baggage) {
318+
return try await Baggage.withValue(span.baggage) {
243319
try await operation(span)
244320
}
245321
} catch {

Tests/TracingTests/DynamicTracepointTracerTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final class DynamicTracepointTracerTests: XCTestCase {
2424
}
2525

2626
func test_adhoc_enableBySourceLoc() {
27+
#if swift(>=5.5)
2728
let tracer = DynamicTracepointTestTracer()
2829

2930
InstrumentationSystem.bootstrapInternal(tracer)
@@ -55,9 +56,11 @@ final class DynamicTracepointTracerTests: XCTestCase {
5556
}
5657
XCTAssertEqual(tracer.spans[0].baggage.spanID, "span-id-fake-\(fileID)-\(fakeLine)")
5758
XCTAssertEqual(tracer.spans[1].baggage.spanID, "span-id-fake-\(fileID)-\(fakeNextLine)")
59+
#endif
5860
}
5961

6062
func test_adhoc_enableByFunction() {
63+
#if swift(>=5.5)
6164
let tracer = DynamicTracepointTestTracer()
6265

6366
InstrumentationSystem.bootstrapInternal(tracer)
@@ -80,6 +83,7 @@ final class DynamicTracepointTracerTests: XCTestCase {
8083
}
8184
XCTAssertEqual(tracer.spans[0].baggage.spanID, "span-id-fake-\(fileID)-\(fakeLine)")
8285
XCTAssertEqual(tracer.spans[1].baggage.spanID, "span-id-fake-\(fileID)-\(fakeNextLine)")
86+
#endif
8387
}
8488

8589
func logic(fakeLine: UInt) {
@@ -88,11 +92,13 @@ final class DynamicTracepointTracerTests: XCTestCase {
8892
}
8993

9094
func traceMeLogic(fakeLine: UInt) {
95+
#if swift(>=5.5)
9196
InstrumentationSystem.tracer.withSpan("\(#function)-yes", line: fakeLine) { _ in
9297
InstrumentationSystem.tracer.withSpan("\(#function)-yes-inside", line: fakeLine + 11) { _ in
9398
// inside
9499
}
95100
}
101+
#endif
96102
}
97103
}
98104

0 commit comments

Comments
 (0)