1
1
package spancounter
2
2
3
3
import (
4
+ "context"
4
5
"testing"
5
6
6
7
"github.com/stretchr/testify/assert"
7
8
"go.opentelemetry.io/collector/pdata/ptrace"
9
+ conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
8
10
"go.uber.org/zap"
9
11
)
10
12
@@ -47,6 +49,109 @@ func TestNewProcessor(t *testing.T) {
47
49
assert .Equal (t , "custom-tenant-id" , p .tenantIDAttributeKey )
48
50
}
49
51
52
+ func TestCreateTenantsMap (t * testing.T ) {
53
+ c := & Config {
54
+ TenantConfigs : []TenantConfig {
55
+ {
56
+ // No tenant id. Will skip this whole config.
57
+ ServiceConfigs : []ServiceConfig {
58
+ {
59
+ ServiceName : "example-service" ,
60
+ SpanConfigs : []SpanConfig {
61
+ {
62
+ Label : "example-label" ,
63
+ SpanName : "span-1" ,
64
+ },
65
+ },
66
+ },
67
+ },
68
+ },
69
+ {
70
+ TenantId : "example-tenant" ,
71
+ ServiceConfigs : []ServiceConfig {
72
+ {
73
+ ServiceName : "example-service-1" ,
74
+ SpanConfigs : []SpanConfig {
75
+ {
76
+ Label : "example-label-1" ,
77
+ SpanName : "span-1" ,
78
+ },
79
+ },
80
+ },
81
+ {
82
+ // No service name. Will skip
83
+ SpanConfigs : []SpanConfig {
84
+ {
85
+ Label : "example-label-2" ,
86
+ SpanName : "span-2" ,
87
+ },
88
+ },
89
+ },
90
+ },
91
+ },
92
+ // well formed config
93
+ {
94
+ TenantId : "example-tenant-2" ,
95
+ ServiceConfigs : []ServiceConfig {
96
+ {
97
+ ServiceName : "example-service-10" ,
98
+ SpanConfigs : []SpanConfig {
99
+ {
100
+ Label : "example-label-10" ,
101
+ SpanName : "span-10" ,
102
+ },
103
+ },
104
+ },
105
+ {
106
+ ServiceName : "example-service-11" ,
107
+ SpanConfigs : []SpanConfig {
108
+ {
109
+ Label : "label-11" ,
110
+ SpanName : "span-11" ,
111
+ SpanAttributes : []SpanAttribute {
112
+ {Key : "k1" },
113
+ {Key : "k2" , Value : "v2" },
114
+ },
115
+ },
116
+ },
117
+ },
118
+ },
119
+ },
120
+ },
121
+ }
122
+
123
+ expectedMap := map [string ]map [string ][]SpanConfig {
124
+ "example-tenant" : {
125
+ "example-service-1" : {
126
+ {
127
+ Label : "example-label-1" ,
128
+ SpanName : "span-1" ,
129
+ },
130
+ },
131
+ },
132
+ "example-tenant-2" : {
133
+ "example-service-10" : {
134
+ {
135
+ Label : "example-label-10" ,
136
+ SpanName : "span-10" ,
137
+ },
138
+ },
139
+ "example-service-11" : {
140
+ {
141
+ Label : "label-11" ,
142
+ SpanName : "span-11" ,
143
+ SpanAttributes : []SpanAttribute {
144
+ {Key : "k1" },
145
+ {Key : "k2" , Value : "v2" },
146
+ },
147
+ },
148
+ },
149
+ },
150
+ }
151
+
152
+ assert .Equal (t , expectedMap , createTenantsMap (c ))
153
+ }
154
+
50
155
func TestSpanMatchesConfig (t * testing.T ) {
51
156
span := ptrace .NewSpan ()
52
157
span .SetName ("span1" )
@@ -67,10 +172,35 @@ func TestSpanMatchesConfig(t *testing.T) {
67
172
}
68
173
assert .True (t , spanMatchesConfig (span , sc ))
69
174
175
+ sc = SpanConfig {
176
+ SpanAttributes : []SpanAttribute {
177
+ {Key : "a1" },
178
+ },
179
+ }
180
+ assert .True (t , spanMatchesConfig (span , sc ))
181
+
182
+ sc = SpanConfig {
183
+ SpanName : "span1" ,
184
+ SpanAttributes : []SpanAttribute {
185
+ {Key : "a1" , Value : "v1" },
186
+ },
187
+ }
188
+ assert .True (t , spanMatchesConfig (span , sc ))
189
+
70
190
sc = SpanConfig {
71
191
SpanName : "span1" ,
72
192
SpanAttributes : []SpanAttribute {
73
193
{Key : "a1" , Value : "v1" },
194
+ {Key : "a2" , Value : "v2" },
195
+ },
196
+ }
197
+ assert .True (t , spanMatchesConfig (span , sc ))
198
+
199
+ sc = SpanConfig {
200
+ SpanName : "span1" ,
201
+ SpanAttributes : []SpanAttribute {
202
+ {Key : "a1" },
203
+ {Key : "a2" , Value : "v2" },
74
204
},
75
205
}
76
206
assert .True (t , spanMatchesConfig (span , sc ))
@@ -82,4 +212,87 @@ func TestSpanMatchesConfig(t *testing.T) {
82
212
},
83
213
}
84
214
assert .False (t , spanMatchesConfig (span , sc ))
215
+
216
+ sc = SpanConfig {
217
+ SpanAttributes : []SpanAttribute {
218
+ {Key : "a1" , Value : "v3" },
219
+ },
220
+ }
221
+ assert .False (t , spanMatchesConfig (span , sc ))
222
+
223
+ sc = SpanConfig {
224
+ SpanAttributes : []SpanAttribute {
225
+ {Key : "a3" },
226
+ },
227
+ }
228
+ assert .False (t , spanMatchesConfig (span , sc ))
229
+ }
230
+
231
+ func TestProcessTraces (t * testing.T ) {
232
+ // Create test traces
233
+ td := ptrace .NewTraces ()
234
+ td .ResourceSpans ().AppendEmpty ()
235
+ rs0 := td .ResourceSpans ().At (0 )
236
+ rs0 .Resource ().Attributes ().PutString (defaultTenantIDAttributeKey , "example-tenant-1" )
237
+ rs0 .Resource ().Attributes ().PutString (conventions .AttributeServiceName , "example-service-1" )
238
+ rs0 .ScopeSpans ().AppendEmpty ()
239
+ rs0scopespan0 := td .ResourceSpans ().At (0 ).ScopeSpans ().At (0 )
240
+ rs0scopespan0 .Spans ().AppendEmpty ()
241
+ rs0scopespan0 .Spans ().At (0 ).SetName ("span-1" )
242
+ rs0scopespan0 .Spans ().At (0 ).Attributes ().PutString ("k1" , "v1" )
243
+ rs0scopespan0 .Spans ().At (0 ).Attributes ().PutString ("k2" , "v2" )
244
+ rs0scopespan0 .Spans ().AppendEmpty ()
245
+ rs0scopespan0 .Spans ().At (1 ).SetName ("span-2" )
246
+ rs0scopespan0 .Spans ().At (1 ).Attributes ().PutString ("k1" , "v10" )
247
+ rs0scopespan0 .Spans ().At (1 ).Attributes ().PutString ("k2" , "v20" )
248
+ rs0scopespan0 .Spans ().AppendEmpty ()
249
+ rs0scopespan0 .Spans ().At (2 ).SetName ("span-1" )
250
+ rs0scopespan0 .Spans ().At (2 ).Attributes ().PutString ("k1" , "v1" )
251
+ rs0scopespan0 .Spans ().At (2 ).Attributes ().PutString ("k2" , "v23" )
252
+
253
+ logger := zap .NewNop ()
254
+ c := & Config {
255
+ TenantConfigs : []TenantConfig {
256
+ {
257
+ TenantId : "example-tenant-1" ,
258
+ ServiceConfigs : []ServiceConfig {
259
+ {
260
+ ServiceName : "example-service-1" ,
261
+ SpanConfigs : []SpanConfig {
262
+ {
263
+ SpanName : "span-1" ,
264
+ SpanAttributes : []SpanAttribute {
265
+ {Key : "k1" , Value : "v1" },
266
+ {Key : "k2" , Value : "v23" },
267
+ },
268
+ },
269
+ },
270
+ },
271
+ },
272
+ },
273
+ },
274
+ }
275
+
276
+ p := newProcessor (logger , c )
277
+
278
+ // We cannot verify metrics :( We will verify no errors and no change in traces
279
+ processedTd , err := p .ProcessTraces (context .Background (), td )
280
+ assert .NoError (t , err )
281
+ assert .Equal (t , td , processedTd )
282
+
283
+ // Non matching tenant should also not throw an error
284
+ c .TenantConfigs [0 ].TenantId = "example-tenant-2"
285
+ p = newProcessor (logger , c )
286
+
287
+ processedTd , err = p .ProcessTraces (context .Background (), td )
288
+ assert .NoError (t , err )
289
+ assert .Equal (t , td , processedTd )
290
+
291
+ // Empty config
292
+ c = & Config {}
293
+ p = newProcessor (logger , c )
294
+
295
+ processedTd , err = p .ProcessTraces (context .Background (), td )
296
+ assert .NoError (t , err )
297
+ assert .Equal (t , td , processedTd )
85
298
}
0 commit comments