Skip to content

Commit

Permalink
[internal/comparetest] Support IgnoreResourceAttributeValue option in…
Browse files Browse the repository at this point in the history
… CompareTraces (#17590)
  • Loading branch information
fatsheep9146 authored Jan 17, 2023
1 parent 3f56ff9 commit 6928546
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 0 deletions.
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",
"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

0 comments on commit 6928546

Please sign in to comment.