-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
factory.go
219 lines (187 loc) · 7.38 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package metricstransformprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/metricstransformprocessor"
import (
"context"
"fmt"
"regexp"
"strings"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/processorhelper"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/aggregateutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/metricstransformprocessor/internal/metadata"
)
var consumerCapabilities = consumer.Capabilities{MutatesData: true}
// NewFactory returns a new factory for the Metrics transform processor.
func NewFactory() processor.Factory {
return processor.NewFactory(
metadata.Type,
createDefaultConfig,
processor.WithMetrics(createMetricsProcessor, metadata.MetricsStability))
}
func createDefaultConfig() component.Config {
return &Config{}
}
func createMetricsProcessor(
ctx context.Context,
set processor.Settings,
cfg component.Config,
nextConsumer consumer.Metrics,
) (processor.Metrics, error) {
oCfg := cfg.(*Config)
if err := validateConfiguration(oCfg); err != nil {
return nil, err
}
hCfg, err := buildHelperConfig(oCfg, set.BuildInfo.Version)
if err != nil {
return nil, err
}
metricsProcessor := newMetricsTransformProcessor(set.Logger, hCfg)
return processorhelper.NewMetricsProcessor(
ctx,
set,
cfg,
nextConsumer,
metricsProcessor.processMetrics,
processorhelper.WithCapabilities(consumerCapabilities))
}
// 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 == "" {
return fmt.Errorf("missing required field %q", includeFieldName)
}
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.Action == Group && transform.GroupResourceLabels == nil {
return fmt.Errorf("missing required field %q while %q is %v", groupResourceLabelsFieldName, actionFieldName, Group)
}
if transform.AggregationType != "" && !transform.AggregationType.IsValid() {
return fmt.Errorf("%q must be in %q", aggregationTypeFieldName, aggregateutil.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.Action == scaleValue && op.Scale == 0 {
return fmt.Errorf("operation %v: missing required field %q while %q is %v", i+1, scaleFieldName, actionFieldName, scaleValue)
}
if op.AggregationType != "" && !op.AggregationType.IsValid() {
return fmt.Errorf("operation %v: %q must be in %q", i+1, aggregationTypeFieldName, aggregateutil.AggregationTypes)
}
}
}
return nil
}
// buildHelperConfig constructs the maps that will be useful for the operations
func buildHelperConfig(config *Config, version string) ([]internalTransform, error) {
helperDataTransforms := make([]internalTransform, len(config.Transforms))
for i, t := range config.Transforms {
if t.MetricIncludeFilter.MatchType == "" {
t.MetricIncludeFilter.MatchType = strictMatchType
}
filter, err := createFilter(t.MetricIncludeFilter)
if err != nil {
return nil, err
}
helperT := internalTransform{
MetricIncludeFilter: filter,
Action: t.Action,
NewName: t.NewName,
GroupResourceLabels: t.GroupResourceLabels,
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, nil
}
func createFilter(filterConfig FilterConfig) (internalFilter, error) {
switch filterConfig.MatchType {
case strictMatchType:
matchers, err := getMatcherMap(filterConfig.MatchLabels, func(str string) (StringMatcher, error) { return strictMatcher(str), nil })
if err != nil {
return nil, err
}
return internalFilterStrict{include: filterConfig.Include, attrMatchers: matchers}, nil
case regexpMatchType:
matchers, err := getMatcherMap(filterConfig.MatchLabels, func(str string) (StringMatcher, error) { return regexp.Compile(str) })
if err != nil {
return nil, err
}
return internalFilterRegexp{include: regexp.MustCompile(filterConfig.Include), attrMatchers: matchers}, nil
}
return nil, fmt.Errorf("invalid match type: %v", filterConfig.MatchType)
}
// 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
}
func getMatcherMap(strMap map[string]string, ctor func(string) (StringMatcher, error)) (map[string]StringMatcher, error) {
out := make(map[string]StringMatcher)
for k, v := range strMap {
matcher, err := ctor(v)
if err != nil {
return nil, err
}
out[k] = matcher
}
return out, nil
}