-
Notifications
You must be signed in to change notification settings - Fork 0
/
transparency_processor_test.go
178 lines (161 loc) · 5.26 KB
/
transparency_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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package transparencyprocessor
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"github.com/mindtastic/opentelemetry-transparency-processor/internal/filterset"
)
// Common structure for all the Tests
type testCase struct {
skip bool
name string
serviceName string
inputAttributes map[string]interface{}
expectedAttributes map[string]interface{}
}
// runIndividualTestCase is the common logic of passing trace data through a configured tiltAttributes processor.
func runIndividualTestCase(t *testing.T, tt testCase, tp component.TracesProcessor) {
t.Run(tt.name, func(t *testing.T) {
td := generateTraceData(tt.serviceName, tt.name, tt.inputAttributes)
assert.NoError(t, tp.ConsumeTraces(context.Background(), td))
// Ensure that the modified `td` has the tiltAttributes sorted:
sortAttributes(td)
require.Equal(t, generateTraceData(tt.serviceName, tt.name, tt.expectedAttributes), td)
})
}
func generateTraceData(serviceName, spanName string, attrs map[string]interface{}) ptrace.Traces {
td := ptrace.NewTraces()
rs := td.ResourceSpans().AppendEmpty()
if serviceName != "" {
rs.Resource().Attributes().UpsertString(conventions.AttributeServiceName, serviceName)
}
span := rs.ScopeSpans().AppendEmpty().Spans().AppendEmpty()
span.SetName(spanName)
pcommon.NewMapFromRaw(attrs).CopyTo(span.Attributes())
span.Attributes().Sort()
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 TestProcessTraces(t *testing.T) {
testCases := []testCase{
{
skip: true,
name: "do nothing",
serviceName: "testing",
inputAttributes: map[string]interface{}{},
expectedAttributes: map[string]interface{}{},
},
{
skip: true,
name: "do nothing without host and path",
serviceName: "linkerd-proxy",
inputAttributes: map[string]interface{}{},
expectedAttributes: map[string]interface{}{},
},
{
skip: false,
name: "add tiltAttributes",
serviceName: "linkerd-proxy",
inputAttributes: map[string]interface{}{
"http.host": "testHost",
"http.path": "testPath",
},
expectedAttributes: map[string]interface{}{
"http.host": "testHost",
"http.path": "testPath",
"tilt.categories": "testing",
},
},
}
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
tp, err := factory.CreateTracesProcessor(context.Background(), componenttest.NewNopProcessorCreateSettings(), cfg, consumertest.NewNop())
require.Nil(t, err)
require.NotNil(t, tp)
for _, tt := range testCases {
if tt.skip {
continue
}
runIndividualTestCase(t, tt, tp)
}
}
func BenchmarkProcessTraces(b *testing.B) {
testCases := []testCase{
{
name: "apply_to_span_with_no_attrs",
inputAttributes: map[string]interface{}{},
expectedAttributes: map[string]interface{}{
"attribute1": 123,
},
},
{
name: "apply_to_span_with_attr",
inputAttributes: map[string]interface{}{
"NoModification": false,
},
expectedAttributes: map[string]interface{}{
"attribute1": 123,
"NoModification": false,
},
},
{
name: "dont_apply",
inputAttributes: map[string]interface{}{},
expectedAttributes: map[string]interface{}{},
},
}
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
tp, err := factory.CreateTracesProcessor(context.Background(), componenttest.NewNopProcessorCreateSettings(), cfg, consumertest.NewNop())
require.Nil(b, err)
require.NotNil(b, tp)
for _, tt := range testCases {
td := generateTraceData(tt.serviceName, tt.name, tt.inputAttributes)
b.Run(tt.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
assert.NoError(b, tp.ConsumeTraces(context.Background(), td))
}
})
// Ensure that the modified `td` has the tiltAttributes sorted:
sortAttributes(td)
require.Equal(b, generateTraceData(tt.serviceName, tt.name, tt.expectedAttributes), td)
}
}
func createConfig(matchType filterset.MatchType) *filterset.Config {
return &filterset.Config{
MatchType: matchType,
}
}