Skip to content

Commit

Permalink
Add nanoseconds to SpanEvent (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
slashmo authored Apr 13, 2023
1 parent aae93f8 commit 9fb9446
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Sources/Tracing/SpanProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,13 @@ public struct SpanEvent: Equatable {

/// The timestamp at which this event occurred.
///
/// It should be expressed as the number of milliseconds since UNIX Epoch (January 1st 1970).
public let millisecondsSinceEpoch: UInt64
/// It should be expressed as the number of nanoseconds since UNIX Epoch (January 1st 1970).
public let nanosecondsSinceEpoch: UInt64

/// Representation of the timestamp this event occured as the number of milliseconds since UNIX Epoch (January 1st 1970).
public var millisecondsSinceEpoch: UInt64 {
self.nanosecondsSinceEpoch / 1_000_000
}

/// Create a new `SpanEvent`.
/// - Parameters:
Expand All @@ -163,15 +168,15 @@ public struct SpanEvent: Equatable {
{
self.name = name
self.attributes = attributes
self.millisecondsSinceEpoch = clock.now.millisecondsSinceEpoch
self.nanosecondsSinceEpoch = clock.now.nanosecondsSinceEpoch
}

public init(name: String,
attributes: SpanAttributes = [:])
{
self.name = name
self.attributes = attributes
self.millisecondsSinceEpoch = DefaultTracerClock.now.millisecondsSinceEpoch
self.nanosecondsSinceEpoch = DefaultTracerClock.now.nanosecondsSinceEpoch
}
}

Expand Down
11 changes: 11 additions & 0 deletions Tests/TracingTests/SpanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ final class SpanTests: XCTestCase {
XCTAssertEqual(event.name, "test")
}

func testSpanEventUsesNanosecondsFromClock() {
let clock = MockClock()
clock.setTime(42_000_000)

let event = SpanEvent(name: "test", clock: clock)

XCTAssertEqual(event.name, "test")
XCTAssertEqual(event.nanosecondsSinceEpoch, 42_000_000)
XCTAssertEqual(event.millisecondsSinceEpoch, 42)
}

func testSpanAttributeIsExpressibleByStringLiteral() {
let stringAttribute: SpanAttribute = "test"
guard case .string(let stringValue) = stringAttribute else {
Expand Down

0 comments on commit 9fb9446

Please sign in to comment.