Skip to content

Commit

Permalink
[pkg/pdatatest] Ignore span timestamps (#27798)
Browse files Browse the repository at this point in the history
**Description:** 
Support ignore timestamps in span comparisons for pdatatest.

**Link to tracking Issue:** 

#27688

**Testing:**
make chlog-validate
go test for pkg/pdatatest

**Documentation:**
Add usage for `ptracetest.IgnoreStartTimestamp()` and
`ptracetest.IgnoreEndTimestamp()`
  • Loading branch information
sakulali committed Oct 17, 2023
1 parent 22ffdf5 commit c878560
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .chloggen/pdatatest-ignore-span-timestamps.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "support ignore timestamps in span comparisons for pdatatest"

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

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# 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: []
5 changes: 3 additions & 2 deletions pkg/pdatatest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestMetricsScraper(t *testing.T) {
require.NoError(err)

require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, actualMetrics, pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp()))))
pmetrictest.IgnoreTimestamp()))
}
```

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

require.NoError(t, ptracetest.CompareTraces(expectedTraces, actualTraces))
require.NoError(t, ptracetest.CompareTraces(expectedTraces, actualTraces, ptracetest.IgnoreStartTimestamp(),
ptracetest.IgnoreEndTimestamp()))
}
```
46 changes: 46 additions & 0 deletions pkg/pdatatest/ptracetest/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package ptracetest // import "github.com/open-telemetry/opentelemetry-collector-

import (
"bytes"
"time"

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/internal"
Expand Down Expand Up @@ -126,3 +128,47 @@ func sortSpanSlices(ts ptrace.Traces) {
}
}
}

// IgnoreStartTimestamp is a CompareTracesOption that clears StartTimestamp fields on all spans.
func IgnoreStartTimestamp() CompareTracesOption {
return compareTracesOptionFunc(func(expected, actual ptrace.Traces) {
now := pcommon.NewTimestampFromTime(time.Now())
maskStartTimestamp(expected, now)
maskStartTimestamp(actual, now)
})
}

func maskStartTimestamp(traces ptrace.Traces, ts pcommon.Timestamp) {
for i := 0; i < traces.ResourceSpans().Len(); i++ {
rs := traces.ResourceSpans().At(i)
for j := 0; j < rs.ScopeSpans().Len(); j++ {
ss := rs.ScopeSpans().At(j)
for k := 0; k < ss.Spans().Len(); k++ {
span := ss.Spans().At(k)
span.SetStartTimestamp(ts)
}
}
}
}

// IgnoreEndTimestamp is a CompareTracesOption that clears EndTimestamp fields on all spans.
func IgnoreEndTimestamp() CompareTracesOption {
return compareTracesOptionFunc(func(expected, actual ptrace.Traces) {
now := pcommon.NewTimestampFromTime(time.Now())
maskEndTimestamp(expected, now)
maskEndTimestamp(actual, now)
})
}

func maskEndTimestamp(traces ptrace.Traces, ts pcommon.Timestamp) {
for i := 0; i < traces.ResourceSpans().Len(); i++ {
rs := traces.ResourceSpans().At(i)
for j := 0; j < rs.ScopeSpans().Len(); j++ {
ss := rs.ScopeSpans().At(j)
for k := 0; k < ss.Spans().Len(); k++ {
span := ss.Spans().At(k)
span.SetEndTimestamp(ts)
}
}
}
}
22 changes: 22 additions & 0 deletions pkg/pdatatest/ptracetest/testdata/ignore-end-timestamp/actual.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
resourceSpans:
- resource:
attributes:
- key: host.name
value:
stringValue: node1
scopeSpans:
- scope:
name: collector
version: v0.1.0
spans:
- attributes:
- key: key1
value:
stringValue: value1
name: span1
parentSpanId: ""
spanId: fd0da883bb27cd6b
endTimeUnixNano: "11651379494838206464"
status: {}
traceId: 8c8b1765a7b0acf0b66aa4623fcb7bd5

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resourceSpans:
- resource:
attributes:
- key: host.name
value:
stringValue: node1
scopeSpans:
- scope:
name: collector
version: v0.1.0
spans:
- attributes:
- key: key1
value:
stringValue: value1
name: span1
parentSpanId: ""
spanId: fd0da883bb27cd6b
status: {}
traceId: 8c8b1765a7b0acf0b66aa4623fcb7bd5
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
resourceSpans:
- resource:
attributes:
- key: host.name
value:
stringValue: node1
scopeSpans:
- scope:
name: collector
version: v0.1.0
spans:
- attributes:
- key: key1
value:
stringValue: value1
name: span1
parentSpanId: ""
spanId: fd0da883bb27cd6b
startTimeUnixNano: "11651379494838206464"
status: {}
traceId: 8c8b1765a7b0acf0b66aa4623fcb7bd5

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resourceSpans:
- resource:
attributes:
- key: host.name
value:
stringValue: node1
scopeSpans:
- scope:
name: collector
version: v0.1.0
spans:
- attributes:
- key: key1
value:
stringValue: value1
name: span1
parentSpanId: ""
spanId: fd0da883bb27cd6b
status: {}
traceId: 8c8b1765a7b0acf0b66aa4623fcb7bd5
20 changes: 20 additions & 0 deletions pkg/pdatatest/ptracetest/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ func TestCompareTraces(t *testing.T) {
),
withOptions: nil,
},
{
name: "ignore-start-timestamp",
compareOptions: []CompareTracesOption{
IgnoreStartTimestamp(),
},
withoutOptions: multierr.Combine(
errors.New("resource \"map[host.name:node1]\": scope \"collector\": span \"span1\": start timestamp doesn't match expected: 11651379494838206464, actual: 0"),
),
withOptions: nil,
},
{
name: "ignore-end-timestamp",
compareOptions: []CompareTracesOption{
IgnoreEndTimestamp(),
},
withoutOptions: multierr.Combine(
errors.New("resource \"map[host.name:node1]\": scope \"collector\": span \"span1\": end timestamp doesn't match expected: 11651379494838206464, actual: 0"),
),
withOptions: nil,
},
{
name: "resourcespans-amount-unequal",
withoutOptions: multierr.Combine(
Expand Down

0 comments on commit c878560

Please sign in to comment.