forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
207 lines (177 loc) · 6.92 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
// 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:https://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 metricstransformprocessor
import (
"context"
"fmt"
"regexp"
"strings"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configmodels"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/processor/processorhelper"
)
const (
// The value of "type" key in configuration.
typeStr = "metricstransform"
)
var processorCapabilities = component.ProcessorCapabilities{MutatesConsumedData: true}
// NewFactory returns a new factory for the Metrics Transform processor.
func NewFactory() component.ProcessorFactory {
return processorhelper.NewFactory(
typeStr,
createDefaultConfig,
processorhelper.WithMetrics(createMetricsProcessor))
}
func createDefaultConfig() configmodels.Processor {
return &Config{
ProcessorSettings: configmodels.ProcessorSettings{
TypeVal: typeStr,
NameVal: typeStr,
},
}
}
func createMetricsProcessor(
ctx context.Context,
params component.ProcessorCreateParams,
cfg configmodels.Processor,
nextConsumer consumer.MetricsConsumer,
) (component.MetricsProcessor, error) {
oCfg := cfg.(*Config)
if err := validateConfiguration(oCfg); err != nil {
return nil, err
}
metricsProcessor := newMetricsTransformProcessor(params.Logger, buildHelperConfig(oCfg, params.ApplicationStartInfo.Version))
return processorhelper.NewMetricsProcessor(
cfg,
nextConsumer,
metricsProcessor,
processorhelper.WithCapabilities(processorCapabilities))
}
// validateConfiguration validates the input configuration has all of the required fields for the processor
// An error is returned if there are any invalid inputs.
func validateConfiguration(config *Config) error {
for _, transform := range config.Transforms {
if transform.MetricIncludeFilter.Include == "" && transform.MetricName == "" {
return fmt.Errorf("missing required field %q", IncludeFieldName)
}
if transform.MetricIncludeFilter.Include != "" && transform.MetricName != "" {
return fmt.Errorf("cannot supply both %q and %q, use %q with %q match type", IncludeFieldName, MetricNameFieldName, IncludeFieldName, StrictMatchType)
}
if transform.MetricIncludeFilter.MatchType != "" && !transform.MetricIncludeFilter.MatchType.isValid() {
return fmt.Errorf("%q must be in %q", MatchTypeFieldName, MatchTypes)
}
if transform.MetricIncludeFilter.MatchType == RegexpMatchType {
_, err := regexp.Compile(transform.MetricIncludeFilter.Include)
if err != nil {
return fmt.Errorf("%q, %w", IncludeFieldName, err)
}
}
if !transform.Action.isValid() {
return fmt.Errorf("%q must be in %q", ActionFieldName, Actions)
}
if transform.Action == Insert && transform.NewName == "" {
return fmt.Errorf("missing required field %q while %q is %v", NewNameFieldName, ActionFieldName, Insert)
}
if transform.AggregationType != "" && !transform.AggregationType.isValid() {
return fmt.Errorf("%q must be in %q", AggregationTypeFieldName, AggregationTypes)
}
if transform.SubmatchCase != "" && !transform.SubmatchCase.isValid() {
return fmt.Errorf("%q must be in %q", SubmatchCaseFieldName, SubmatchCases)
}
for i, op := range transform.Operations {
if !op.Action.isValid() {
return fmt.Errorf("operation %v: %q must be in %q", i+1, ActionFieldName, OperationActions)
}
if op.Action == UpdateLabel && op.Label == "" {
return fmt.Errorf("operation %v: missing required field %q while %q is %v", i+1, LabelFieldName, ActionFieldName, UpdateLabel)
}
if op.Action == AddLabel && op.NewLabel == "" {
return fmt.Errorf("operation %v: missing required field %q while %q is %v", i+1, NewLabelFieldName, ActionFieldName, AddLabel)
}
if op.Action == AddLabel && op.NewValue == "" {
return fmt.Errorf("operation %v: missing required field %q while %q is %v", i+1, NewValueFieldName, ActionFieldName, AddLabel)
}
if op.AggregationType != "" && !op.AggregationType.isValid() {
return fmt.Errorf("operation %v: %q must be in %q", i+1, AggregationTypeFieldName, AggregationTypes)
}
}
}
return nil
}
// buildHelperConfig constructs the maps that will be useful for the operations
func buildHelperConfig(config *Config, version string) []internalTransform {
helperDataTransforms := make([]internalTransform, len(config.Transforms))
for i, t := range config.Transforms {
// for backwards compatibility, convert metric name to an include filter
if t.MetricName != "" {
t.MetricIncludeFilter = FilterConfig{Include: t.MetricName}
t.MetricName = ""
}
if t.MetricIncludeFilter.MatchType == "" {
t.MetricIncludeFilter.MatchType = StrictMatchType
}
helperT := internalTransform{
MetricIncludeFilter: createFilter(t.MetricIncludeFilter),
Action: t.Action,
NewName: t.NewName,
AggregationType: t.AggregationType,
Operations: make([]internalOperation, len(t.Operations)),
}
for j, op := range t.Operations {
op.NewValue = strings.ReplaceAll(op.NewValue, "{{version}}", version)
mtpOp := internalOperation{
configOperation: op,
}
if len(op.ValueActions) > 0 {
mtpOp.valueActionsMapping = createLabelValueMapping(op.ValueActions, version)
}
if op.Action == AggregateLabels {
mtpOp.labelSetMap = sliceToSet(op.LabelSet)
} else if op.Action == AggregateLabelValues {
mtpOp.aggregatedValuesSet = sliceToSet(op.AggregatedValues)
}
helperT.Operations[j] = mtpOp
}
helperDataTransforms[i] = helperT
}
return helperDataTransforms
}
func createFilter(filterConfig FilterConfig) internalFilter {
switch filterConfig.MatchType {
case StrictMatchType:
return internalFilterStrict{include: filterConfig.Include}
case RegexpMatchType:
return internalFilterRegexp{include: regexp.MustCompile(filterConfig.Include)}
}
return nil
}
// createLabelValueMapping creates the labelValue rename mappings based on the valueActions
func createLabelValueMapping(valueActions []ValueAction, version string) map[string]string {
mapping := make(map[string]string)
for i := 0; i < len(valueActions); i++ {
valueActions[i].NewValue = strings.ReplaceAll(valueActions[i].NewValue, "{{version}}", version)
mapping[valueActions[i].Value] = valueActions[i].NewValue
}
return mapping
}
// sliceToSet converts slice of strings to set of strings
// Returns the set of strings
func sliceToSet(slice []string) map[string]bool {
set := make(map[string]bool, len(slice))
for _, s := range slice {
set[s] = true
}
return set
}