diff --git a/Sources/Tracing/Tracer.swift b/Sources/Tracing/Tracer.swift index 4413f6c..5d944f6 100644 --- a/Sources/Tracing/Tracer.swift +++ b/Sources/Tracing/Tracer.swift @@ -195,7 +195,7 @@ public func withSpan( ) rethrows -> T { try InstrumentationSystem.legacyTracer.withAnySpan( operationName, - at: DefaultTracerClock.now, + at: instant(), context: context(), ofKind: kind, function: function, @@ -339,7 +339,7 @@ public func withSpan( ) async rethrows -> T { try await InstrumentationSystem.legacyTracer.withAnySpan( operationName, - at: DefaultTracerClock.now, + at: instant(), context: context(), ofKind: kind, function: function, diff --git a/Tests/TracingTests/TracerTests.swift b/Tests/TracingTests/TracerTests.swift index 5f0b556..17a2aab 100644 --- a/Tests/TracingTests/TracerTests.swift +++ b/Tests/TracingTests/TracerTests.swift @@ -369,6 +369,22 @@ final class TracerTests: XCTestCase { tracer.withAnySpan("", context: .topLevel) { _ in } } + func testWithSpanShouldNotMissPropagatingInstant() { + let tracer = TestTracer() + InstrumentationSystem.bootstrapInternal(tracer) + defer { + InstrumentationSystem.bootstrapInternal(nil) + } + + let clock = DefaultTracerClock() + + let instant = clock.now + withSpan("span", at: instant) { _ in } + + let span = tracer.spans.first! + XCTAssertEqual(span.startTimestampNanosSinceEpoch, instant.nanosecondsSinceEpoch) + } + #if swift(>=5.7.0) // @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) /// Helper method to execute async operations until we can use async tests (currently incompatible with the generated LinuxMain file).