Skip to content

Commit

Permalink
(logs exporter) Fall back to observed time if timestamp is not set (#640
Browse files Browse the repository at this point in the history
)
  • Loading branch information
damemi committed Jun 9, 2023
1 parent 7e416c3 commit 9c6bfe4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"scope": {},
"logRecords": [
{
"timeUnixNano": "1650984816000000000",
"observedTimeUnixNano": "1650984816000000000",
"body": {
"stringValue": "127.0.0.1 - - [26/Apr/2022:22:53:36 +0800] \"GET / HTTP/1.1\" 200 1247"
},
Expand Down
12 changes: 8 additions & 4 deletions exporter/collector/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,16 @@ func (l logMapper) logToSplitEntries(
Resource: mr,
}

// if timestamp has not been explicitly initialized, default to current time
// TODO: figure out how to fall back to observed_time_unix_nano as recommended
// (see https://github.com/open-telemetry/opentelemetry-proto/blob/4abbb78/opentelemetry/proto/logs/v1/logs.proto#L176-L179)
entry.Timestamp = log.Timestamp().AsTime()
if log.Timestamp() == 0 {
entry.Timestamp = processTime
// if timestamp is unset, fall back to observed_time_unix_nano as recommended
// (see https://github.com/open-telemetry/opentelemetry-proto/blob/4abbb78/opentelemetry/proto/logs/v1/logs.proto#L176-L179)
if log.ObservedTimestamp() != 0 {
entry.Timestamp = log.ObservedTimestamp().AsTime()
} else {
// if observed_time is 0, use the current time
entry.Timestamp = processTime
}
}

// build our own map off OTel attributes so we don't have to call .Get() for each special case
Expand Down
17 changes: 17 additions & 0 deletions exporter/collector/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ func TestLogMapping(t *testing.T) {
},
maxEntrySize: defaultMaxEntrySize,
},
{
name: "log with empty timestamp, but set observed_time",
log: func() plog.LogRecord {
log := plog.NewLogRecord()
log.SetObservedTimestamp(pcommon.NewTimestampFromTime(testSampleTime))
return log
},
mr: func() *monitoredres.MonitoredResource {
return nil
},
expectedEntries: []logging.Entry{
{
Timestamp: testSampleTime,
},
},
maxEntrySize: defaultMaxEntrySize,
},
{
name: "log body with string value",
mr: func() *monitoredres.MonitoredResource {
Expand Down

0 comments on commit 9c6bfe4

Please sign in to comment.