Skip to content

Commit 210abc6

Browse files
committed
introduce tracing time
1 parent 1aff1f1 commit 210abc6

File tree

7 files changed

+94
-29
lines changed

7 files changed

+94
-29
lines changed

Sources/Tracing/NoOpTracer.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Dispatch
1615
@_exported import Instrumentation
1716
@_exported import InstrumentationBaggage
1817

@@ -24,7 +23,7 @@ public struct NoOpTracer: Tracer {
2423
_ operationName: String,
2524
baggage: Baggage,
2625
ofKind kind: SpanKind,
27-
at time: DispatchWallTime,
26+
at time: TracingTime,
2827
function: String,
2928
file fileID: String,
3029
line: UInt
@@ -71,7 +70,7 @@ public struct NoOpTracer: Tracer {
7170
}
7271
}
7372

74-
public func end(at time: DispatchWallTime) {
73+
public func end(at time: TracingTime) {
7574
// ignore
7675
}
7776
}

Sources/Tracing/Span.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -67,7 +62,7 @@ public protocol Span: AnyObject, _SwiftTracingSendableSpan {
6762
/// - Parameter time: The `DispatchWallTime` at which the span ended.
6863
///
6964
/// - SeeAlso: `Span.end()` which automatically uses the "current" time.
70-
func end(at time: DispatchWallTime)
65+
func end(at time: TracingTime)
7166
}
7267

