@@ -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.
5748public 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}
0 commit comments