From 9ffb95efef238db36eeafe463ace4d0113c52e20 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 1 Feb 2024 10:57:10 +0900 Subject: [PATCH 1/2] [PATCH] [bug] Fix ignored `at` argument in two instances of `withSpan` In two cases, the signature for `withSpan` included the `at` parameter to override the starting timestamp on the span, but it was being ignored and `DefaultTracerClock.now` was being used instead. --- Sources/Tracing/Tracer.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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, From 960a14eb9836c2bae4ded1253aa55cb29ad40669 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 1 Feb 2024 11:06:16 +0900 Subject: [PATCH 2/2] add test --- Tests/TracingTests/TracerTests.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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).