7368
extension Span {
@@ -108,15 +103,15 @@ public struct SpanEvent: Equatable {
108103
/// One or more ``SpanAttribute``s with the same restrictions as defined for ``Span`` attributes.
109104
public var attributes: SpanAttributes
110105

111-
/// The `DispatchWallTime` at which this event occurred.
112-
public let time: DispatchWallTime
106+
/// The time at which this event occurred.
107+
public let time: TracingTime
113108

114109
/// Create a new `SpanEvent`.
115110
/// - Parameters:
116111
/// - name: The human-readable name of this event.
117112
/// - attributes: attributes describing this event. Defaults to no attributes.
118113
/// - time: The `DispatchWallTime` at which this event occurred. Defaults to `.now()`.
119-
public init(name: String, attributes: SpanAttributes = [:], at time: DispatchWallTime = .now()) {
114+
public init(name: String, attributes: SpanAttributes = [:], at time: TracingTime = .now()) {
120115
self.name = name
121116
self.attributes = attributes
122117
self.time = time

Sources/Tracing/Tracer.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
import Dispatch
1615
@_exported import Instrumentation
1716
@_exported import InstrumentationBaggage
1817

@@ -37,7 +36,7 @@ public protocol Tracer: Instrument {
3736
_ operationName: String,
3837
baggage: Baggage,
3938
ofKind kind: SpanKind,
40-
at time: DispatchWallTime,
39+
at time: TracingTime,
4140
function: String,
4241
file fileID: String,
4342
line: UInt

Sources/Tracing/TracingTime.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Distributed Tracing open source project
4+
//
5+
// Copyright (c) 2020-2022 Apple Inc. and the Swift Distributed Tracing project
6+
// authors
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#if swift(>=5.6.0)
16+
@_implementationOnly @preconcurrency import struct Dispatch.DispatchWallTime
17+
#else
18+
@_implementationOnly import struct Dispatch.DispatchWallTime
19+
#endif
20+
21+
/// A wall-clock based time value used to mark the beginning and end of a trace ``Span``.
22+
///
23+
/// ### Rationale for this type
24+
/// This type is introduced in order to abstract between various time sources, such as
25+
/// Dispatch's `DispatchWallTime` or any potential future `Clock` type in the Swift
26+
/// standard library (or elsewhere) that we might want to use as source of time.
27+
///
28+
/// Abstracting away the source of the measurement into this wrapper allows us to
29+
/// have ``Tracer`` implementations don't care about the user API that is surfaced as `startSpan`
30+
/// to end-users. As time goes on, this API may evolve, but we will not have to make breaking changes
31+
/// to tracer implementations themselves.
32+
public struct TracingTime: Sendable {
33+
enum Repr {
34+
case dispatchWallTime(DispatchWallTime)
35+
}
36+
private var repr: Repr
37+
38+
private init(dispatchWallTime: DispatchWallTime) {
39+
self.repr = .dispatchWallTime(dispatchWallTime)
40+
}
41+
42+
public static func now() -> TracingTime {
43+
.init(dispatchWallTime: .now())
44+
}
45+
46+
public var rawValue: UInt64 {
47+
switch self.repr {
48+
case .dispatchWallTime(let wallTime):
49+
return wallTime.rawValue
50+
}
51+
}
52+
}
53+
54+
extension TracingTime: Equatable {
55+
public static func ==(lhs: TracingTime, rhs: TracingTime) -> Bool {
56+
switch (lhs.repr, rhs.repr) {
57+
case (.dispatchWallTime(let l), .dispatchWallTime(let r)):
58+
return l.rawValue == r.rawValue
59+
}
60+
}
61+
62+
}
63+
64+
extension TracingTime: CustomStringConvertible {
65+
public var description: String {
66+
switch self.repr {
67+
case .dispatchWallTime(let wallTime):
68+
return "TracingTime(\(wallTime.rawValue))"
69+
}
70+
71+
}
72+
}

Tests/TracingTests/DynamicTracepointTracerTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ final class DynamicTracepointTestTracer: Tracer {
145145
func startSpan(_ operationName: String,
146146
baggage: InstrumentationBaggage.Baggage,
147147
ofKind kind: Tracing.SpanKind,
148-
at time: DispatchWallTime,
148+
at time: TracingTime,
149149
function: String,
150150
file fileID: String,
151151
line: UInt) -> Tracing.Span
@@ -235,16 +235,16 @@ extension DynamicTracepointTestTracer {
235235

236236
private var status: SpanStatus?
237237

238-
private let startTime: DispatchWallTime
239-
private(set) var endTime: DispatchWallTime?
238+
private let startTime: TracingTime
239+
private(set) var endTime: TracingTime?
240240

241241
private(set) var baggage: Baggage
242242
private(set) var isRecording: Bool = false
243243

244244
let onEnd: (Span) -> Void
245245

246246
init(operationName: String,
247-
startTime: DispatchWallTime,
247+
startTime: TracingTime,
248248
baggage: Baggage,
249249
kind: SpanKind,
250250
file fileID: String,
@@ -284,7 +284,7 @@ extension DynamicTracepointTestTracer {
284284
// nothing
285285
}
286286

287-
func end(at time: DispatchWallTime) {
287+
func end(at time: TracingTime) {
288288
self.endTime = time
289289
self.onEnd(self)
290290
}

Tests/TracingTests/TestTracer.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final class TestTracer: Tracer {
2727
_ operationName: String,
2828
baggage: Baggage,
2929
ofKind kind: SpanKind,
30-
at time: DispatchWallTime,
30+
at time: TracingTime,
3131
function: String,
3232
file fileID: String,
3333
line: UInt
@@ -101,8 +101,8 @@ final class TestSpan: Span {
101101

102102
private var status: SpanStatus?
103103

104-
private let startTime: DispatchWallTime
105-
private(set) var endTime: DispatchWallTime?
104+
private let startTime: TracingTime
105+
private(set) var endTime: TracingTime?
106106

107107
let baggage: Baggage
108108

@@ -126,7 +126,7 @@ final class TestSpan: Span {
126126

127127
init(
128128
operationName: String,
129-
startTime: DispatchWallTime,
129+
startTime: TracingTime,
130130
baggage: Baggage,
131131
kind: SpanKind,
132132
onEnd: @escaping (Span) -> Void
@@ -153,7 +153,7 @@ final class TestSpan: Span {
153153

154154
func recordError(_ error: Error) {}
155155

156-
func end(at time: DispatchWallTime) {
156+
func end(at time: TracingTime) {
157157
self.endTime = time
158158
self.onEnd(self)
159159
}

Tests/TracingTests/TracedLockTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private final class TracedLockPrintlnTracer: Tracer {
6464
_ operationName: String,
6565
baggage: Baggage,
6666
ofKind kind: SpanKind,
67-
at time: DispatchWallTime,
67+
at time: TracingTime,
6868
function: String,
6969
file fileID: String,
7070
line: UInt
@@ -103,8 +103,8 @@ private final class TracedLockPrintlnTracer: Tracer {
103103

104104
private var status: SpanStatus?
105105

106-
private let startTime: DispatchWallTime
107-
private(set) var endTime: DispatchWallTime?
106+
private let startTime: TracingTime
107+
private(set) var endTime: TracingTime?
108108

109109
let baggage: Baggage
110110

@@ -126,7 +126,7 @@ private final class TracedLockPrintlnTracer: Tracer {
126126

127127
init(
128128
operationName: String,
129-
startTime: DispatchWallTime,
129+
startTime: TracingTime,
130130
kind: SpanKind,
131131
baggage: Baggage
132132
) {
@@ -153,7 +153,7 @@ private final class TracedLockPrintlnTracer: Tracer {
153153

154154
func recordError(_ error: Error) {}
155155

156-
func end(at time: DispatchWallTime) {
156+
func end(at time: TracingTime) {
157157
self.endTime = time
158158
print(" span [\(self.operationName): \(self.baggage[TaskIDKey.self] ?? "no-name")] @ \(time): end")
159159
}

0 commit comments

Comments
 (0)