Skip to content

Commit

Permalink
[otelconsumer] Fix timestamp discrepancy for documents ingested in ot…
Browse files Browse the repository at this point in the history
…el mode (#42725)



* [otelconsumer]: Fix timing discrepancy
  • Loading branch information
khushijain21 authored Feb 18, 2025
1 parent 6763697 commit ef79280
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions libbeat/otelbeat/otelmap/otelmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ func FromMapstr(m mapstr.M) pcommon.Map {
newMap.CopyTo(newVal.SetEmptyMap())
}
case time.Time:
out.PutStr(k, x.Format("2006-01-02T15:04:05.000Z"))
out.PutStr(k, x.UTC().Format("2006-01-02T15:04:05.000Z"))
case []time.Time:
dest := out.PutEmptySlice(k)
for _, i := range v.([]time.Time) {
newVal := dest.AppendEmpty()
newVal.SetStr(i.Format("2006-01-02T15:04:05.000Z"))
newVal.SetStr(i.UTC().Format("2006-01-02T15:04:05.000Z"))
}
default:
out.PutStr(k, fmt.Sprintf("unknown type: %T", x))
Expand Down
4 changes: 2 additions & 2 deletions libbeat/otelbeat/otelmap/otelmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestFromMapstrTime(t *testing.T) {
mapstr_val string
pcommon_val string
}{
{mapstr_val: "2006-01-02T15:04:05+07:00", pcommon_val: "2006-01-02T15:04:05.000Z"},
{mapstr_val: "2006-01-02T15:04:05+07:00", pcommon_val: "2006-01-02T08:04:05.000Z"},
{mapstr_val: "1970-01-01T00:00:00+00:00", pcommon_val: "1970-01-01T00:00:00.000Z"},
}
for _, tc := range tests {
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestFromMapstrSliceTime(t *testing.T) {
mapstr_val string
pcommon_val string
}{
{mapstr_val: "2006-01-02T15:04:05+07:00", pcommon_val: "2006-01-02T15:04:05.000Z"},
{mapstr_val: "2006-01-02T15:04:05+07:00", pcommon_val: "2006-01-02T08:04:05.000Z"},
{mapstr_val: "1970-01-01T00:00:00+00:00", pcommon_val: "1970-01-01T00:00:00.000Z"},
}
var sliceTimes []time.Time
Expand Down
9 changes: 5 additions & 4 deletions libbeat/outputs/otelconsumer/otelconsumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,21 @@ func TestPublish(t *testing.T) {
batch := outest.NewBatch(event3)
batch.Events()[0].Content.Timestamp = time.Date(2025, time.January, 29, 9, 2, 39, 0, time.UTC)

var timestamp string
var bodyTimestamp string
var recordTimestamp string
otelConsumer := makeOtelConsumer(t, func(ctx context.Context, ld plog.Logs) error {
record := ld.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0)
field, ok := record.Body().Map().Get("@timestamp")
recordTimestamp = record.Timestamp().AsTime().UTC().Format("2006-01-02T15:04:05.000Z")
assert.True(t, ok, "timestamp field not found")
timestamp = field.AsString()

bodyTimestamp = field.AsString()
return nil
})

err := otelConsumer.Publish(ctx, batch)
assert.NoError(t, err)
assert.Len(t, batch.Signals, 1)
assert.Equal(t, outest.BatchACK, batch.Signals[0].Tag)
assert.Equal(t, "2025-01-29T09:02:39.000Z", timestamp)
assert.Equal(t, bodyTimestamp, recordTimestamp, "log record timestamp should match body timestamp")
})
}

0 comments on commit ef79280

Please sign in to comment.