Skip to content

Commit a5b5160

Browse files
authored
Implement TracerClock and abstract away "now" (#98)
1 parent b42cf43 commit a5b5160

30 files changed

+886
-230
lines changed

Sources/Instrumentation/Instrument.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Distributed Tracing open source project
44
//
5-
// Copyright (c) 2020-2022 Apple Inc. and the Swift Distributed Tracing project
5+
// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project
66
// authors
77
// Licensed under Apache License v2.0
88
//

Sources/Instrumentation/InstrumentationSystem.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Distributed Tracing open source project
44
//
5-
// Copyright (c) 2020-2022 Apple Inc. and the Swift Distributed Tracing project
5+
// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project
66
// authors
77
// Licensed under Apache License v2.0
88
//

Sources/Instrumentation/Locks.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Distributed Tracing open source project
44
//
5-
// Copyright (c) 2020-2021 Apple Inc. and the Swift Distributed Tracing project
5+
// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project
66
// authors
77
// Licensed under Apache License v2.0
88
//

Sources/Instrumentation/MultiplexInstrument.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Distributed Tracing open source project
44
//
5-
// Copyright (c) 2020-2022 Apple Inc. and the Swift Distributed Tracing project
5+
// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project
66
// authors
77
// Licensed under Apache License v2.0
88
//

Sources/Instrumentation/NoOpInstrument.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Distributed Tracing open source project
44
//
5-
// Copyright (c) 2020-2022 Apple Inc. and the Swift Distributed Tracing project
5+
// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project
66
// authors
77
// Licensed under Apache License v2.0
88
//

Sources/Tracing/InstrumentationSystem+Tracing.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Distributed Tracing open source project
44
//
5-
// Copyright (c) 2020-2022 Apple Inc. and the Swift Distributed Tracing project
5+
// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project
66
// authors
77
// Licensed under Apache License v2.0
88
//

Sources/Tracing/NoOpTracer.swift

+12-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Distributed Tracing open source project
44
//
5-
// Copyright (c) 2020-2022 Apple Inc. and the Swift Distributed Tracing project
5+
// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project
66
// authors
77
// Licensed under Apache License v2.0
88
//
@@ -17,18 +17,19 @@ import Dispatch
1717
@_exported import InstrumentationBaggage
1818

1919
/// Tracer that ignores all operations, used when no tracing is required.
20+
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal Baggage
2021
public struct NoOpTracer: LegacyTracerProtocol {
2122
public typealias TracerSpan = NoOpSpan
2223

2324
public init() {}
2425

25-
public func startAnySpan(_ operationName: String,
26-
baggage: @autoclosure () -> Baggage,
27-
ofKind kind: SpanKind,
28-
at time: DispatchWallTime,
29-
function: String,
30-
file fileID: String,
31-
line: UInt) -> any Span
26+
public func startAnySpan<Clock: TracerClock>(_ operationName: String,
27+
baggage: @autoclosure () -> Baggage,
28+
ofKind kind: SpanKind,
29+
clock: Clock,
30+
function: String,
31+
file fileID: String,
32+
line: UInt) -> any Span
3233
{
3334
NoOpSpan(baggage: baggage())
3435
}
@@ -83,19 +84,19 @@ public struct NoOpTracer: LegacyTracerProtocol {
8384
}
8485
}
8586

86-
public func end(at time: DispatchWallTime) {
87+
public func end<Clock: TracerClock>(clock: Clock) {
8788
// ignore
8889
}
8990
}
9091
}
9192

