Skip to content

Commit c5a0ed2

Browse files
authored
[pkg/pdatatest/plogtest] Add an option to ignore log timestamp (#32540)
We have an option to ignore observed timestamp, but sometimes it's needed to ignore timestamp as well
1 parent 5bdedd0 commit c5a0ed2

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/pdatatest/plogtest
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add an option to ignore log timestamp
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [32540]
14+
15+
# If your change doesn't affect end users or the exported elements of any package,
16+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
17+
# Optional: The change log or logs in which this entry should be included.
18+
# e.g. '[user]' or '[user, api]'
19+
# Include 'user' if the change is relevant to end users.
20+
# Include 'api' if there is a change to a library API.
21+
# Default: '[user]'
22+
change_logs: [api]

pkg/pdatatest/plogtest/logs_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ func TestCompareLogs(t *testing.T) {
134134
withoutOptions: errors.New(`resource "map[]": scope "collector": log record "map[]": observed timestamp doesn't match expected: 11651379494838206465, actual: 11651379494838206464`),
135135
withOptions: nil,
136136
},
137+
{
138+
name: "ignore-timestamp",
139+
compareOptions: []CompareLogsOption{
140+
IgnoreTimestamp(),
141+
},
142+
withoutOptions: errors.New(`resource "map[]": scope "collector": log record "map[]": timestamp doesn't match expected: 11651379494838206465, actual: 11651379494838206464`),
143+
withOptions: nil,
144+
},
137145
}
138146

139147
for _, tc := range tcs {

pkg/pdatatest/plogtest/options.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ func (opt ignoreResourceAttributeValue) maskLogsResourceAttributeValue(metrics p
4949
}
5050
}
5151

52+
func IgnoreTimestamp() CompareLogsOption {
53+
return compareLogsOptionFunc(func(expected, actual plog.Logs) {
54+
now := pcommon.NewTimestampFromTime(time.Now())
55+
maskTimestamp(expected, now)
56+
maskTimestamp(actual, now)
57+
})
58+
}
59+
60+
func maskTimestamp(logs plog.Logs, ts pcommon.Timestamp) {
61+
rls := logs.ResourceLogs()
62+
for i := 0; i < logs.ResourceLogs().Len(); i++ {
63+
sls := rls.At(i).ScopeLogs()
64+
for j := 0; j < sls.Len(); j++ {
65+
lrs := sls.At(j).LogRecords()
66+
for k := 0; k < lrs.Len(); k++ {
67+
lrs.At(k).SetTimestamp(ts)
68+
}
69+
}
70+
}
71+
}
72+
5273
func IgnoreObservedTimestamp() CompareLogsOption {
5374
return compareLogsOptionFunc(func(expected, actual plog.Logs) {
5475
now := pcommon.NewTimestampFromTime(time.Now())
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
resourceLogs:
2+
- resource: {}
3+
schemaUrl: https://opentelemetry.io/schemas/1.6.1
4+
scopeLogs:
5+
- logRecords:
6+
- body:
7+
stringValue: test
8+
flags: 1
9+
observedTimeUnixNano: "11651379494838206464"
10+
severityNumber: 9
11+
severityText: TEST
12+
spanId: ""
13+
timeUnixNano: "11651379494838206464"
14+
traceId: ""
15+
schemaUrl: https://opentelemetry.io/schemas/1.6.1
16+
scope:
17+
name: collector
18+
version: v0.1.0
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
resourceLogs:
2+
- resource: {}
3+
schemaUrl: https://opentelemetry.io/schemas/1.6.1
4+
scopeLogs:
5+
- logRecords:
6+
- body:
7+
stringValue: test
8+
flags: 1
9+
observedTimeUnixNano: "11651379494838206464"
10+
severityNumber: 9
11+
severityText: TEST
12+
spanId: ""
13+
timeUnixNano: "11651379494838206465"
14+
traceId: ""
15+
schemaUrl: https://opentelemetry.io/schemas/1.6.1
16+
scope:
17+
name: collector
18+
version: v0.1.0

0 commit comments

Comments
 (0)