Skip to content

Commit 03af4c2

Browse files
committed
fix API, always require Baggage, people should pass .topLevel
1 parent 9e8edfb commit 03af4c2

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

Sources/Tracing/Tracer.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ extension Tracer {
166166
/// - Throws: the error the `operation` has thrown (if any)
167167
public func withSpan<T>(
168168
_ operationName: String,
169-
baggage: Baggage?,
169+
baggage: Baggage,
170170
ofKind kind: SpanKind = .internal,
171171
_ operation: (Span) async throws -> T
172172
) async rethrows -> T {
173-
let span = self.startSpan(operationName, baggage: baggage ?? .topLevel, ofKind: kind)
173+
let span = self.startSpan(operationName, baggage: baggage, ofKind: kind)
174174
defer { span.end() }
175175
do {
176176
return try await Baggage.$current.withValue(span.baggage) {

Tests/TracingTests/TracerTests+XCTest.swift

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extension TracerTests {
3333
("testWithSpan_automaticBaggagePropagation_sync", testWithSpan_automaticBaggagePropagation_sync),
3434
("testWithSpan_automaticBaggagePropagation_sync_throws", testWithSpan_automaticBaggagePropagation_sync_throws),
3535
("testWithSpan_automaticBaggagePropagation_async", testWithSpan_automaticBaggagePropagation_async),
36+
("testWithSpan_enterFromNonAsyncCode_passBaggage_asyncOperation", testWithSpan_enterFromNonAsyncCode_passBaggage_asyncOperation),
3637
("testWithSpan_automaticBaggagePropagation_async_throws", testWithSpan_automaticBaggagePropagation_async_throws),
3738
]
3839
}

Tests/TracingTests/TracerTests.swift

+12-11
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ final class TracerTests: XCTestCase {
187187
#endif
188188
}
189189

190-
191-
func testWithSpan_enterFromNonAsyncCode_passBaggage_asyncOperation() async throws {
190+
func testWithSpan_enterFromNonAsyncCode_passBaggage_asyncOperation() throws {
192191
#if swift(>=5.5) && canImport(_Concurrency)
193192
guard #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) else {
194193
throw XCTSkip("Task locals are not supported on this platform.")
@@ -207,16 +206,18 @@ final class TracerTests: XCTestCase {
207206
"world"
208207
}
209208

210-
var fromNonAsyncWorld = Baggage.topLevel
211-
fromNonAsyncWorld.traceID = "1234-5678"
212-
let value = await tracer.withSpan("hello", baggage: fromNonAsyncWorld) { (span: Span) -> String in
213-
XCTAssertEqual(span.baggage.traceID, Baggage.current?.traceID)
214-
XCTAssertEqual(span.baggage.traceID, fromNonAsyncWorld.traceID)
215-
return await operation(span: span)
216-
}
209+
self.testAsync {
210+
var fromNonAsyncWorld = Baggage.topLevel
211+
fromNonAsyncWorld.traceID = "1234-5678"
212+
let value = await tracer.withSpan("hello", baggage: fromNonAsyncWorld) { (span: Span) -> String in
213+
XCTAssertEqual(span.baggage.traceID, Baggage.current?.traceID)
214+
XCTAssertEqual(span.baggage.traceID, fromNonAsyncWorld.traceID)
215+
return await operation(span: span)
216+
}
217217

218-
XCTAssertEqual(value, "world")
219-
XCTAssertTrue(spanEnded)
218+
XCTAssertEqual(value, "world")
219+
XCTAssertTrue(spanEnded)
220+
}
220221
#endif
221222
}
222223

0 commit comments

Comments
 (0)