Skip to content

Commit bb7fa3b

Browse files
authored
Add nanoseconds to instant protocol (#114)
1 parent 63a9c24 commit bb7fa3b

File tree

6 files changed

+41
-52
lines changed

6 files changed

+41
-52
lines changed

Sources/Tracing/TracingTime.swift

+17-28
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,17 @@ import Darwin
2121
@_exported import Instrumentation
2222
@_exported import InstrumentationBaggage
2323

24-
public protocol SwiftDistributedTracingDurationProtocol: Comparable, AdditiveArithmetic, Sendable {
25-
static func / (_ lhs: Self, _ rhs: Int) -> Self
26-
static func /= (_ lhs: inout Self, _ rhs: Int)
27-
static func * (_ lhs: Self, _ rhs: Int) -> Self
28-
static func *= (_ lhs: inout Self, _ rhs: Int)
29-
30-
static func / (_ lhs: Self, _ rhs: Self) -> Double
31-
}
32-
33-
extension SwiftDistributedTracingDurationProtocol {
34-
public static func /= (_ lhs: inout Self, _ rhs: Int) {
35-
lhs = lhs / rhs
36-
}
24+
public protocol TracerInstant: Comparable, Hashable, Sendable {
25+
/// Representation of this instant as the number of nanoseconds since UNIX Epoch (January 1st 1970)
26+
var nanosecondsSinceEpoch: UInt64 { get }
3727
}
3828

39-
public protocol SwiftDistributedTracingInstantProtocol: Comparable, Hashable, Sendable {}
40-
41-
public protocol TracerInstantProtocol: SwiftDistributedTracingInstantProtocol {
29+
extension TracerInstant {
4230
/// Representation of this instant as the number of milliseconds since UNIX Epoch (January 1st 1970)
43-
var millisecondsSinceEpoch: UInt64 { get }
31+
@inlinable
32+
public var millisecondsSinceEpoch: UInt64 {
33+
self.nanosecondsSinceEpoch / 1_000_000
34+
}
4435
}
4536

4637
/// A specialized clock protocol for purposes of tracing.
@@ -55,7 +46,7 @@ public protocol TracerInstantProtocol: SwiftDistributedTracingInstantProtocol {
5546
/// especially when the system is already using some notion of simulated or mocked time, such that traces are
5647
/// expressed using the same notion of time.
5748
public protocol TracerClock {
58-
associatedtype Instant: TracerInstantProtocol
49+
associatedtype Instant: TracerInstant
5950

6051
var now: Self.Instant { get }
6152
}
@@ -68,24 +59,23 @@ public struct DefaultTracerClock: TracerClock {
6859
// empty
6960
}
7061

71-
public struct Timestamp: TracerInstantProtocol {
72-
/// Milliseconds since January 1st, 1970, also known as "unix epoch".
73-
public var millisecondsSinceEpoch: UInt64
62+
public struct Timestamp: TracerInstant {
63+
public let nanosecondsSinceEpoch: UInt64
7464

75-
internal init(millisecondsSinceEpoch: UInt64) {
76-
self.millisecondsSinceEpoch = millisecondsSinceEpoch
65+
public init(nanosecondsSinceEpoch: UInt64) {
66+
self.nanosecondsSinceEpoch = nanosecondsSinceEpoch
7767
}
7868

7969
public static func < (lhs: Instant, rhs: Instant) -> Bool {
80-
lhs.millisecondsSinceEpoch < rhs.millisecondsSinceEpoch
70+
lhs.nanosecondsSinceEpoch < rhs.nanosecondsSinceEpoch
8171
}
8272

8373
public static func == (lhs: Instant, rhs: Instant) -> Bool {
84-
lhs.millisecondsSinceEpoch == rhs.millisecondsSinceEpoch
74+
lhs.nanosecondsSinceEpoch == rhs.nanosecondsSinceEpoch
8575
}
8676

8777
public func hash(into hasher: inout Hasher) {
88-
self.millisecondsSinceEpoch.hash(into: &hasher)
78+
self.nanosecondsSinceEpoch.hash(into: &hasher)
8979
}
9080
}
9181

@@ -100,8 +90,7 @@ public struct DefaultTracerClock: TracerClock {
10090
/// and the odds that this code will still be running 530 years from now is very, very low,
10191
/// so as a practical matter this will never overflow.
10292
let nowNanos = UInt64(ts.tv_sec) &* 1_000_000_000 &+ UInt64(ts.tv_nsec)
103-
let nowMillis = UInt64(nowNanos / 1_000_000) // nanos to millis
10493

105-
return Instant(millisecondsSinceEpoch: nowMillis)
94+
return Instant(nanosecondsSinceEpoch: nowNanos)
10695
}
10796
}

Tests/TracingTests/DynamicTracepointTracerTests.swift

+11-11
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ extension DynamicTracepointTestTracer {
258258

259259
private var status: SpanStatus?
260260

261-
private let startTime: UInt64
262-
private(set) var endTime: UInt64?
261+
private let startTimestampNanosSinceEpoch: UInt64
262+
private(set) var endTimestampNanosSinceEpoch: UInt64?
263263

264264
public var operationName: String
265265
private(set) var baggage: Baggage
@@ -281,16 +281,16 @@ extension DynamicTracepointTestTracer {
281281
return span
282282
}
283283

284-
init<Instant: TracerInstantProtocol>(operationName: String,
285-
startTime: Instant,
286-
baggage: Baggage,
287-
kind: SpanKind,
288-
file fileID: String,
289-
line: UInt,
290-
onEnd: @escaping (TracepointSpan) -> Void)
284+
init<Instant: TracerInstant>(operationName: String,
285+
startTime: Instant,
286+
baggage: Baggage,
287+
kind: SpanKind,
288+
file fileID: String,
289+
line: UInt,
290+
onEnd: @escaping (TracepointSpan) -> Void)
291291
{
292292
self.operationName = operationName
293-
self.startTime = startTime.millisecondsSinceEpoch
293+
self.startTimestampNanosSinceEpoch = startTime.nanosecondsSinceEpoch
294294
self.baggage = baggage
295295
self.onEnd = onEnd
296296
self.kind = kind
@@ -323,7 +323,7 @@ extension DynamicTracepointTestTracer {
323323
}
324324

325325
func end<Clock: TracerClock>(clock: Clock) {
326-
self.endTime = clock.now.millisecondsSinceEpoch
326+
self.endTimestampNanosSinceEpoch = clock.now.nanosecondsSinceEpoch
327327
self.onEnd(self)
328328
}
329329
}

Tests/TracingTests/TestTracer.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ final class TestSpan: Span {
124124

125125
private var status: SpanStatus?
126126

127-
public let startTime: UInt64
128-
public private(set) var endTime: UInt64?
127+
public let startTimestampNanosSinceEpoch: UInt64
128+
public private(set) var endTimestampNanosSinceEpoch: UInt64?
129129

130130
private(set) var recordedErrors: [(Error, SpanAttributes)] = []
131131

@@ -150,15 +150,15 @@ final class TestSpan: Span {
150150

151151
let onEnd: (TestSpan) -> Void
152152

153-
init<Instant: TracerInstantProtocol>(
153+
init<Instant: TracerInstant>(
154154
operationName: String,
155155
startTime: Instant,
156156
baggage: Baggage,
157157
kind: SpanKind,
158158
onEnd: @escaping (TestSpan) -> Void
159159
) {
160160
self.operationName = operationName
161-
self.startTime = startTime.millisecondsSinceEpoch
161+
self.startTimestampNanosSinceEpoch = startTime.nanosecondsSinceEpoch
162162
self.baggage = baggage
163163
self.onEnd = onEnd
164164
self.kind = kind
@@ -182,7 +182,7 @@ final class TestSpan: Span {
182182
}
183183

184184
func end<Clock: TracerClock>(clock: Clock) {
185-
self.endTime = clock.now.millisecondsSinceEpoch
185+
self.endTimestampNanosSinceEpoch = clock.now.nanosecondsSinceEpoch
186186
self.onEnd(self)
187187
}
188188
}

Tests/TracingTests/TracedLockTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private final class TracedLockPrintlnTracer: LegacyTracer {
124124

125125
private(set) var isRecording = false
126126

127-
init<Instant: TracerInstantProtocol>(
127+
init<Instant: TracerInstant>(
128128
operationName: String,
129129
startTime: Instant,
130130
kind: SpanKind,

Tests/TracingTests/TracerTests+swift57.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ final class SampleSwift57Span: Span {
9191

9292
let onEnd: (SampleSwift57Span) -> Void
9393

94-
init<Instant: TracerInstantProtocol>(
94+
init<Instant: TracerInstant>(
9595
operationName: String,
9696
startTime: Instant,
9797
baggage: Baggage,

Tests/TracingTests/TracerTimeTests.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ final class TracerTimeTests: XCTestCase {
4444
mockClock.setTime(13)
4545
#if swift(>=5.7.0)
4646
let span: TestSpan = tracer.startSpan("start", clock: mockClock)
47-
XCTAssertEqual(span.startTime, 13)
47+
XCTAssertEqual(span.startTimestampNanosSinceEpoch, 13)
4848
#else
4949
let span: TestSpan = tracer.startAnySpan("start", clock: mockClock) as! TestSpan
50-
XCTAssertEqual(span.startTime, 13)
50+
XCTAssertEqual(span.startTimestampNanosSinceEpoch, 13)
5151
#endif
5252
}
5353
}
@@ -61,14 +61,14 @@ final class MockClock: TracerClock {
6161
self._now = time
6262
}
6363

64-
struct Instant: TracerInstantProtocol {
65-
var millisecondsSinceEpoch: UInt64
64+
struct Instant: TracerInstant {
65+
var nanosecondsSinceEpoch: UInt64
6666
static func < (lhs: MockClock.Instant, rhs: MockClock.Instant) -> Bool {
67-
lhs.millisecondsSinceEpoch < rhs.millisecondsSinceEpoch
67+
lhs.nanosecondsSinceEpoch < rhs.nanosecondsSinceEpoch
6868
}
6969
}
7070

7171
var now: Instant {
72-
Instant(millisecondsSinceEpoch: self._now)
72+
Instant(nanosecondsSinceEpoch: self._now)
7373
}
7474
}

0 commit comments

Comments
 (0)