Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/pdatatest/plogtest] Add an option to ignore log timestamp #32540

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .chloggen/plogtest-ignore-timestamp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: pkg/pdatatest/plogtest

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add an option to ignore log timestamp

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [32540]

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
8 changes: 8 additions & 0 deletions pkg/pdatatest/plogtest/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ func TestCompareLogs(t *testing.T) {
withoutOptions: errors.New(`resource "map[]": scope "collector": log record "map[]": observed timestamp doesn't match expected: 11651379494838206465, actual: 11651379494838206464`),
withOptions: nil,
},
{
name: "ignore-timestamp",
compareOptions: []CompareLogsOption{
IgnoreTimestamp(),
},
withoutOptions: errors.New(`resource "map[]": scope "collector": log record "map[]": timestamp doesn't match expected: 11651379494838206465, actual: 11651379494838206464`),
withOptions: nil,
},
}

for _, tc := range tcs {
Expand Down
21 changes: 21 additions & 0 deletions pkg/pdatatest/plogtest/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ func (opt ignoreResourceAttributeValue) maskLogsResourceAttributeValue(metrics p
}
}

func IgnoreTimestamp() CompareLogsOption {
return compareLogsOptionFunc(func(expected, actual plog.Logs) {
now := pcommon.NewTimestampFromTime(time.Now())
maskTimestamp(expected, now)
maskTimestamp(actual, now)
})
}

func maskTimestamp(logs plog.Logs, ts pcommon.Timestamp) {
rls := logs.ResourceLogs()
for i := 0; i < logs.ResourceLogs().Len(); i++ {
sls := rls.At(i).ScopeLogs()
for j := 0; j < sls.Len(); j++ {
lrs := sls.At(j).LogRecords()
for k := 0; k < lrs.Len(); k++ {
lrs.At(k).SetTimestamp(ts)
}
}
}
}

func IgnoreObservedTimestamp() CompareLogsOption {
return compareLogsOptionFunc(func(expected, actual plog.Logs) {
now := pcommon.NewTimestampFromTime(time.Now())
Expand Down
18 changes: 18 additions & 0 deletions pkg/pdatatest/plogtest/testdata/ignore-timestamp/actual.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
resourceLogs:
- resource: {}
schemaUrl: https://opentelemetry.io/schemas/1.6.1
scopeLogs:
- logRecords:
- body:
stringValue: test
flags: 1
observedTimeUnixNano: "11651379494838206464"
severityNumber: 9
severityText: TEST
spanId: ""
timeUnixNano: "11651379494838206464"
traceId: ""
schemaUrl: https://opentelemetry.io/schemas/1.6.1
scope:
name: collector
version: v0.1.0
18 changes: 18 additions & 0 deletions pkg/pdatatest/plogtest/testdata/ignore-timestamp/expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
resourceLogs:
- resource: {}
schemaUrl: https://opentelemetry.io/schemas/1.6.1
scopeLogs:
- logRecords:
- body:
stringValue: test
flags: 1
observedTimeUnixNano: "11651379494838206464"
severityNumber: 9
severityText: TEST
spanId: ""
timeUnixNano: "11651379494838206465"
traceId: ""
schemaUrl: https://opentelemetry.io/schemas/1.6.1
scope:
name: collector
version: v0.1.0