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

[internal/comparetest] support IgnoreResourceAttributeValue compare option #17590

Merged
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
16 changes: 16 additions & 0 deletions .chloggen/trace-support-ignore-resource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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: internal/comparetest

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: traces support IgnoreResourceAttributeValue compare option

# One or more tracking issues related to the change
issues: [17588]

# (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:
13 changes: 13 additions & 0 deletions internal/comparetest/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type TracesCompareOption interface {
type CompareOption interface {
MetricsCompareOption
LogsCompareOption
TracesCompareOption
}

// IgnoreMetricValues is a MetricsCompareOption that clears all metric values.
Expand Down Expand Up @@ -204,6 +205,18 @@ func (opt ignoreResourceAttributeValue) maskLogsResourceAttributeValue(metrics p
}
}

func (opt ignoreResourceAttributeValue) applyOnTraces(expected, actual ptrace.Traces) {
opt.maskTracesResourceAttributeValue(expected)
opt.maskTracesResourceAttributeValue(actual)
}

func (opt ignoreResourceAttributeValue) maskTracesResourceAttributeValue(traces ptrace.Traces) {
rss := traces.ResourceSpans()
for i := 0; i < rss.Len(); i++ {
opt.maskResourceAttributeValue(rss.At(i).Resource())
}
}

