Skip to content

Commit

Permalink
Delegate to withSpan(_ baggage:) from withSpan(_ context:) (#36)
Browse files Browse the repository at this point in the history
Co-authored-by: Konrad `ktoso` Malawski <ktoso@apple.com>
  • Loading branch information
slashmo and ktoso authored Dec 21, 2020
1 parent b50cfc1 commit 73f2b52
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
9 changes: 1 addition & 8 deletions Sources/Tracing/Tracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,7 @@ extension Tracer {
ofKind kind: SpanKind = .internal,
_ function: (Span) throws -> T
) rethrows -> T {
let span = self.startSpan(operationName, context: context, ofKind: kind)
defer { span.end() }
do {
return try function(span)
} catch {
span.recordError(error)
throw error // rethrow
}
return try self.withSpan(operationName, baggage: context.baggage, function)
}

/// Execute a specific task within a newly created `Span`.
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 @@ -29,6 +29,7 @@ extension TracerTests {
("testContextPropagationWithNoOpSpan", testContextPropagationWithNoOpSpan),
("testWithSpan_success", testWithSpan_success),
("testWithSpan_throws", testWithSpan_throws),
("testWithSpan_context", testWithSpan_context),
]
}
}
Expand Down
15 changes: 15 additions & 0 deletions Tests/TracingTests/TracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ final class TracerTests: XCTestCase {
}
XCTFail("Should have throw")
}

func testWithSpan_context() {
let tracer = TestTracer()
InstrumentationSystem.bootstrapInternal(tracer)
defer {
InstrumentationSystem.bootstrapInternal(NoOpTracer())
}

var spanEnded = false
tracer.onEndSpan = { _ in spanEnded = true }

tracer.withSpan("", context: DefaultLoggingContext.topLevel(logger: Logger(label: "test"))) { _ in }

XCTAssertTrue(spanEnded)
}
}

struct ExampleSpanError: Error, Equatable {}
Expand Down

0 comments on commit 73f2b52

Please sign in to comment.