Skip to content

Commit 7a41072

Browse files
author
Ignacio Bonafonte
authored
Merge pull request #411 from emanuelschmoczer/add-option-to-set-timestamp
Add option to set the timestamp in LogRecordBuilder This PR adds the option to set the Timestamp for log events when using the LogRecordBuilder. I looked at other libraries, like the one for Java before I started working on this PR and they allow setting both the timestamp and observed timestamp. To not change any existing functionality I went with timestamp ?? sharedState.clock.now in the emit() function, which should allow anyone who does not care about setting the timestamp manually to ignore this change. For the tests I only explicitly covered the cases when both timestamp and observed timestamp are either set or not set. Please let me know if you prefer testing this in a different way or have any feedback for improvement.
2 parents e585c48 + 44f56cb commit 7a41072

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

Sources/OpenTelemetryApi/Logs/DefaultLogger.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class DefaultLogger : Logger {
3737
}
3838

3939
private class NoopLogRecordBuilder : EventBuilder {
40+
func setTimestamp(_ timestamp: Date) -> Self {
41+
return self
42+
}
4043

4144
func setObservedTimestamp(_ observed: Date) -> Self {
4245
return self

Sources/OpenTelemetryApi/Logs/LogRecordBuilder.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
import Foundation
77

88
public protocol LogRecordBuilder {
9+
/// set the timestamp on which the event ocurred
10+
///
11+
/// - Parameter timestamp: the Date object
12+
/// - Returns: self
13+
func setTimestamp(_ timestamp: Date) -> Self
914

1015
/// set the timestamp that the log was observed
1116
///

Sources/OpenTelemetrySdk/Logs/LogRecordBuilderSdk.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class LogRecordBuilderSdk : EventBuilder {
1515
private var limits : LogLimits
1616
private var instrumentationScope : InstrumentationScopeInfo
1717
private var includeSpanContext : Bool
18-
18+
private var timestamp: Date?
1919
private var observedTimestamp : Date?
2020
private var body : String?
2121
private var severity: Severity?
@@ -31,7 +31,12 @@ public class LogRecordBuilderSdk : EventBuilder {
3131
self.instrumentationScope = instrumentationScope
3232
attributes = AttributesDictionary(capacity: sharedState.logLimits.maxAttributeCount, valueLengthLimit: sharedState.logLimits.maxAttributeLength)
3333
}
34-
34+
35+
public func setTimestamp(_ timestamp: Date) -> Self {
36+
self.timestamp = timestamp
37+
return self
38+
}
39+
3540
public func setObservedTimestamp(_ observed: Date) -> Self {
3641
self.observedTimestamp = observed
3742
return self
@@ -68,7 +73,7 @@ public class LogRecordBuilderSdk : EventBuilder {
6873

6974
sharedState.activeLogRecordProcessor.onEmit(logRecord: ReadableLogRecord(resource: sharedState.resource,
7075
instrumentationScopeInfo: instrumentationScope,
71-
timestamp: sharedState.clock.now,
76+
timestamp: timestamp ?? sharedState.clock.now,
7277
observedTimestamp: observedTimestamp,
7378
spanContext: spanContext,
7479
severity: severity,

Tests/OpenTelemetryApiTests/Logs/DefaultLoggerTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class DefaultLoggerTests : XCTestCase {
2525
XCTAssertNoThrow(defaultLogger.logRecordBuilder()
2626
.setSpanContext(spanContext)
2727
.setAttributes([:])
28+
.setTimestamp(Date())
2829
.setObservedTimestamp(Date())
2930
.setSeverity(.debug)
3031
.setBody("hello, world")
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Copyright The OpenTelemetry Authors
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
6+
import XCTest
7+
@testable import OpenTelemetrySdk
8+
9+
final class LogRecordBuilderSdkTests: XCTestCase {
10+
private let mockProcessor = LogRecordProcessorMock()
11+
private let testClock = TestClock()
12+
private var sharedState: LoggerSharedState!
13+
14+
override func setUp() {
15+
sharedState = LoggerSharedState(resource: .empty, logLimits: .init(), processors: [mockProcessor], clock: testClock)
16+
}
17+
18+
func testGivenNeitherTimestampNorObservedSet_whenEmit_thenTimestampFromClockAndObservedIsNil() {
19+
// When
20+
LogRecordBuilderSdk(sharedState: sharedState, instrumentationScope: .init(), includeSpanContext: false)
21+
.emit()
22+
23+
// Then
24+
XCTAssertEqual(mockProcessor.onEmitCalledTimes, 1)
25+
let logRecord = mockProcessor.onEmitCalledLogRecord
26+
XCTAssertEqual(logRecord?.timestamp, testClock.now)
27+
XCTAssertNil(logRecord?.observedTimestamp)
28+
}
29+
30+
func testGivenTimestampAndObservedSet_whenEmit_thenRecordHasTimestampAndObserved() {
31+
let timestamp = TestUtils.dateFromNanos(1234000001234)
32+
let observedTimestamp = TestUtils.dateFromNanos(1234000005678)
33+
34+
// Given
35+
let logRecordBuilder = LogRecordBuilderSdk(sharedState: sharedState, instrumentationScope: .init(), includeSpanContext: false)
36+
.setTimestamp(timestamp)
37+
.setObservedTimestamp(observedTimestamp)
38+
39+
// When
40+
logRecordBuilder.emit()
41+
42+
// Then
43+
XCTAssertEqual(mockProcessor.onEmitCalledTimes, 1)
44+
let logRecord = mockProcessor.onEmitCalledLogRecord
45+
XCTAssertEqual(logRecord?.timestamp, timestamp)
46+
XCTAssertEqual(logRecord?.observedTimestamp, observedTimestamp)
47+
}
48+
}

0 commit comments

Comments
 (0)