Skip to content

Commit 73f2b52

Browse files
slashmoktoso
andauthored
Delegate to withSpan(_ baggage:) from withSpan(_ context:) (#36)
Co-authored-by: Konrad `ktoso` Malawski <[email protected]>
1 parent b50cfc1 commit 73f2b52

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

Sources/Tracing/Tracer.swift

+1-8
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,7 @@ extension Tracer {
9797
ofKind kind: SpanKind = .internal,
9898
_ function: (Span) throws -> T
9999
) rethrows -> T {
100-
let span = self.startSpan(operationName, context: context, ofKind: kind)
101-
defer { span.end() }
102-
do {
103-
return try function(span)
104-
} catch {
105-
span.recordError(error)
106-
throw error // rethrow
107-
}
100+
return try self.withSpan(operationName, baggage: context.baggage, function)
108101
}
109102

110103
/// Execute a specific task within a newly created `Span`.

Tests/TracingTests/TracerTests+XCTest.swift

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extension TracerTests {
2929
("testContextPropagationWithNoOpSpan", testContextPropagationWithNoOpSpan),
3030
("testWithSpan_success", testWithSpan_success),
3131
("testWithSpan_throws", testWithSpan_throws),
32+
("testWithSpan_context", testWithSpan_context),
3233
]
3334
}
3435
}

Tests/TracingTests/TracerTests.swift

+15
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ final class TracerTests: XCTestCase {
9595
}
9696
XCTFail("Should have throw")
9797
}
98+
99+
func testWithSpan_context() {
100+
let tracer = TestTracer()
101+
InstrumentationSystem.bootstrapInternal(tracer)
102+
defer {
103+
InstrumentationSystem.bootstrapInternal(NoOpTracer())
104+
}
105+
106+
var spanEnded = false
107+
tracer.onEndSpan = { _ in spanEnded = true }
108+
109+
tracer.withSpan("", context: DefaultLoggingContext.topLevel(logger: Logger(label: "test"))) { _ in }
110+
111+
XCTAssertTrue(spanEnded)
112+
}
98113
}
99114

100115
struct ExampleSpanError: Error, Equatable {}

0 commit comments

Comments
 (0)