Skip to content

Commit 9fb9446

Browse files
authored
Add nanoseconds to SpanEvent (#118)
1 parent aae93f8 commit 9fb9446

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

Sources/Tracing/SpanProtocol.swift

+9-4
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,13 @@ public struct SpanEvent: Equatable {
149149

150150
/// The timestamp at which this event occurred.
151151
///
152-
/// It should be expressed as the number of milliseconds since UNIX Epoch (January 1st 1970).
153-
public let millisecondsSinceEpoch: UInt64
152+
/// It should be expressed as the number of nanoseconds since UNIX Epoch (January 1st 1970).
153+
public let nanosecondsSinceEpoch: UInt64
154+
155+
/// Representation of the timestamp this event occured as the number of milliseconds since UNIX Epoch (January 1st 1970).
156+
public var millisecondsSinceEpoch: UInt64 {
157+
self.nanosecondsSinceEpoch / 1_000_000
158+
}
154159

155160
/// Create a new `SpanEvent`.
156161
/// - Parameters:
@@ -163,15 +168,15 @@ public struct SpanEvent: Equatable {
163168
{
164169
self.name = name
165170
self.attributes = attributes
166-
self.millisecondsSinceEpoch = clock.now.millisecondsSinceEpoch
171+
self.nanosecondsSinceEpoch = clock.now.nanosecondsSinceEpoch
167172
}
168173

169174
public init(name: String,
170175
attributes: SpanAttributes = [:])
171176
{
172177
self.name = name
173178
self.attributes = attributes
174-
self.millisecondsSinceEpoch = DefaultTracerClock.now.millisecondsSinceEpoch
179+
self.nanosecondsSinceEpoch = DefaultTracerClock.now.nanosecondsSinceEpoch
175180
}
176181
}
177182

Tests/TracingTests/SpanTests.swift

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ final class SpanTests: XCTestCase {
2424
XCTAssertEqual(event.name, "test")
2525
}
2626

27+
func testSpanEventUsesNanosecondsFromClock() {
28+
let clock = MockClock()
29+
clock.setTime(42_000_000)
30+
31+
let event = SpanEvent(name: "test", clock: clock)
32+
33+
XCTAssertEqual(event.name, "test")
34+
XCTAssertEqual(event.nanosecondsSinceEpoch, 42_000_000)
35+
XCTAssertEqual(event.millisecondsSinceEpoch, 42)
36+
}
37+
2738
func testSpanAttributeIsExpressibleByStringLiteral() {
2839
let stringAttribute: SpanAttribute = "test"
2940
guard case .string(let stringValue) = stringAttribute else {

0 commit comments

Comments
 (0)