Skip to content

Commit 343a700

Browse files
committed
Add nanoseconds to instant protocol
**Motivation:** we have nanoseconds precision on most platforms so lets expose it.
1 parent a08effc commit 343a700

File tree

4 files changed

+27
-38
lines changed

4 files changed

+27
-38
lines changed

Sources/Tracing/TracingTime.swift

+15-26
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 TracerInstantProtocol: 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 TracerInstantProtocol {
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.
@@ -69,23 +60,22 @@ public struct DefaultTracerClock: TracerClock {
6960
}
7061

7162
public struct Timestamp: TracerInstantProtocol {
72-
/// Milliseconds since January 1st, 1970, also known as "unix epoch".
73-
public var millisecondsSinceEpoch: UInt64
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

+4-4
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
@@ -290,7 +290,7 @@ extension DynamicTracepointTestTracer {
290290
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

+4-4
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

@@ -158,7 +158,7 @@ final class TestSpan: Span {
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/TracerTimeTests.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ 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
5050
XCTAssertEqual(span.startTime, 13)
@@ -62,13 +62,13 @@ final class MockClock: TracerClock {
6262
}
6363

6464
struct Instant: TracerInstantProtocol {
65-
var millisecondsSinceEpoch: UInt64
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)