-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor_test.go
247 lines (239 loc) · 7.41 KB
/
processor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package spanignoreprocessor
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/processortest"
)
type testCase struct {
name string
serviceName string
spanInputAttributes map[string]interface{}
spanExpectedAttributes map[string]interface{}
resourceInputAttributes map[string]interface{}
resourceExpectedAttributes map[string]interface{}
spanInputEvents []string
spanExpectedEvents []string
}
func runIndividualTestCase(t *testing.T, tt testCase, tp processor.Traces) {
t.Run(tt.name, func(t *testing.T) {
td := generateTraceData(tt.name, tt.spanInputAttributes, tt.resourceInputAttributes, tt.spanInputEvents)
assert.NoError(t, tp.ConsumeTraces(context.Background(), td))
// Ensure that the modified `td` has the attributes sorted:
sortAttributes(td)
require.Equal(t, generateTraceData(tt.name, tt.spanExpectedAttributes, tt.resourceExpectedAttributes, tt.spanExpectedEvents), td)
})
}
func generateTraceData(
spanName string,
spanAttributes map[string]interface{},
resourceAttributes map[string]interface{},
spanEvents []string,
) ptrace.Traces {
td := ptrace.NewTraces()
rs := td.ResourceSpans().AppendEmpty()
if len(resourceAttributes) > 0 {
rs.Resource().Attributes().FromRaw(resourceAttributes)
rs.Resource().Attributes().Sort()
}
scopeSpan := rs.ScopeSpans().AppendEmpty()
span := scopeSpan.Spans().AppendEmpty()
span.SetName(spanName)
span.SetTraceID([16]byte{1, 2, 3, 4})
span.Attributes().FromRaw(spanAttributes)
span.Attributes().Sort()
for _, event := range spanEvents {
spanEvent := span.Events().AppendEmpty()
spanEvent.SetName(event)
}
return td
}
func sortAttributes(td ptrace.Traces) {
rss := td.ResourceSpans()
for i := 0; i < rss.Len(); i++ {
rs := rss.At(i)
rs.Resource().Attributes().Sort()
ilss := rs.ScopeSpans()
for j := 0; j < ilss.Len(); j++ {
spans := ilss.At(j).Spans()
for k := 0; k < spans.Len(); k++ {
spans.At(k).Attributes().Sort()
}
}
}
}
func TestIgnoreSpans(t *testing.T) {
testCases := []struct {
test testCase
config *Config
}{
{
test: testCase{
name: "Remove span attribute whose Key is listed in IgnoredAttributes property",
serviceName: "admin_service",
spanInputAttributes: map[string]interface{}{
"account.id": "007",
"http.status_code": 200,
"account.password": "AKdhcjs^&xva",
},
spanExpectedAttributes: map[string]interface{}{
"account.id": "007",
"http.status_code": 200,
},
},
config: &Config{
IgnoredAttributes: AttributesConfiguration{
IncludeResources: false,
Attributes: []string{"account.password"},
},
},
},
{
test: testCase{
name: "Remove resource attribute whose Key is listed in IgnoredAttributes property",
serviceName: "admin_service",
resourceInputAttributes: map[string]interface{}{
"service.namespace": "mpt-001",
"service.instance.id": "0x3467Sdfjk",
"service.owner.name": "alpha-team",
"service.owner.hash": "GfghjW$dshjkl32UYK",
},
resourceExpectedAttributes: map[string]interface{}{
"service.namespace": "mpt-001",
"service.instance.id": "0x3467Sdfjk",
"service.owner.name": "alpha-team",
},
},
config: &Config{
IgnoredAttributes: AttributesConfiguration{
IncludeResources: true,
Attributes: []string{"service.owner.hash"},
},
},
},
{
test: testCase{
name: "Ignore resource attribute when IncludeResources is false",
serviceName: "admin_service",
resourceInputAttributes: map[string]interface{}{
"service.namespace": "mpt-001",
"service.instance.id": "0x3467Sdfjk",
"service.owner.name": "alpha-team",
"service.owner.hash": "GfghjW$dshjkl32UYK",
},
resourceExpectedAttributes: map[string]interface{}{
"service.namespace": "mpt-001",
"service.instance.id": "0x3467Sdfjk",
"service.owner.name": "alpha-team",
"service.owner.hash": "GfghjW$dshjkl32UYK",
},
},
config: &Config{
IgnoredAttributes: AttributesConfiguration{
IncludeResources: false,
Attributes: []string{"service.owner.hash"},
},
},
},
{
test: testCase{
name: "Remove span and resource attributes",
serviceName: "admin_service",
resourceInputAttributes: map[string]interface{}{
"service.owner.name": "alpha-team",
"service.owner.hash": "GfghjW$dshjkl32UYK",
},
resourceExpectedAttributes: map[string]interface{}{
"service.owner.name": "alpha-team",
},
spanInputAttributes: map[string]interface{}{
"account.id": "007",
"http.status_code": 200,
"account.password": "AKdhcjs^&xva",
},
spanExpectedAttributes: map[string]interface{}{
"account.id": "007",
"http.status_code": 200,
},
},
config: &Config{
IgnoredAttributes: AttributesConfiguration{
IncludeResources: true,
Attributes: []string{
"service.owner.hash",
"account.password",
},
},
},
},
}
for _, v := range testCases {
factory := NewFactory()
tp, err := factory.CreateTracesProcessor(context.Background(), processortest.NewNopCreateSettings(), v.config, consumertest.NewNop())
require.Nil(t, err)
require.NotNil(t, tp)
runIndividualTestCase(t, v.test, tp)
}
}
func TestSpanEventsDeletion(t *testing.T) {
testCases := []struct {
test testCase
config *Config
}{
{
test: testCase{
name: "Delete span events based on regular expression",
serviceName: "admin_service",
spanInputAttributes: map[string]interface{}{
"account.id": "007",
"http.status_code": 200,
},
spanExpectedAttributes: map[string]interface{}{
"account.id": "007",
"http.status_code": 200,
},
spanInputEvents: []string{"Update user preferences", "Cache user token:eyJhbVCJ9.eyJzdWIiOiIxMjM0NTYlIi5MDIyfQ.SflK", "Cancelled wait due to external signal"},
spanExpectedEvents: []string{"Update user preferences", "Cancelled wait due to external signal"},
},
config: &Config{
IgnoredEvents: []string{
//regex that match jwt token
`([\w-]*\.[\w-]*\.[\w-]*$)`,
},
},
},
{
test: testCase{
name: "Delete span events based on regular expression (ignore events when regex doesn't match)",
serviceName: "admin_service",
spanInputAttributes: map[string]interface{}{
"account.id": "007",
"http.status_code": 200,
},
spanExpectedAttributes: map[string]interface{}{
"account.id": "007",
"http.status_code": 200,
},
spanInputEvents: []string{"Update user preferences", "Cancelled wait due to external signal"},
spanExpectedEvents: []string{"Update user preferences", "Cancelled wait due to external signal"},
},
config: &Config{
IgnoredEvents: []string{
//regex that match jwt token
`^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$`,
},
},
},
}
for _, v := range testCases {
factory := NewFactory()
tp, err := factory.CreateTracesProcessor(context.Background(), processortest.NewNopCreateSettings(), v.config, consumertest.NewNop())
require.Nil(t, err)
require.NotNil(t, tp)
runIndividualTestCase(t, v.test, tp)
}
}