Skip to content

Commit de81b9b

Browse files
sakulaliJaredTan95
authored andcommitted
[pkg/pdatatest] Ignore span timestamps (open-telemetry#27798)
**Description:** Support ignore timestamps in span comparisons for pdatatest. **Link to tracking Issue:** open-telemetry#27688 **Testing:** make chlog-validate go test for pkg/pdatatest **Documentation:** Add usage for `ptracetest.IgnoreStartTimestamp()` and `ptracetest.IgnoreEndTimestamp()`
1 parent 745c9c6 commit de81b9b

File tree

8 files changed

+180
-2
lines changed

8 files changed

+180
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "support ignore timestamps in span comparisons for pdatatest"
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: [27688]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

pkg/pdatatest/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestMetricsScraper(t *testing.T) {
2424
require.NoError(err)
2525

2626
require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, actualMetrics, pmetrictest.IgnoreStartTimestamp(),
27-
pmetrictest.IgnoreTimestamp()))))
27+
pmetrictest.IgnoreTimestamp()))
2828
}
2929
```
3030

@@ -59,6 +59,7 @@ func TestTraceProcessor(t *testing.T) {
5959
expectedTraces, err := readTraces(filepath.Join("testdata", "traces", "expected.json"))
6060
require.NoError(t, err)
6161

62-
require.NoError(t, ptracetest.CompareTraces(expectedTraces, actualTraces))
62+
require.NoError(t, ptracetest.CompareTraces(expectedTraces, actualTraces, ptracetest.IgnoreStartTimestamp(),
63+
ptracetest.IgnoreEndTimestamp()))
6364
}
6465
```

pkg/pdatatest/ptracetest/options.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ package ptracetest // import "github.com/open-telemetry/opentelemetry-collector-
55

66
import (
77
"bytes"
8+
"time"
89

10+
"go.opentelemetry.io/collector/pdata/pcommon"
911
"go.opentelemetry.io/collector/pdata/ptrace"
1012

1113
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/internal"
@@ -126,3 +128,47 @@ func sortSpanSlices(ts ptrace.Traces) {
126128
}
127129
}
128130
}
131+
132+
// IgnoreStartTimestamp is a CompareTracesOption that clears StartTimestamp fields on all spans.
133+
func IgnoreStartTimestamp() CompareTracesOption {
134+
return compareTracesOptionFunc(func(expected, actual ptrace.Traces) {
135+
now := pcommon.NewTimestampFromTime(time.Now())
136+
maskStartTimestamp(expected, now)
137+
maskStartTimestamp(actual, now)
138+
})
139+
}
140+
141+
func maskStartTimestamp(traces ptrace.Traces, ts pcommon.Timestamp) {
142+
for i := 0; i < traces.ResourceSpans().Len(); i++ {
143+
rs := traces.ResourceSpans().At(i)
144+
for j := 0; j < rs.ScopeSpans().Len(); j++ {
145+
ss := rs.ScopeSpans().At(j)
146+
for k := 0; k < ss.Spans().Len(); k++ {
147+
span := ss.Spans().At(k)
148+
span.SetStartTimestamp(ts)
149+
}
150+
}
151+
}
152+
}
153+
154+
// IgnoreEndTimestamp is a CompareTracesOption that clears EndTimestamp fields on all spans.
155+
func IgnoreEndTimestamp() CompareTracesOption {
156+
return compareTracesOptionFunc(func(expected, actual ptrace.Traces) {
157+
now := pcommon.NewTimestampFromTime(time.Now())
158+
maskEndTimestamp(expected, now)
159+
maskEndTimestamp(actual, now)
160+
})
161+
}
162+
163+
func maskEndTimestamp(traces ptrace.Traces, ts pcommon.Timestamp) {
164+
for i := 0; i < traces.ResourceSpans().Len(); i++ {
165+
rs := traces.ResourceSpans().At(i)
166+
for j := 0; j < rs.ScopeSpans().Len(); j++ {
167+
ss := rs.ScopeSpans().At(j)
168+
for k := 0; k < ss.Spans().Len(); k++ {
169+
span := ss.Spans().At(k)
170+
span.SetEndTimestamp(ts)
171+
}
172+
}
173+
}
174+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
resourceSpans:
2+
- resource:
3+
attributes:
4+
- key: host.name
5+
value:
6+
stringValue: node1
7+
scopeSpans:
8+
- scope:
9+
name: collector
10+
version: v0.1.0
11+
spans:
12+
- attributes:
13+
- key: key1
14+
value:
15+
stringValue: value1
16+
name: span1
17+
parentSpanId: ""
18+
spanId: fd0da883bb27cd6b
19+
endTimeUnixNano: "11651379494838206464"
20+
status: {}
21+
traceId: 8c8b1765a7b0acf0b66aa4623fcb7bd5
22+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
resourceSpans:
2+
- resource:
3+
attributes:
4+
- key: host.name
5+
value:
6+
stringValue: node1
7+
scopeSpans:
8+
- scope:
9+
name: collector
10+
version: v0.1.0
11+
spans:
12+
- attributes:
13+
- key: key1
14+
value:
15+
stringValue: value1
16+
name: span1
17+
parentSpanId: ""
18+
spanId: fd0da883bb27cd6b
19+
status: {}
20+
traceId: 8c8b1765a7b0acf0b66aa4623fcb7bd5
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
resourceSpans:
2+
- resource:
3+
attributes:
4+
- key: host.name
5+
value:
6+
stringValue: node1
7+
scopeSpans:
8+
- scope:
9+
name: collector
10+
version: v0.1.0
11+
spans:
12+
- attributes:
13+
- key: key1
14+
value:
15+
stringValue: value1
16+
name: span1
17+
parentSpanId: ""
18+
spanId: fd0da883bb27cd6b
19+
startTimeUnixNano: "11651379494838206464"
20+
status: {}
21+
traceId: 8c8b1765a7b0acf0b66aa4623fcb7bd5
22+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
resourceSpans:
2+
- resource:
3+
attributes:
4+
- key: host.name
5+
value:
6+
stringValue: node1
7+
scopeSpans:
8+
- scope:
9+
name: collector
10+
version: v0.1.0
11+
spans:
12+
- attributes:
13+
- key: key1
14+
value:
15+
stringValue: value1
16+
name: span1
17+
parentSpanId: ""
18+
spanId: fd0da883bb27cd6b
19+
status: {}
20+
traceId: 8c8b1765a7b0acf0b66aa4623fcb7bd5

pkg/pdatatest/ptracetest/traces_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ func TestCompareTraces(t *testing.T) {
4848
),
4949
withOptions: nil,
5050
},
51+
{
52+
name: "ignore-start-timestamp",
53+
compareOptions: []CompareTracesOption{
54+
IgnoreStartTimestamp(),
55+
},
56+
withoutOptions: multierr.Combine(
57+
errors.New("resource \"map[host.name:node1]\": scope \"collector\": span \"span1\": start timestamp doesn't match expected: 11651379494838206464, actual: 0"),
58+
),
59+
withOptions: nil,
60+
},
61+
{
62+
name: "ignore-end-timestamp",
63+
compareOptions: []CompareTracesOption{
64+
IgnoreEndTimestamp(),
65+
},
66+
withoutOptions: multierr.Combine(
67+
errors.New("resource \"map[host.name:node1]\": scope \"collector\": span \"span1\": end timestamp doesn't match expected: 11651379494838206464, actual: 0"),
68+
),
69+
withOptions: nil,
70+
},
5171
{
5272
name: "resourcespans-amount-unequal",
5373
withoutOptions: multierr.Combine(

0 commit comments

Comments
 (0)