Skip to content

Commit

Permalink
fix API, always require Baggage, people should pass .topLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
ktoso committed Aug 19, 2022
1 parent 9e8edfb commit deb480c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Sources/Tracing/Tracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ extension Tracer {
/// - Throws: the error the `operation` has thrown (if any)
public func withSpan<T>(
_ operationName: String,
baggage: Baggage?,
baggage: Baggage,
ofKind kind: SpanKind = .internal,
_ operation: (Span) async throws -> T
) async rethrows -> T {
let span = self.startSpan(operationName, baggage: baggage ?? .topLevel, ofKind: kind)
let span = self.startSpan(operationName, baggage: baggage, ofKind: kind)
defer { span.end() }
do {
return try await Baggage.$current.withValue(span.baggage) {
Expand Down
1 change: 1 addition & 0 deletions Tests/TracingTests/TracerTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extension TracerTests {
("testWithSpan_automaticBaggagePropagation_sync", testWithSpan_automaticBaggagePropagation_sync),
("testWithSpan_automaticBaggagePropagation_sync_throws", testWithSpan_automaticBaggagePropagation_sync_throws),
("testWithSpan_automaticBaggagePropagation_async", testWithSpan_automaticBaggagePropagation_async),
("testWithSpan_enterFromNonAsyncCode_passBaggage_asyncOperation", testWithSpan_enterFromNonAsyncCode_passBaggage_asyncOperation),
("testWithSpan_automaticBaggagePropagation_async_throws", testWithSpan_automaticBaggagePropagation_async_throws),
]
}
Expand Down
22 changes: 12 additions & 10 deletions Tests/TracingTests/TracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ final class TracerTests: XCTestCase {
}


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

var fromNonAsyncWorld = Baggage.topLevel
fromNonAsyncWorld.traceID = "1234-5678"
let value = await tracer.withSpan("hello", baggage: fromNonAsyncWorld) { (span: Span) -> String in
XCTAssertEqual(span.baggage.traceID, Baggage.current?.traceID)
XCTAssertEqual(span.baggage.traceID, fromNonAsyncWorld.traceID)
return await operation(span: span)
}
self.testAsync {
var fromNonAsyncWorld = Baggage.topLevel
fromNonAsyncWorld.traceID = "1234-5678"
let value = await tracer.withSpan("hello", baggage: fromNonAsyncWorld) { (span: Span) -> String in
XCTAssertEqual(span.baggage.traceID, Baggage.current?.traceID)
XCTAssertEqual(span.baggage.traceID, fromNonAsyncWorld.traceID)
return await operation(span: span)
}

XCTAssertEqual(value, "world")
XCTAssertTrue(spanEnded)
XCTAssertEqual(value, "world")
XCTAssertTrue(spanEnded)
}
#endif
}

Expand Down

0 comments on commit deb480c

Please sign in to comment.