9293
#if swift(>=5.7.0)
9394
extension NoOpTracer: TracerProtocol {
94-
public func startSpan(
95+
public func startSpan<Clock: TracerClock>(
9596
_ operationName: String,
9697
baggage: @autoclosure () -> Baggage,
9798
ofKind kind: SpanKind,
98-
at time: DispatchWallTime,
99+
clock: Clock,
99100
function: String,
100101
file fileID: String,
101102
line: UInt

Sources/Tracing/SpanProtocol.swift

+24-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Distributed Tracing open source project
44
//
5-
// Copyright (c) 2020-2022 Apple Inc. and the Swift Distributed Tracing project
5+
// Copyright (c) 2020-2023 Apple Inc. and the Swift Distributed Tracing project
66
// authors
77
// Licensed under Apache License v2.0
88
//
@@ -12,11 +12,6 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
#if swift(>=5.6.0)
16-
@preconcurrency import struct Dispatch.DispatchWallTime
17-
#else
18-
import struct Dispatch.DispatchWallTime
19-
#endif
2015
@_exported import InstrumentationBaggage
2116

2217
/// A `Span` represents an interval from the start of an operation to its end, along with additional metadata included
@@ -98,10 +93,11 @@ public protocol Span: _SwiftTracingSendableSpan {
9893
/// Implementations SHOULD prevent double-emitting by marking a span as ended internally, however it still is a
9994
/// programming mistake to rely on this behavior.
10095
///
101-
/// - Parameter time: The `DispatchWallTime` at which the span ended.
96+
/// Parameters:
97+
/// - clock: The clock to use as time source for the start time of the ``Span``
10298
///
10399
/// - SeeAlso: `Span.end()` which automatically uses the "current" time.
104-
func end(at time: DispatchWallTime)
100+
func end<Clock: TracerClock>(clock: Clock)
105101
}
106102

107103
extension Span {
@@ -115,12 +111,10 @@ extension Span {
115111
/// Implementations SHOULD prevent double-emitting by marking a span as ended internally, however it still is a
116112
/// programming mistake to rely on this behavior.
117113
///
118-
/// - Parameter time: The `DispatchWallTime` at which the span ended.
119-
///
120-
/// - SeeAlso: ``end(at:)`` which allows passing in a specific time, e.g. if the operation was ended and recorded somewhere and we need to post-factum record it.
114+
/// - SeeAlso: ``end(clock:)`` which allows passing in a specific time, e.g. if the operation was ended and recorded somewhere and we need to post-factum record it.
121115
/// Generally though prefer using the ``end()`` version of this API in user code and structure your system such that it can be called in the right place and time.
122116
public func end() {
123-
self.end(at: .now())
117+
self.end(clock: DefaultTracerClock())
124118
}
125119

126120
/// Adds a ``SpanLink`` between this `Span` and the given `Span`.
@@ -153,18 +147,31 @@ public struct SpanEvent: Equatable {
153147
/// One or more ``SpanAttribute``s with the same restrictions as defined for ``Span`` attributes.
154148
public var attributes: SpanAttributes
155149

156-
/// The `DispatchWallTime` at which this event occurred.
157-
public let time: DispatchWallTime
150+
/// The timestamp at which this event occurred.
151+
///
152+
/// It should be expressed as the number of milliseconds since UNIX Epoch (January 1st 1970).
153+
public let millisecondsSinceEpoch: UInt64
158154

159155
/// Create a new `SpanEvent`.
160156
/// - Parameters:
161157
/// - name: The human-readable name of this event.
162158
/// - attributes: attributes describing this event. Defaults to no attributes.
163-
/// - time: The `DispatchWallTime` at which this event occurred. Defaults to `.now()`.
164-
public init(name: String, attributes: SpanAttributes = [:], at time: DispatchWallTime = .now()) {
159+
/// - clock: The clock to use as time source for the start time of the ``Span``
160+
public init<Clock: TracerClock>(name: String,
161+
clock: Clock,
162+
attributes: SpanAttributes = [:])
163+
{
164+
self.name = name
165+
self.attributes = attributes
166+
self.millisecondsSinceEpoch = clock.now.millisecondsSinceEpoch
167+
}
168+
169+
public init(name: String,
170+
attributes: SpanAttributes = [:])
171+
{
165172
self.name = name
166173
self.attributes = attributes
167-
self.time = time
174+
self.millisecondsSinceEpoch = DefaultTracerClock.now.millisecondsSinceEpoch
168175
}
169176
}
170177

0 commit comments

Comments
 (0)