Skip to content

Commit 30631dd

Browse files
authored
remove clock protocol, use autoclosure "now" instant (#120)
**Motivation:** we don't need the clock protocol, but we do need the default clock to provide an instant. We also add time to recordError in this PR
1 parent 9fb9446 commit 30631dd

13 files changed

+150
-145
lines changed

Sources/Tracing/NoOpTracer.swift

+11-11
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public struct NoOpTracer: LegacyTracer {
2323

2424
public init() {}
2525

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
26+
public func startAnySpan<Instant: TracerInstant>(_ operationName: String,
27+
baggage: @autoclosure () -> Baggage,
28+
ofKind kind: SpanKind,
29+
at instant: @autoclosure () -> Instant,
30+
function: String,
31+
file fileID: String,
32+
line: UInt) -> any Span
3333
{
3434
NoOpSpan(baggage: baggage())
3535
}
@@ -73,7 +73,7 @@ public struct NoOpTracer: LegacyTracer {
7373

7474
public func addEvent(_ event: SpanEvent) {}
7575

76-
public func recordError(_ error: Error, attributes: SpanAttributes) {}
76+
public func recordError<Instant: TracerInstant>(_ error: Error, attributes: SpanAttributes, at instant: @autoclosure () -> Instant) {}
7777

7878
public var attributes: SpanAttributes {
7979
get {
@@ -84,19 +84,19 @@ public struct NoOpTracer: LegacyTracer {
8484
}
8585
}
8686

87-
public func end<Clock: TracerClock>(clock: Clock) {
87+
public func end<Instant: TracerInstant>(at instant: Instant) {
8888
// ignore
8989
}
9090
}
9191
}
9292

9393
#if swift(>=5.7.0)
9494
extension NoOpTracer: Tracer {
95-
public func startSpan<Clock: TracerClock>(
95+
public func startSpan<Instant: TracerInstant>(
9696
_ operationName: String,
9797
baggage: @autoclosure () -> Baggage,
9898
ofKind kind: SpanKind,
99-
clock: Clock,
99+
at instant: @autoclosure () -> Instant,
100100
function: String,
101101
file fileID: String,
102102
line: UInt

Sources/Tracing/SpanProtocol.swift

+24-12
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ public protocol Span: _SwiftTracingSendableSpan {
6565
/// Record an error of the given type described by the the given message.
6666
///
6767
/// - Parameters:
68-
/// - error: The error to be recorded.
69-
/// - attributes: Additional attributes describing the error.
70-
func recordError(_ error: Error, attributes: SpanAttributes)
68+
/// - error: The error to be recorded
69+
/// - attributes: Additional attributes describing the error
70+
/// - instant: the time instant at which the event occurred
71+
func recordError<Instant: TracerInstant>(_ error: Error,
72+
attributes: SpanAttributes,
73+
at instant: @autoclosure () -> Instant)
7174

7275
/// The attributes describing this `Span`.
7376
var attributes: SpanAttributes {
@@ -94,13 +97,22 @@ public protocol Span: _SwiftTracingSendableSpan {
9497
/// programming mistake to rely on this behavior.
9598
///
9699
/// Parameters:
97-
/// - clock: The clock to use as time source for the start time of the ``Span``
100+
/// - instant: the time instant at which the span ended
98101
///
99102
/// - SeeAlso: `Span.end()` which automatically uses the "current" time.
100-
func end<Clock: TracerClock>(clock: Clock)
103+
func end<Instant: TracerInstant>(at instant: Instant)
101104
}
102105

103106
extension Span {
107+
/// Record an error of the given type, at the current time, described by the the given message
108+
///
109+
/// - Parameters:
110+
/// - error: The error to be recorded
111+
/// - attributes: Additional attributes describing the error
112+
public func recordError(_ error: Error, attributes: SpanAttributes) {
113+
self.recordError(error, attributes: attributes, at: DefaultTracerClock.now)
114+
}
115+
104116
/// End this `Span` at the current time.
105117
///
106118
/// ### Rules about ending Spans
@@ -114,7 +126,7 @@ extension Span {
114126
/// - 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.
115127
/// 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.
116128
public func end() {
117-
self.end(clock: DefaultTracerClock())
129+
self.end(at: DefaultTracerClock.now)
118130
}
119131

120132
/// Adds a ``SpanLink`` between this `Span` and the given `Span`.
@@ -152,7 +164,7 @@ public struct SpanEvent: Equatable {
152164
/// It should be expressed as the number of nanoseconds since UNIX Epoch (January 1st 1970).
153165
public let nanosecondsSinceEpoch: UInt64
154166

155-
/// Representation of the timestamp this event occured as the number of milliseconds since UNIX Epoch (January 1st 1970).
167+
/// Representation of the timestamp this event occurred as the number of milliseconds since UNIX Epoch (January 1st 1970).
156168
public var millisecondsSinceEpoch: UInt64 {
157169
self.nanosecondsSinceEpoch / 1_000_000
158170
}
@@ -161,14 +173,14 @@ public struct SpanEvent: Equatable {
161173
/// - Parameters:
162174
/// - name: The human-readable name of this event.
163175
/// - attributes: attributes describing this event. Defaults to no attributes.
164-
/// - clock: The clock to use as time source for the start time of the ``Span``
165-
public init<Clock: TracerClock>(name: String,
166-
clock: Clock,
167-
attributes: SpanAttributes = [:])
176+
/// - instant: the time instant at which the event occurred
177+
public init<Instant: TracerInstant>(name: String,
178+
at instant: @autoclosure () -> Instant,
179+
attributes: SpanAttributes = [:])
168180
{
169181
self.name = name
170182
self.attributes = attributes
171-
self.nanosecondsSinceEpoch = clock.now.nanosecondsSinceEpoch
183+
self.nanosecondsSinceEpoch = instant().nanosecondsSinceEpoch
172184
}
173185

174186
public init(name: String,

Sources/Tracing/Tracer.swift

+24-24
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ import Dispatch
3434
///
3535
/// - Parameters:
3636
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
37-
/// - clock: The clock to use as time source for the start time of the ``Span``
37+
/// - instant: the time instant at which the span started
3838
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
3939
/// - kind: The ``SpanKind`` of the new ``Span``.
4040
/// - function: The function name in which the span was started
4141
/// - fileID: The `fileID` where the span was started.
4242
/// - line: The file line where the span was started.
4343
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal Baggage
44-
public func startSpan<Clock: TracerClock>(
44+
public func startSpan<Instant: TracerInstant>(
4545
_ operationName: String,
46-
clock: Clock,
46+
at instant: @autoclosure () -> Instant,
4747
baggage: @autoclosure () -> Baggage = .current ?? .topLevel,
4848
ofKind kind: SpanKind = .internal,
4949
function: String = #function,
@@ -54,7 +54,7 @@ public func startSpan<Clock: TracerClock>(
5454
// we try to not use the deprecated methods ourselves anyway
5555
InstrumentationSystem.legacyTracer.startAnySpan(
5656
operationName,
57-
clock: clock,
57+
at: instant(),
5858
baggage: baggage(),
5959
ofKind: kind,
6060
function: function,
@@ -99,7 +99,7 @@ public func startSpan(
9999
// we try to not use the deprecated methods ourselves anyway
100100
InstrumentationSystem.legacyTracer.startAnySpan(
101101
operationName,
102-
clock: DefaultTracerClock(),
102+
at: DefaultTracerClock.now,
103103
baggage: baggage(),
104104
ofKind: kind,
105105
function: function,
@@ -129,7 +129,7 @@ public func startSpan(
129129
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
130130
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
131131
/// - kind: The ``SpanKind`` of the new ``Span``.
132-
/// - clock: The clock to use as time source for the start time of the ``Span``
132+
/// - instant: the time instant at which the span started
133133
/// - function: The function name in which the span was started
134134
/// - fileID: The `fileID` where the span was started.
135135
/// - line: The file line where the span was started.
@@ -138,7 +138,7 @@ public func startSpan(
138138
_ operationName: String,
139139
baggage: @autoclosure () -> Baggage = .current ?? .topLevel,
140140
ofKind kind: SpanKind = .internal,
141-
clock: some TracerClock = DefaultTracerClock(),
141+
at instant: @autoclosure () -> some TracerInstant = DefaultTracerClock.now,
142142
function: String = #function,
143143
file fileID: String = #fileID,
144144
line: UInt = #line
@@ -147,7 +147,7 @@ public func startSpan(
147147
// we try to not use the deprecated methods ourselves anyway
148148
InstrumentationSystem.tracer.startAnySpan(
149149
operationName,
150-
clock: clock,
150+
at: instant(),
151151
baggage: baggage(),
152152
ofKind: kind,
153153
function: function,
@@ -173,7 +173,7 @@ public func startSpan(
173173
///
174174
/// - Parameters:
175175
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
176-
/// - clock: The clock to use as time source for the start time of the ``Span``
176+
/// - instant: the time instant at which the span started
177177
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
178178
/// - kind: The ``SpanKind`` of the new ``Span``.
179179
/// - function: The function name in which the span was started
@@ -183,9 +183,9 @@ public func startSpan(
183183
/// - Returns: the value returned by `operation`
184184
/// - Throws: the error the `operation` has thrown (if any)
185185
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal Baggage
186-
public func withSpan<T, Clock: TracerClock>(
186+
public func withSpan<T, Instant: TracerInstant>(
187187
_ operationName: String,
188-
clock: Clock,
188+
at instant: @autoclosure () -> Instant,
189189
baggage: @autoclosure () -> Baggage = .current ?? .topLevel,
190190
ofKind kind: SpanKind = .internal,
191191
function: String = #function,
@@ -195,7 +195,7 @@ public func withSpan<T, Clock: TracerClock>(
195195
) rethrows -> T {
196196
try InstrumentationSystem.legacyTracer.withAnySpan(
197197
operationName,
198-
clock: DefaultTracerClock(),
198+
at: DefaultTracerClock.now,
199199
baggage: baggage(),
200200
ofKind: kind,
201201
function: function,
@@ -240,7 +240,7 @@ public func withSpan<T>(
240240
) rethrows -> T {
241241
try InstrumentationSystem.legacyTracer.withAnySpan(
242242
operationName,
243-
clock: DefaultTracerClock(),
243+
at: DefaultTracerClock.now,
244244
baggage: baggage(),
245245
ofKind: kind,
246246
function: function,
@@ -267,7 +267,7 @@ public func withSpan<T>(
267267
///
268268
/// - Parameters:
269269
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
270-
/// - clock: The clock to use as time source for the start time of the ``Span``
270+
/// - instant: the time instant at which the span started
271271
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
272272
/// - kind: The ``SpanKind`` of the new ``Span``.
273273
/// - function: The function name in which the span was started
@@ -280,15 +280,15 @@ public func withSpan<T>(
280280
_ operationName: String,
281281
baggage: @autoclosure () -> Baggage = .current ?? .topLevel,
282282
ofKind kind: SpanKind = .internal,
283-
clock: some TracerClock = DefaultTracerClock(),
283+
at instant: @autoclosure () -> some TracerInstant = DefaultTracerClock.now,
284284
function: String = #function,
285285
file fileID: String = #fileID,
286286
line: UInt = #line,
287287
_ operation: (any Span) throws -> T
288288
) rethrows -> T {
289289
try InstrumentationSystem.legacyTracer.withAnySpan(
290290
operationName,
291-
clock: clock,
291+
at: instant(),
292292
baggage: baggage(),
293293
ofKind: kind,
294294
function: function,
@@ -316,7 +316,7 @@ public func withSpan<T>(
316316
///
317317
/// - Parameters:
318318
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
319-
/// - clock: The clock to use as time source for the start time of the ``Span``
319+
/// - instant: the time instant at which the span started
320320
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
321321
/// - kind: The ``SpanKind`` of the new ``Span``.
322322
/// - function: The function name in which the span was started
@@ -326,9 +326,9 @@ public func withSpan<T>(
326326
/// - Returns: the value returned by `operation`
327327
/// - Throws: the error the `operation` has thrown (if any)
328328
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal Baggage
329-
public func withSpan<T, Clock: TracerClock>(
329+
public func withSpan<T, Instant: TracerInstant>(
330330
_ operationName: String,
331-
clock: Clock,
331+
at instant: @autoclosure () -> Instant,
332332
baggage: @autoclosure () -> Baggage = .current ?? .topLevel,
333333
ofKind kind: SpanKind = .internal,
334334
function: String = #function,
@@ -338,7 +338,7 @@ public func withSpan<T, Clock: TracerClock>(
338338
) async rethrows -> T {
339339
try await InstrumentationSystem.legacyTracer.withAnySpan(
340340
operationName,
341-
clock: DefaultTracerClock(),
341+
at: DefaultTracerClock.now,
342342
baggage: baggage(),
343343
ofKind: kind,
344344
function: function,
@@ -383,7 +383,7 @@ public func withSpan<T>(
383383
) async rethrows -> T {
384384
try await InstrumentationSystem.legacyTracer.withAnySpan(
385385
operationName,
386-
clock: DefaultTracerClock(),
386+
at: DefaultTracerClock.now,
387387
baggage: baggage(),
388388
ofKind: kind,
389389
function: function,
@@ -411,7 +411,7 @@ public func withSpan<T>(
411411
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
412412
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
413413
/// - kind: The ``SpanKind`` of the new ``Span``.
414-
/// - clock: The clock to use as time source for the start time of the ``Span``
414+
/// - instant: the time instant at which the span started
415415
/// - function: The function name in which the span was started
416416
/// - fileID: The `fileID` where the span was started.
417417
/// - line: The file line where the span was started.
@@ -422,15 +422,15 @@ public func withSpan<T>(
422422
_ operationName: String,
423423
baggage: @autoclosure () -> Baggage = .current ?? .topLevel,
424424
ofKind kind: SpanKind = .internal,
425-
clock: some TracerClock = DefaultTracerClock(),
425+
at instant: @autoclosure () -> some TracerInstant = DefaultTracerClock.now,
426426
function: String = #function,
427427
file fileID: String = #fileID,
428428
line: UInt = #line,
429429
_ operation: (any Span) async throws -> T
430430
) async rethrows -> T {
431431
try await InstrumentationSystem.legacyTracer.withAnySpan(
432432
operationName,
433-
clock: clock,
433+
at: instant(),
434434
baggage: baggage(),
435435
ofKind: kind,
436436
function: function,

0 commit comments

Comments
 (0)