Skip to content

Commit 2c997b6

Browse files
Fix TimedEvent creation with Date loses precission (#73)
Fix TimedEvent creation with Date loses precission * Subsecond precision was being lost in the creation of timed event with a Date * Fix TimedEvent should conform to Event protocol * Add specialized addEvent for TimedEvent, so it can be used in the standard flow * Fix some other conversion to nanoseconds that were missing * Modify zipkin test so it doesn't fail in rounding error comparison of timed events
1 parent 44a8f17 commit 2c997b6

File tree

7 files changed

+20
-8
lines changed

7 files changed

+20
-8
lines changed

Sources/OpenTelemetryApi/Trace/SpanBuilder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ extension SpanBuilder {
146146
}
147147

148148
@discardableResult public func setStartTimestamp(timestamp: Date) -> Self {
149-
return setStartEpochNano(epochNano: UInt64(timestamp.timeIntervalSince1970) )
149+
return setStartEpochNano(epochNano: UInt64(timestamp.timeIntervalSince1970 * 1000000000) )
150150
}
151151

152152
}

Sources/OpenTelemetrySdk/Trace/RecordEventsReadableSpan.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ public class RecordEventsReadableSpan: ReadableSpan {
250250
addTimedEvent(timedEvent: TimedEvent(epochNanos: clock.now, event: event))
251251
}
252252

253+
public func addEvent(event: TimedEvent){
254+
addTimedEvent(timedEvent: event)
255+
}
256+
253257
public func addEvent<E>(event: E, timestamp: Date) where E: Event {
254258
addTimedEvent(timedEvent: TimedEvent(timestamp: timestamp, event: event))
255259
}

Sources/OpenTelemetrySdk/Trace/SpanBuilderSDK.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public class SpanBuilderSdk: SpanBuilder {
121121
}
122122

123123
public func setStartTimestamp(timestamp: Date) -> Self {
124-
return setStartEpochNano(epochNano: UInt64(timestamp.timeIntervalSince1970) )
124+
return setStartEpochNano(epochNano: UInt64(timestamp.timeIntervalSince1970 * 1000000000) )
125125
}
126126

127127
public func startSpan() -> Span {

Sources/OpenTelemetrySdk/Trace/TimedEvent.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Foundation
1717
import OpenTelemetryApi
1818

1919
/// Timed event.
20-
public struct TimedEvent: Equatable {
20+
public struct TimedEvent: Equatable, Event {
2121
public private(set) var epochNanos: UInt64
2222
public private(set) var name: String
2323
public private(set) var attributes: [String: AttributeValue]
@@ -47,7 +47,7 @@ public struct TimedEvent: Equatable {
4747
/// - name: the name of this TimedEvent.
4848
/// - attributes: the attributes of this TimedEvent. Empty by default.
4949
public init(name: String, timestamp: Date, attributes: [String: AttributeValue] = [String: AttributeValue]()) {
50-
epochNanos = UInt64(timestamp.timeIntervalSince1970) * 1000000000
50+
epochNanos = UInt64(timestamp.timeIntervalSince1970 * 1000000000)
5151
self.name = name
5252
self.attributes = attributes
5353
}
@@ -57,6 +57,6 @@ public struct TimedEvent: Equatable {
5757
/// - timestamp: timestamp in Date format.
5858
/// - event: the event.
5959
public init(timestamp: Date, event: Event) {
60-
self.init(name: event.name, epochNanos: UInt64(timestamp.timeIntervalSince1970) * 1000000000, attributes: event.attributes)
60+
self.init(name: event.name, epochNanos: UInt64(timestamp.timeIntervalSince1970 * 1000000000), attributes: event.attributes)
6161
}
6262
}

Sources/OpenTracingShim/SpanShim.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public class SpanShim: OTSpan, BaseShimProtocol {
121121
}
122122

123123
public func finish(withTime finishTime: Date?) {
124-
span.end(endOptions: EndSpanOptions(timestamp: UInt64(finishTime?.timeIntervalSince1970 ?? Date().timeIntervalSince1970)))
124+
span.end(endOptions: EndSpanOptions(timestamp: UInt64((finishTime?.timeIntervalSince1970 ?? Date().timeIntervalSince1970)) * 1000000000))
125125
}
126126

127127
static func getEventNameFrom(fields: [String: NSObject]) -> String {

Tests/ExportersTests/Zipkin/Implementation/ZipkinSpanConverterTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ZipkinSpanConverterTests: XCTestCase {
4545
}
4646

4747
public static func createTestSpan(setAttributes: Bool = true, additionalAttributes: [String: Any]? = nil, addEvents: Bool = true, addLinks: Bool = true) -> SpanData {
48-
let startTimestamp = Date()
48+
let startTimestamp = Date(timeIntervalSince1970: Double(Int(Date().timeIntervalSince1970))) // Round for comparison
4949
let endTimestamp = startTimestamp.addingTimeInterval(60)
5050
let eventTimestamp = startTimestamp
5151
let traceId = TraceId(fromHexString: "e8ea7e9ac72de94e91fabc613f9686b2")
@@ -68,7 +68,7 @@ class ZipkinSpanConverterTests: XCTestCase {
6868

6969
// let linkedSpanId = SpanId(fromHexString: "888915b6286b9c41")
7070

71-
return SpanData(traceId: traceId, spanId: spanId, parentSpanId: parentSpanId, name: "Name", kind: .client, startEpochNanos: UInt64(startTimestamp.timeIntervalSince1970) * 1000000000, attributes: attributes, timedEvents: events, status: Status.ok, endEpochNanos: UInt64(endTimestamp.timeIntervalSince1970) * 1000000000
71+
return SpanData(traceId: traceId, spanId: spanId, parentSpanId: parentSpanId, name: "Name", kind: .client, startEpochNanos: UInt64(startTimestamp.timeIntervalSince1970 * 1000000000), attributes: attributes, timedEvents: events, status: Status.ok, endEpochNanos: UInt64(endTimestamp.timeIntervalSince1970 * 1000000000)
7272
)
7373
}
7474
}

Tests/OpenTelemetrySdkTests/Trace/TimedEventTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,12 @@ class TimedEventTests: XCTestCase {
4949
XCTAssertEqual(event.name, TimedEventTests.eventName2)
5050
XCTAssertEqual(event.attributes, TimedEventTests.attributes2)
5151
}
52+
53+
func testRawTimedEventWithDate() {
54+
let dateForEvent = Date()
55+
let event = TimedEvent(name: TimedEventTests.eventName, timestamp: dateForEvent)
56+
XCTAssertEqual(event.epochNanos, UInt64(dateForEvent.timeIntervalSince1970 * 1000000000))
57+
XCTAssertEqual(event.name, TimedEventTests.eventName)
58+
XCTAssertEqual(event.attributes.count, 0)
59+
}
5260
}

0 commit comments

Comments
 (0)