func (opt ignoreResourceAttributeValue) maskResourceAttributeValue(res pcommon.Resource) {
if _, ok := res.Attributes().Get(opt.attributeName); ok {
res.Attributes().Remove(opt.attributeName)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"resourceSpans": [
{
"resource": {
"attributes": [
{
"key": "host.name",
"value": {
"stringValue": "host1"
}
}
],
"droppedAttributesCount": 0
},
"scopeSpans": [
{
"spans": [
{
"attributes": [
{
"key": "testKey1",
dmitryax marked this conversation as resolved.
Show resolved Hide resolved
"value": {
"stringValue": "teststringvalue1"
}
},
{
"key": "testKey2",
"value": {
"stringValue": "teststringvalue2"
}
}
],
"droppedAttributesCount": 0,
"endTimeUnixNano": "11651379494838206464",
"startTimeUnixNano": "11651379494838206464",
"status": {
"code": 0,
"description": ""
},
"events": [
{
"name": "Sub span event",
"attributes": null,
"droppedAttributeCount": 0
}
],
"droppedEventsCount": 0,
"links": [
{
"traceId": "8c8b1765a7b0acf0b66aa4623fcb7bd5",
"spanId": "fd0da883bb27cd6b",
"traceState": "",
"attributes": [
{
"key": "testKey1",
"value": {
"stringValue": "teststringvalue1"
}
},
{
"key": "testKey2",
"value": {
"stringValue": "teststringvalue2"
}
}
],
"droppedAttributesCount": 0
}
],
"droppedLinksCount": 0,
"name": "span1",
"kind": 2,
"parentSpanId": "bcff497b5a47310f",
"traceState": "",
"spanId": "fd0da883bb27cd6b",
"traceId": "8c8b1765a7b0acf0b66aa4623fcb7bd5"
}
],
"scope": {
"name": "collector",
"version": "v0.1.0"
},
"schemaUrl": "https://opentelemetry.io/schemas/1.6.1"
}
],
"schemaUrl": "https://opentelemetry.io/schemas/1.6.1"
},
{
"resource": {
"attributes": [
{
"key": "testKey3",
"value": {
"stringValue": "teststringvalue3"
}
},
{
"key": "testKey4",
"value": {
"stringValue": "teststringvalue4"
}
}
]
},
"scopeSpans": [
{
"spans": [
{
"attributes": [
{
"key": "testKey3",
"value": {
"stringValue": "teststringvalue3"
}
},
{
"key": "testKey4",
"value": {
"stringValue": "teststringvalue4"
}
}
],
"droppedAttributesCount": 0,
"endTimeUnixNano": "11651379494838206464",
"startTimeUnixNano": "11651379494838206464",
"status": {},
"events": [],
"droppedEventsCount": 0,
"links": [],
"droppedLinksCount": 0,
"name": "",
"kind": 2,
"parentSpanId": "",
"traceState": "",
"spanId": "",
"traceId": ""
}
],
"scope": {
"name": "collector",
"version": "v0.1.0"
}
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"resourceSpans": [
{
"resource": {
"attributes": [
{
"key": "host.name",
"value": {
"stringValue": "different-node1"
}
}
],
"droppedAttributesCount": 0
},
"scopeSpans": [
{
"spans": [
{
"attributes": [
{
"key": "testKey1",
"value": {
"stringValue": "teststringvalue1"
}
},
{
"key": "testKey2",
"value": {
"stringValue": "teststringvalue2"
}
}
],
"droppedAttributesCount": 0,
"endTimeUnixNano": "11651379494838206464",
"startTimeUnixNano": "11651379494838206464",
"status": {
"code": 0,
"description": ""
},
"events": [
{
"name": "Sub span event",
"attributes": null,
"droppedAttributeCount": 0
}
],
"droppedEventsCount": 0,
"links": [
{
"traceId": "8c8b1765a7b0acf0b66aa4623fcb7bd5",
"spanId": "fd0da883bb27cd6b",
"traceState": "",
"attributes": [
{
"key": "testKey1",
"value": {
"stringValue": "teststringvalue1"
}
},
{
"key": "testKey2",
"value": {
"stringValue": "teststringvalue2"
}
}
],
"droppedAttributesCount": 0
}
],
"droppedLinksCount": 0,
"name": "span1",
"kind": 2,
"parentSpanId": "bcff497b5a47310f",
"traceState": "",
"spanId": "fd0da883bb27cd6b",
"traceId": "8c8b1765a7b0acf0b66aa4623fcb7bd5"
}
],
"scope": {
"name": "collector",
"version": "v0.1.0"
},
"schemaUrl": "https://opentelemetry.io/schemas/1.6.1"
}
],
"schemaUrl": "https://opentelemetry.io/schemas/1.6.1"
},
{
"resource": {
"attributes": [
{
"key": "testKey3",
"value": {
"stringValue": "teststringvalue3"
}
},
{
"key": "testKey4",
"value": {
"stringValue": "teststringvalue4"
}
}
]
},
"scopeSpans": [
{
"spans": [
{
"attributes": [
{
"key": "testKey3",
"value": {
"stringValue": "teststringvalue3"
}
},
{
"key": "testKey4",
"value": {
"stringValue": "teststringvalue4"
}
}
],
"droppedAttributesCount": 0,
"endTimeUnixNano": "11651379494838206464",
"startTimeUnixNano": "11651379494838206464",
"status": {},
"events": [],
"droppedEventsCount": 0,
"links": [],
"droppedLinksCount": 0,
"name": "",
"kind": 2,
"parentSpanId": "",
"traceState": "",
"spanId": "",
"traceId": ""
}
],
"scope": {
"name": "collector",
"version": "v0.1.0"
}
}
]
}
]
}
19 changes: 19 additions & 0 deletions internal/comparetest/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package comparetest

import (
"errors"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/comparetest/golden"
)
Expand All @@ -33,6 +35,23 @@ func TestCompareTraces(t *testing.T) {
{
name: "equal",
},
{
name: "ignore-one-resource-attribute",
compareOptions: []TracesCompareOption{
IgnoreResourceAttributeValue("host.name"),
},
withoutOptions: expectation{
err: multierr.Combine(
errors.New("missing expected resource with attributes: map[host.name:different-node1]"),
errors.New("extra resource with attributes: map[host.name:host1]"),
),
reason: "An unpredictable resource attribute will cause failures if not ignored.",
},
withOptions: expectation{
err: nil,
reason: "The unpredictable resource attribute was ignored on each resource that carried it.",
},
},
}

for _, tc := range tcs {
Expand Down