-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
span.go
323 lines (279 loc) · 9.05 KB
/
span.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
// Copyright 2020, 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 span
import (
"errors"
"fmt"
"regexp"
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
"github.com/open-telemetry/opentelemetry-collector/internal/processor"
)
var (
// TODO Add processor type invoking the NewMatcher in error text.
errAtLeastOneMatchFieldNeeded = errors.New(
`error creating processor. At least one ` +
`of "services", "span_names" or "attributes" field must be specified"`)
errInvalidMatchType = fmt.Errorf(
`match_type must be either %q or %q`, MatchTypeStrict, MatchTypeRegexp)
)
// TODO: Modify Matcher to invoke both the include and exclude properties so
// calling processors will always have the same logic.
// Matcher is an interface that allows matching a span against a configuration
// of a match.
type Matcher interface {
MatchSpan(span *tracepb.Span, serviceName string) bool
}
type attributesMatcher []attributeMatcher
// strictPropertiesMatcher allows matching a span against a "strict" match type
// configuration.
type strictPropertiesMatcher 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 attributesMatcher
}
// regexpPropertiesMatcher allows matching a span against a "regexp" match type
// configuration.
type regexpPropertiesMatcher 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 attributesMatcher
}
// attributeMatcher is a attribute key/value pair to match to.
type attributeMatcher struct {
Key string
AttributeValue *tracepb.AttributeValue
}
func NewMatcher(config *MatchProperties) (Matcher, error) {
if config == nil {
return nil, nil
}
if len(config.Services) == 0 && len(config.SpanNames) == 0 && len(config.Attributes) == 0 {
return nil, errAtLeastOneMatchFieldNeeded
}
var properties Matcher
var err error
switch config.MatchType {
case MatchTypeStrict:
properties, err = newStrictPropertiesMatcher(config)
case MatchTypeRegexp:
properties, err = newRegexpPropertiesMatcher(config)
default:
return nil, errInvalidMatchType
}
if err != nil {
return nil, err
}
return properties, nil
}
func newStrictPropertiesMatcher(config *MatchProperties) (*strictPropertiesMatcher, error) {
properties := &strictPropertiesMatcher{
Services: config.Services,
SpanNames: config.SpanNames,
}
var err error
properties.Attributes, err = newAttributesMatcher(config)
if err != nil {
return nil, err
}
return properties, nil
}
func newRegexpPropertiesMatcher(config *MatchProperties) (*regexpPropertiesMatcher, error) {
properties := ®expPropertiesMatcher{}
// Precompile Services regexp patterns.
for _, pattern := range config.Services {
g, err := regexp.Compile(pattern)
if err != nil {
return nil, fmt.Errorf(
"error creating processor. %s is not a valid service name regexp pattern",
pattern,
)
}
properties.Services = append(properties.Services, g)
}
// Precompile SpanNames regexp patterns.
for _, pattern := range config.SpanNames {
g, err := regexp.Compile(pattern)
if err != nil {
return nil, fmt.Errorf(
"error creating processor. %s is not a valid span name regexp pattern",
pattern,
)
}
properties.SpanNames = append(properties.SpanNames, g)
}
if len(config.Attributes) > 0 {
return nil, fmt.Errorf(
"%s=%s is not supported for %q",
MatchTypeFieldName, MatchTypeRegexp, AttributesFieldName,
)
}
return properties, nil
}
func newAttributesMatcher(config *MatchProperties) (attributesMatcher, error) {
// Convert attribute values from config representation to in-memory representation.
var rawAttributes []attributeMatcher
for _, attribute := range config.Attributes {
if attribute.Key == "" {
return nil, errors.New("error creating processor. Can't have empty key in the list of attributes")
}
entry := attributeMatcher{
Key: attribute.Key,
}
if attribute.Value != nil {
val, err := processor.AttributeValue(attribute.Value)
if err != nil {
return nil, err
}
entry.AttributeValue = val
}
rawAttributes = append(rawAttributes, entry)
}
return rawAttributes, nil
}
// 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 *strictPropertiesMatcher) 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 *regexpPropertiesMatcher) 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 attributesMatcher) 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
}