Skip to content

Commit b80af1b

Browse files
authored
[PATCH] [bug] Fix ignored at argument in two instances of withSpan (#139)
* [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. * add test
1 parent f7d3e56 commit b80af1b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Sources/Tracing/Tracer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public func withSpan<T, Instant: TracerInstant>(
195195
) rethrows -> T {
196196
try InstrumentationSystem.legacyTracer.withAnySpan(
197197
operationName,
198-
at: DefaultTracerClock.now,
198+
at: instant(),
199199
context: context(),
200200
ofKind: kind,
201201
function: function,
@@ -339,7 +339,7 @@ public func withSpan<T, Instant: TracerInstant>(
339339
) async rethrows -> T {
340340
try await InstrumentationSystem.legacyTracer.withAnySpan(
341341
operationName,
342-
at: DefaultTracerClock.now,
342+
at: instant(),
343343
context: context(),
344344
ofKind: kind,
345345
function: function,

Tests/TracingTests/TracerTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,22 @@ final class TracerTests: XCTestCase {
369369
tracer.withAnySpan("", context: .topLevel) { _ in }
370370
}
371371

372+
func testWithSpanShouldNotMissPropagatingInstant() {
373+
let tracer = TestTracer()
374+
InstrumentationSystem.bootstrapInternal(tracer)
375+
defer {
376+
InstrumentationSystem.bootstrapInternal(nil)
377+
}
378+
379+
let clock = DefaultTracerClock()
380+
381+
let instant = clock.now
382+
withSpan("span", at: instant) { _ in }
383+
384+
let span = tracer.spans.first!
385+
XCTAssertEqual(span.startTimestampNanosSinceEpoch, instant.nanosecondsSinceEpoch)
386+
}
387+
372388
#if swift(>=5.7.0)
373389
// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
374390
/// Helper method to execute async operations until we can use async tests (currently incompatible with the generated LinuxMain file).

0 commit comments

Comments
 (0)