Skip to content

Commit a47b9c5

Browse files
authored
[ptracetest] Add additional methods to ignore scope span instrumentation scope information (#32852)
**Description:** Add additional methods to ignore scope span instrumentation scope information: * Version `IgnoreScopeSpanInstrumentationScopeVersion` * Name `IgnoreScopeSpanInstrumentationScopeName` * Attributes `IgnoreScopeSpanInstrumentationScopeAttributeValue` **Testing:** Added to tests.
1 parent e8d997d commit a47b9c5

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

.chloggen/ptracetest_options.yaml

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: ptracetest
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add support for ignore scope span instrumentation scope information
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: [32852]
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/ptracetest/options.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,62 @@ func maskSpanAttributeValue(traces ptrace.Traces, attributeName string) {
174174
}
175175
}
176176

177+
// IgnoreScopeSpanInstrumentationScopeName is a CompareTracesOption that clears value of the scope span instrumentation scope name.
178+
func IgnoreScopeSpanInstrumentationScopeName() CompareTracesOption {
179+
return compareTracesOptionFunc(func(expected, actual ptrace.Traces) {
180+
maskScopeSpanInstrumentationScopeName(expected)
181+
maskScopeSpanInstrumentationScopeName(actual)
182+
})
183+
}
184+
185+
func maskScopeSpanInstrumentationScopeName(traces ptrace.Traces) {
186+
for i := 0; i < traces.ResourceSpans().Len(); i++ {
187+
rs := traces.ResourceSpans().At(i)
188+
for j := 0; j < rs.ScopeSpans().Len(); j++ {
189+
ss := rs.ScopeSpans().At(j)
190+
ss.Scope().SetName("")
191+
}
192+
}
193+
}
194+
195+
// IgnoreScopeSpanInstrumentationScopeVersion is a CompareTracesOption that clears value of the scope span instrumentation scope version.
196+
func IgnoreScopeSpanInstrumentationScopeVersion() CompareTracesOption {
197+
return compareTracesOptionFunc(func(expected, actual ptrace.Traces) {
198+
maskScopeSpanInstrumentationScopeVersion(expected)
199+
maskScopeSpanInstrumentationScopeVersion(actual)
200+
})
201+
}
202+
203+
func maskScopeSpanInstrumentationScopeVersion(traces ptrace.Traces) {
204+
for i := 0; i < traces.ResourceSpans().Len(); i++ {
205+
rs := traces.ResourceSpans().At(i)
206+
for j := 0; j < rs.ScopeSpans().Len(); j++ {
207+
ss := rs.ScopeSpans().At(j)
208+
ss.Scope().SetVersion("")
209+
}
210+
}
211+
}
212+
213+
// IgnoreScopeSpanInstrumentationScopeAttributeValue is a CompareTracesOption that clears value of the scope span instrumentation scope name.
214+
func IgnoreScopeSpanInstrumentationScopeAttributeValue(attributeName string) CompareTracesOption {
215+
return compareTracesOptionFunc(func(expected, actual ptrace.Traces) {
216+
maskScopeSpanInstrumentationScopeAttributeValue(expected, attributeName)
217+
maskScopeSpanInstrumentationScopeAttributeValue(actual, attributeName)
218+
})
219+
}
220+
221+
func maskScopeSpanInstrumentationScopeAttributeValue(traces ptrace.Traces, attributeName string) {
222+
for i := 0; i < traces.ResourceSpans().Len(); i++ {
223+
rs := traces.ResourceSpans().At(i)
224+
for j := 0; j < rs.ScopeSpans().Len(); j++ {
225+
ss := rs.ScopeSpans().At(j)
226+
if _, ok := ss.Scope().Attributes().Get(attributeName); ok {
227+
ss.Scope().Attributes().PutStr(attributeName, "*")
228+
}
229+
}
230+
}
231+
}
232+
177233
// IgnoreStartTimestamp is a CompareTracesOption that clears StartTimestamp fields on all spans.
178234
func IgnoreStartTimestamp() CompareTracesOption {
179235
return compareTracesOptionFunc(func(expected, actual ptrace.Traces) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resourceSpans:
2+
- resource:
3+
attributes:
4+
- key: host.name
5+
value:
6+
stringValue: host1
7+
scopeSpans:
8+
- scope:
9+
name: scope1
10+
version: v0.1.0
11+
attributes:
12+
- key: key1
13+
value:
14+
stringValue: value1
15+
- scope:
16+
name: scope2
17+
version: v0.1.0
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
resourceSpans:
2+
- resource:
3+
attributes:
4+
- key: host.name
5+
value:
6+
stringValue: host1
7+
scopeSpans:
8+
- scope:
9+
name: scope1
10+
version: v0.1.0
11+
attributes:
12+
- key: key1
13+
value:
14+
stringValue: value2
15+
- scope:
16+
name: scope2
17+
version: v0.1.0

pkg/pdatatest/ptracetest/traces_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,30 @@ func TestCompareTraces(t *testing.T) {
122122
withoutOptions: multierr.Combine(
123123
errors.New("resource \"map[host.name:host1]\": missing expected scope: scope3; resource \"map[host.name:host1]\": unexpected scope: scope2"),
124124
),
125+
compareOptions: []CompareTracesOption{
126+
IgnoreScopeSpanInstrumentationScopeName(),
127+
},
128+
withOptions: nil,
125129
},
126130
{
127131
name: "scopespans-scope-version-mismatch",
128132
withoutOptions: multierr.Combine(
129133
errors.New("resource \"map[host.name:host1]\": scope \"scope2\": version doesn't match expected: v0.2.0, actual: v0.1.0"),
130134
),
135+
compareOptions: []CompareTracesOption{
136+
IgnoreScopeSpanInstrumentationScopeVersion(),
137+
},
138+
withOptions: nil,
139+
},
140+
{
141+
name: "scopespans-scope-attributes-mismatch",
142+
withoutOptions: multierr.Combine(
143+
errors.New("resource \"map[host.name:host1]\": scope \"scope1\": attributes don't match expected: map[key1:value2], actual: map[key1:value1]"),
144+
),
145+
compareOptions: []CompareTracesOption{
146+
IgnoreScopeSpanInstrumentationScopeAttributeValue("key1"),
147+
},
148+
withOptions: nil,
131149
},
132150
{
133151
name: "scopespans-spans-amount-unequal",
@@ -462,6 +480,34 @@ func TestCompareScopeSpans(t *testing.T) {
462480
}(),
463481
err: errors.New("name doesn't match expected: scope1, actual: scope2"),
464482
},
483+
{
484+
name: "scope-version-mismatch",
485+
expected: func() ptrace.ScopeSpans {
486+
ss := ptrace.NewScopeSpans()
487+
ss.Scope().SetVersion("1")
488+
return ss
489+
}(),
490+
actual: func() ptrace.ScopeSpans {
491+
ss := ptrace.NewScopeSpans()
492+
ss.Scope().SetVersion("2")
493+
return ss
494+
}(),
495+
err: errors.New("version doesn't match expected: 1, actual: 2"),
496+
},
497+
{
498+
name: "scope-attributes-mismatch",
499+
expected: func() ptrace.ScopeSpans {
500+
ss := ptrace.NewScopeSpans()
501+
ss.Scope().Attributes().PutStr("foo", "bar")
502+
return ss
503+
}(),
504+
actual: func() ptrace.ScopeSpans {
505+
ss := ptrace.NewScopeSpans()
506+
ss.Scope().Attributes().PutStr("foo", "foobar")
507+
return ss
508+
}(),
509+
err: errors.New("attributes don't match expected: map[foo:bar], actual: map[foo:foobar]"),
510+
},
465511
{
466512
name: "spans-number-mismatch",
467513
expected: func() ptrace.ScopeSpans {

0 commit comments

Comments
 (0)