/
attributes.go
367 lines (320 loc) · 11.5 KB
/
attributes.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright 2019, 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 attributesprocessor
import (
"context"
"regexp"
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
"github.com/open-telemetry/opentelemetry-collector/component"
"github.com/open-telemetry/opentelemetry-collector/consumer"
"github.com/open-telemetry/opentelemetry-collector/consumer/consumerdata"
"github.com/open-telemetry/opentelemetry-collector/oterr"
"github.com/open-telemetry/opentelemetry-collector/processor"
)
type attributesProcessor struct {
nextConsumer consumer.TraceConsumer
config attributesConfig
}
// This structure is very similar to the config for attributes processor
// with the value in the converted attribute format instead of the
// raw format from the configuration.
type attributesConfig struct {
actions []attributeAction
include matchingProperties
exclude matchingProperties
}
type attributeAction struct {
Key string
FromAttribute string
// TODO https://github.com/open-telemetry/opentelemetry-collector/issues/296
// Do benchmark testing between having action be of type string vs integer.
// The reason is attributes processor will most likely be commonly used
// and could impact performance.
Action Action
AttributeValue *tracepb.AttributeValue
}
// matchingProperties is an interface that allows matching a span against a configuration
// of a match.
type matchingProperties interface {
matchSpan(span *tracepb.Span, serviceName string) bool
}
type matchAttributes []matchAttribute
// strictMatchingProperties allows matching a span against a "strict" match type
// configuration.
type strictMatchingProperties struct {
// Service names to compare to.
Services []string
// Span names to compare to.
SpanNames []string
// The attribute values are stored in the internal format.
Attributes matchAttributes
}
// strictMatchingProperties allows matching a span against a "regexp" match type
// configuration.
type regexpMatchingProperties struct {
// Precompiled service name regexp-es.
Services []*regexp.Regexp
// Precompiled span name regexp-es.
SpanNames []*regexp.Regexp
// The attribute values are stored in the internal format.
Attributes matchAttributes
}
// matchAttribute is a attribute key/value pair to match to.
type matchAttribute struct {
Key string
AttributeValue *tracepb.AttributeValue
}
// newTraceProcessor returns a processor that modifies attributes of a span.
// To construct the attributes processors, the use of the factory methods are required
// in order to validate the inputs.
func newTraceProcessor(nextConsumer consumer.TraceConsumer, config attributesConfig) (processor.TraceProcessor, error) {
if nextConsumer == nil {
return nil, oterr.ErrNilNextConsumer
}
ap := &attributesProcessor{
nextConsumer: nextConsumer,
config: config,
}
return ap, nil
}
func (a *attributesProcessor) ConsumeTraceData(ctx context.Context, td consumerdata.TraceData) error {
serviceName := processor.ServiceNameForNode(td.Node)
for _, span := range td.Spans {
if span == nil {
// Do not create empty spans just to add attributes
continue
}
if a.skipSpan(span, serviceName) {
continue
}
if span.Attributes == nil {
span.Attributes = &tracepb.Span_Attributes{}
}
// Create a new map if one does not exist and size it to the number of actions.
// This is the largest size of the new map could be if every action is an insert.
if span.Attributes.AttributeMap == nil {
span.Attributes.AttributeMap = make(map[string]*tracepb.AttributeValue, len(a.config.actions))
}
for _, action := range a.config.actions {
// TODO https://github.com/open-telemetry/opentelemetry-collector/issues/296
// Do benchmark testing between having action be of type string vs integer.
// The reason is attributes processor will most likely be commonly used
// and could impact performance.
switch action.Action {
case DELETE:
delete(span.Attributes.AttributeMap, action.Key)
case INSERT:
insertAttribute(action, span.Attributes.AttributeMap)
case UPDATE:
updateAttribute(action, span.Attributes.AttributeMap)
case UPSERT:
// There is no need to check if the target key exists in the attribute map
// because the value is to be set regardless.
setAttribute(action, span.Attributes.AttributeMap)
}
}
}
return a.nextConsumer.ConsumeTraceData(ctx, td)
}
func (a *attributesProcessor) GetCapabilities() processor.Capabilities {
return processor.Capabilities{MutatesConsumedData: true}
}
// Start is invoked during service startup.
func (a *attributesProcessor) Start(host component.Host) error {
return nil
}
// Shutdown is invoked during service shutdown.
func (a *attributesProcessor) Shutdown() error {
return nil
}
func insertAttribute(action attributeAction, attributesMap map[string]*tracepb.AttributeValue) {
// Insert is only performed when the target key does not already exist
// in the attribute map.
if _, exists := attributesMap[action.Key]; exists {
return
}
setAttribute(action, attributesMap)
}
func updateAttribute(action attributeAction, attributesMap map[string]*tracepb.AttributeValue) {
// Update is only performed when the target key already exists in
// the attribute map.
if _, exists := attributesMap[action.Key]; !exists {
return
}
setAttribute(action, attributesMap)
}
func setAttribute(action attributeAction, attributesMap map[string]*tracepb.AttributeValue) {
// Set the key with a value from the configuration.
if action.AttributeValue != nil {
attributesMap[action.Key] = action.AttributeValue
} else if value, fromAttributeExists := attributesMap[action.FromAttribute]; fromAttributeExists {
// Set the key with a value from another attribute, if it exists.
attributesMap[action.Key] = value
}
}
// skipSpan determines if a span should be processed.
// True is returned when a span should be skipped.
// False is returned when a span should not be skipped.
// The logic determining if a span should be processed is set
// in the attribute configuration with the include and exclude settings.
// Include properties are checked before exclude settings are checked.
func (a *attributesProcessor) skipSpan(span *tracepb.Span, serviceName string) bool {
// By default all spans are processed when no include and exclude properties are set.
if a.config.include == nil && a.config.exclude == nil {
return false
}
if a.config.include != nil {
// A false returned in this case means the span should not be processed.
if include := a.config.include.matchSpan(span, serviceName); !include {
return true
}
}
if a.config.exclude != nil {
// A true returned in this case means the span should not be processed.
if exclude := a.config.exclude.matchSpan(span, serviceName); exclude {
return true
}
}
return false
}
// matchSpan matches a span and service to a set of properties.
// There are 3 sets of properties to match against.
// The service name is checked first, if specified. Then span names are matched, if specified.
// The attributes are checked last, if specified.
// At least one of services, span names or attributes must be specified. It is supported
// to have more than one of these specified, and all specified must evaluate
// to true for a match to occur.
func (mp *strictMatchingProperties) matchSpan(span *tracepb.Span, serviceName string) bool {
if len(mp.Services) > 0 {
// Verify service name matches at least one of the items.
matched := false
for _, item := range mp.Services {
if item == serviceName {
matched = true
break
}
}
if !matched {
return false
}
}
if len(mp.SpanNames) > 0 {
// SpanNames condition is specified. Check if span name matches the condition.
var spanName string
if span.Name != nil {
spanName = span.Name.Value
}
// Verify span name matches at least one of the items.
matched := false
for _, item := range mp.SpanNames {
if item == spanName {
matched = true
break
}
}
if !matched {
return false
}
}
// Service name and span name matched. Now match attributes.
return mp.Attributes.match(span)
}
// matchSpan matches a span and service to a set of properties.
// There are 3 sets of properties to match against.
// The service name is checked first, if specified. Then span names are matched, if specified.
// The attributes are checked last, if specified.
// At least one of services, span names or attributes must be specified. It is supported
// to have more than one of these specified, and all specified must evaluate
// to true for a match to occur.
func (mp *regexpMatchingProperties) matchSpan(span *tracepb.Span, serviceName string) bool {
if len(mp.Services) > 0 {
// Verify service name matches at least one of the regexp patterns.
matched := false
for _, re := range mp.Services {
if re.MatchString(serviceName) {
matched = true
break
}
}
if !matched {
return false
}
}
if len(mp.SpanNames) > 0 {
// SpanNames condition is specified. Check if span name matches the condition.
var spanName string
if span.Name != nil {
spanName = span.Name.Value
}
// Verify span name matches at least one of the regexp patterns.
matched := false
for _, re := range mp.SpanNames {
if re.MatchString(spanName) {
matched = true
break
}
}
if !matched {
return false
}
}
// Service name and span name matched. Now match attributes.
return mp.Attributes.match(span)
}
// match attributes specification against a span.
func (ma matchAttributes) match(span *tracepb.Span) bool {
// If there are no attributes to match against, the span matches.
if len(ma) == 0 {
return true
}
// At this point, it is expected of the span to have attributes because of
// len(ma) != 0. This means for spans with no attributes, it does not match.
if span.Attributes == nil || len(span.Attributes.AttributeMap) == 0 {
return false
}
// Check that all expected properties are set.
for _, property := range ma {
val, exist := span.Attributes.AttributeMap[property.Key]
if !exist {
return false
}
// This is for the case of checking that the key existed.
if property.AttributeValue == nil {
continue
}
var isMatch bool
switch attribValue := val.Value.(type) {
case *tracepb.AttributeValue_StringValue:
if sv, ok := property.AttributeValue.GetValue().(*tracepb.AttributeValue_StringValue); ok {
isMatch = attribValue.StringValue.GetValue() == sv.StringValue.GetValue()
}
case *tracepb.AttributeValue_IntValue:
if iv, ok := property.AttributeValue.GetValue().(*tracepb.AttributeValue_IntValue); ok {
isMatch = attribValue.IntValue == iv.IntValue
}
case *tracepb.AttributeValue_BoolValue:
if bv, ok := property.AttributeValue.GetValue().(*tracepb.AttributeValue_BoolValue); ok {
isMatch = attribValue.BoolValue == bv.BoolValue
}
case *tracepb.AttributeValue_DoubleValue:
if dv, ok := property.AttributeValue.GetValue().(*tracepb.AttributeValue_DoubleValue); ok {
isMatch = attribValue.DoubleValue == dv.DoubleValue
}
}
if !isMatch {
return false
}
}
return true
}