/
factory.go
277 lines (241 loc) · 8.44 KB
/
factory.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
// 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 (
"errors"
"fmt"
"regexp"
"strings"
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
"github.com/spf13/cast"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector/config/configerror"
"github.com/open-telemetry/opentelemetry-collector/config/configmodels"
"github.com/open-telemetry/opentelemetry-collector/consumer"
"github.com/open-telemetry/opentelemetry-collector/processor"
)
const (
// typeStr is the value of "type" key in configuration.
typeStr = "attributes"
)
var (
errAtLeastOneMatchFieldNeeded = errors.New(
`error creating "attributes" 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)
)
// Factory is the factory for Attributes processor.
type Factory struct {
}
// Type gets the type of the config created by this factory.
func (f *Factory) Type() string {
return typeStr
}
// CreateDefaultConfig creates the default configuration for the processor.
// Note: This isn't a valid configuration because the processor would do no work.
func (f *Factory) CreateDefaultConfig() configmodels.Processor {
return &Config{
ProcessorSettings: configmodels.ProcessorSettings{
TypeVal: typeStr,
NameVal: typeStr,
},
}
}
// CreateTraceProcessor creates a trace processor based on this config.
func (f *Factory) CreateTraceProcessor(
logger *zap.Logger,
nextConsumer consumer.TraceConsumer,
cfg configmodels.Processor,
) (processor.TraceProcessor, error) {
oCfg := cfg.(*Config)
actions, err := buildAttributesConfiguration(*oCfg)
if err != nil {
return nil, err
}
include, err := buildMatchProperties(oCfg.Include)
if err != nil {
return nil, err
}
exclude, err := buildMatchProperties(oCfg.Exclude)
if err != nil {
return nil, err
}
config := attributesConfig{
actions: actions,
include: include,
exclude: exclude,
}
return newTraceProcessor(nextConsumer, config)
}
// CreateMetricsProcessor creates a metrics processor based on this config.
func (f *Factory) CreateMetricsProcessor(
logger *zap.Logger,
nextConsumer consumer.MetricsConsumer,
cfg configmodels.Processor,
) (processor.MetricsProcessor, error) {
return nil, configerror.ErrDataTypeIsNotSupported
}
// attributeValue is used to convert the raw `value` from ActionKeyValue to the supported trace attribute values.
func attributeValue(value interface{}) (*tracepb.AttributeValue, error) {
attrib := &tracepb.AttributeValue{}
switch val := value.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
attrib.Value = &tracepb.AttributeValue_IntValue{IntValue: cast.ToInt64(val)}
case float32, float64:
attrib.Value = &tracepb.AttributeValue_DoubleValue{DoubleValue: cast.ToFloat64(val)}
case string:
attrib.Value = &tracepb.AttributeValue_StringValue{
StringValue: &tracepb.TruncatableString{Value: val},
}
case bool:
attrib.Value = &tracepb.AttributeValue_BoolValue{BoolValue: val}
default:
return nil, fmt.Errorf("error unsupported value type \"%T\"", value)
}
return attrib, nil
}
// buildAttributesConfiguration validates the input configuration has all of the required fields for the processor
// and returns a list of valid actions to configure the processor.
// An error is returned if there are any invalid inputs.
func buildAttributesConfiguration(config Config) ([]attributeAction, error) {
if len(config.Actions) == 0 {
return nil, fmt.Errorf("error creating \"attributes\" processor due to missing required field \"actions\" of processor %q", config.Name())
}
var attributeActions []attributeAction
for i, a := range config.Actions {
// `key` is a required field
if a.Key == "" {
return nil, fmt.Errorf("error creating \"attributes\" processor due to missing required field \"key\" at the %d-th actions of processor %q", i, config.Name())
}
// Convert `action` to lowercase for comparison.
a.Action = Action(strings.ToLower(string(a.Action)))
action := attributeAction{
Key: a.Key,
Action: a.Action,
}
switch a.Action {
case INSERT, UPDATE, UPSERT:
if a.Value == nil && a.FromAttribute == "" {
return nil, fmt.Errorf("error creating \"attributes\" processor. Either field \"value\" or \"from_attribute\" setting must be specified for %d-th action of processor %q", i, config.Name())
}
if a.Value != nil && a.FromAttribute != "" {
return nil, fmt.Errorf("error creating \"attributes\" processor due to both fields \"value\" and \"from_attribute\" being set at the %d-th actions of processor %q", i, config.Name())
}
// Convert the raw value from the configuration to the internal trace representation of the value.
if a.Value != nil {
val, err := attributeValue(a.Value)
if err != nil {
return nil, err
}
action.AttributeValue = val
} else {
action.FromAttribute = a.FromAttribute
}
case DELETE:
// Do nothing since `key` is the only required field for `delete` action.
default:
return nil, fmt.Errorf("error creating \"attributes\" processor due to unsupported action %q at the %d-th actions of processor %q", a.Action, i, config.Name())
}
attributeActions = append(attributeActions, action)
}
return attributeActions, nil
}
func buildMatchProperties(config *MatchProperties) (matchingProperties, 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 matchingProperties
var err error
switch config.MatchType {
case MatchTypeStrict:
properties, err = buildStrictMatchingProperties(config)
case MatchTypeRegexp:
properties, err = buildRegexpMatchingProperties(config)
default:
return nil, errInvalidMatchType
}
if err != nil {
return nil, err
}
return properties, nil
}
func buildStrictMatchingProperties(config *MatchProperties) (*strictMatchingProperties, error) {
properties := &strictMatchingProperties{
Services: config.Services,
SpanNames: config.SpanNames,
}
var err error
properties.Attributes, err = buildAttributesMatches(config)
if err != nil {
return nil, err
}
return properties, nil
}
func buildRegexpMatchingProperties(config *MatchProperties) (*regexpMatchingProperties, error) {
properties := ®expMatchingProperties{}
// Precompile Services regexp patterns.
for _, pattern := range config.Services {
g, err := regexp.Compile(pattern)
if err != nil {
return nil, fmt.Errorf(
"error creating \"attributes\" 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 \"attributes\" 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 buildAttributesMatches(config *MatchProperties) (matchAttributes, error) {
// Convert attribute values from config representation to in-memory representation.
var rawAttributes []matchAttribute
for _, attribute := range config.Attributes {
if attribute.Key == "" {
return nil, errors.New("error creating \"attributes\" processor. Can't have empty key in the list of attributes")
}
entry := matchAttribute{
Key: attribute.Key,
}
if attribute.Value != nil {
val, err := attributeValue(attribute.Value)
if err != nil {
return nil, err
}
entry.AttributeValue = val
}
rawAttributes = append(rawAttributes, entry)
}
return rawAttributes, nil
}