forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
318 lines (268 loc) · 12.3 KB
/
config.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
// Copyright The 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 filterprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/filterprocessor"
import (
"errors"
"fmt"
"strings"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/multierr"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filtermetric"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset/regexp"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/filterprocessor/internal/common"
)
// Config defines configuration for Resource processor.
type Config struct {
Metrics MetricFilters `mapstructure:"metrics"`
Logs LogFilters `mapstructure:"logs"`
Spans filterconfig.MatchConfig `mapstructure:"spans"`
Traces TraceFilters `mapstructure:"traces"`
}
// MetricFilters filters by Metric properties.
type MetricFilters struct {
// Include match properties describe metrics that should be included in the Collector Service pipeline,
// all other metrics should be dropped from further processing.
// If both Include and Exclude are specified, Include filtering occurs first.
Include *filtermetric.MatchProperties `mapstructure:"include"`
// Exclude match properties describe metrics that should be excluded from the Collector Service pipeline,
// all other metrics should be included.
// If both Include and Exclude are specified, Include filtering occurs first.
Exclude *filtermetric.MatchProperties `mapstructure:"exclude"`
// RegexpConfig specifies options for the Regexp match type
RegexpConfig *regexp.Config `mapstructure:"regexp"`
// MetricConditions is a list of OTTL conditions for an ottlmetric context.
// If any condition resolves to true, the metric will be dropped.
// Supports `and`, `or`, and `()`
MetricConditions []string `mapstructure:"metric"`
// DataPointConditions is a list of OTTL conditions for an ottldatapoint context.
// If any condition resolves to true, the datapoint will be dropped.
// Supports `and`, `or`, and `()`
DataPointConditions []string `mapstructure:"datapoint"`
}
// TraceFilters filters by OTTL conditions
type TraceFilters struct {
// SpanConditions is a list of OTTL conditions for an ottlspan context.
// If any condition resolves to true, the span will be dropped.
// Supports `and`, `or`, and `()`
SpanConditions []string `mapstructure:"span"`
// SpanEventConditions is a list of OTTL conditions for an ottlspanevent context.
// If any condition resolves to true, the span event will be dropped.
// Supports `and`, `or`, and `()`
SpanEventConditions []string `mapstructure:"spanevent"`
}
// LogFilters filters by Log properties.
type LogFilters struct {
// Include match properties describe logs that should be included in the Collector Service pipeline,
// all other logs should be dropped from further processing.
// If both Include and Exclude are specified, Include filtering occurs first.
Include *LogMatchProperties `mapstructure:"include"`
// Exclude match properties describe logs that should be excluded from the Collector Service pipeline,
// all other logs should be included.
// If both Include and Exclude are specified, Include filtering occurs first.
Exclude *LogMatchProperties `mapstructure:"exclude"`
// LogConditions is a list of OTTL conditions for an ottllog context.
// If any condition resolves to true, the log event will be dropped.
// Supports `and`, `or`, and `()`
LogConditions []string `mapstructure:"log_record"`
}
// LogMatchType specifies the strategy for matching against `plog.Log`s.
type LogMatchType string
// These are the MatchTypes that users can specify for filtering
// `plog.Log`s.
const (
Strict = LogMatchType(filterset.Strict)
Regexp = LogMatchType(filterset.Regexp)
)
var severityToNumber = map[string]plog.SeverityNumber{
"1": plog.SeverityNumberTrace,
"2": plog.SeverityNumberTrace2,
"3": plog.SeverityNumberTrace3,
"4": plog.SeverityNumberTrace4,
"5": plog.SeverityNumberDebug,
"6": plog.SeverityNumberDebug2,
"7": plog.SeverityNumberDebug3,
"8": plog.SeverityNumberDebug4,
"9": plog.SeverityNumberInfo,
"10": plog.SeverityNumberInfo2,
"11": plog.SeverityNumberInfo3,
"12": plog.SeverityNumberInfo4,
"13": plog.SeverityNumberWarn,
"14": plog.SeverityNumberWarn2,
"15": plog.SeverityNumberWarn3,
"16": plog.SeverityNumberWarn4,
"17": plog.SeverityNumberError,
"18": plog.SeverityNumberError2,
"19": plog.SeverityNumberError3,
"20": plog.SeverityNumberError4,
"21": plog.SeverityNumberFatal,
"22": plog.SeverityNumberFatal2,
"23": plog.SeverityNumberFatal3,
"24": plog.SeverityNumberFatal4,
"TRACE": plog.SeverityNumberTrace,
"TRACE2": plog.SeverityNumberTrace2,
"TRACE3": plog.SeverityNumberTrace3,
"TRACE4": plog.SeverityNumberTrace4,
"DEBUG": plog.SeverityNumberDebug,
"DEBUG2": plog.SeverityNumberDebug2,
"DEBUG3": plog.SeverityNumberDebug3,
"DEBUG4": plog.SeverityNumberDebug4,
"INFO": plog.SeverityNumberInfo,
"INFO2": plog.SeverityNumberInfo2,
"INFO3": plog.SeverityNumberInfo3,
"INFO4": plog.SeverityNumberInfo4,
"WARN": plog.SeverityNumberWarn,
"WARN2": plog.SeverityNumberWarn2,
"WARN3": plog.SeverityNumberWarn3,
"WARN4": plog.SeverityNumberWarn4,
"ERROR": plog.SeverityNumberError,
"ERROR2": plog.SeverityNumberError2,
"ERROR3": plog.SeverityNumberError3,
"ERROR4": plog.SeverityNumberError4,
"FATAL": plog.SeverityNumberFatal,
"FATAL2": plog.SeverityNumberFatal2,
"FATAL3": plog.SeverityNumberFatal3,
"FATAL4": plog.SeverityNumberFatal4,
}
var errInvalidSeverity = errors.New("not a valid severity")
// logSeverity is a type that represents a SeverityNumber as a string
type logSeverity string
// validate checks that the logSeverity is valid
func (l logSeverity) validate() error {
if l == "" {
// No severity specified, which means to ignore this field.
return nil
}
capsSeverity := strings.ToUpper(string(l))
if _, ok := severityToNumber[capsSeverity]; !ok {
return fmt.Errorf("'%s' is not a valid severity: %w", string(l), errInvalidSeverity)
}
return nil
}
// severityNumber returns the severity number that the logSeverity represents
func (l logSeverity) severityNumber() plog.SeverityNumber {
capsSeverity := strings.ToUpper(string(l))
return severityToNumber[capsSeverity]
}
// LogMatchProperties specifies the set of properties in a log to match against and the
// type of string pattern matching to use.
type LogMatchProperties struct {
// LogMatchType specifies the type of matching desired
LogMatchType LogMatchType `mapstructure:"match_type"`
// ResourceAttributes defines a list of possible resource attributes to match logs against.
// A match occurs if any resource attribute matches all expressions in this given list.
ResourceAttributes []filterconfig.Attribute `mapstructure:"resource_attributes"`
// RecordAttributes defines a list of possible record attributes to match logs against.
// A match occurs if any record attribute matches at least one expression in this given list.
RecordAttributes []filterconfig.Attribute `mapstructure:"record_attributes"`
// SeverityTexts is a list of strings that the LogRecord's severity text field must match
// against.
SeverityTexts []string `mapstructure:"severity_texts"`
// SeverityNumberProperties defines how to match against a log record's SeverityNumber, if defined.
SeverityNumberProperties *LogSeverityNumberMatchProperties `mapstructure:"severity_number"`
// LogBodies is a list of strings that the LogRecord's body field must match
// against.
LogBodies []string `mapstructure:"bodies"`
}
// validate checks that the LogMatchProperties is valid
func (lmp LogMatchProperties) validate() error {
if lmp.SeverityNumberProperties != nil {
return lmp.SeverityNumberProperties.validate()
}
return nil
}
// isEmpty returns true if the properties is "empty" (meaning, there are no filters specified)
// if this is the case, the filter should be ignored.
func (lmp LogMatchProperties) isEmpty() bool {
return len(lmp.ResourceAttributes) == 0 && len(lmp.RecordAttributes) == 0 &&
len(lmp.SeverityTexts) == 0 && len(lmp.LogBodies) == 0 &&
lmp.SeverityNumberProperties == nil
}
// matchProperties converts the LogMatchProperties to a corresponding filterconfig.MatchProperties
func (lmp LogMatchProperties) matchProperties() *filterconfig.MatchProperties {
mp := &filterconfig.MatchProperties{
Config: filterset.Config{
MatchType: filterset.MatchType(lmp.LogMatchType),
},
Resources: lmp.ResourceAttributes,
Attributes: lmp.RecordAttributes,
LogSeverityTexts: lmp.SeverityTexts,
LogBodies: lmp.LogBodies,
}
// Include SeverityNumberProperties if defined
if lmp.SeverityNumberProperties != nil {
mp.LogSeverityNumber = &filterconfig.LogSeverityNumberMatchProperties{
Min: lmp.SeverityNumberProperties.Min.severityNumber(),
MatchUndefined: lmp.SeverityNumberProperties.MatchUndefined,
}
}
return mp
}
type LogSeverityNumberMatchProperties struct {
// Min is the minimum severity needed for the log record to match.
// This corresponds to the short names specified here:
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#displaying-severity
// this field is case-insensitive ("INFO" == "info")
Min logSeverity `mapstructure:"min"`
// MatchUndefined lets logs records with "unknown" severity match.
// If MinSeverity is not set, this field is ignored, as fields are not matched based on severity.
MatchUndefined bool `mapstructure:"match_undefined"`
}
// validate checks that the LogMatchProperties is valid
func (lmp LogSeverityNumberMatchProperties) validate() error {
return lmp.Min.validate()
}
var _ component.Config = (*Config)(nil)
// Validate checks if the processor configuration is valid
func (cfg *Config) Validate() error {
if (cfg.Traces.SpanConditions != nil || cfg.Traces.SpanEventConditions != nil) && (cfg.Spans.Include != nil || cfg.Spans.Exclude != nil) {
return fmt.Errorf("cannot use ottl conditions and include/exclude for spans at the same time")
}
if (cfg.Metrics.MetricConditions != nil || cfg.Metrics.DataPointConditions != nil) && (cfg.Metrics.Include != nil || cfg.Metrics.Exclude != nil) {
return fmt.Errorf("cannot use ottl conditions and include/exclude for metrics at the same time")
}
if cfg.Logs.LogConditions != nil && (cfg.Logs.Include != nil || cfg.Logs.Exclude != nil) {
return fmt.Errorf("cannot use ottl conditions and include/exclude for logs at the same time")
}
var errors error
if cfg.Traces.SpanConditions != nil {
_, err := common.ParseSpan(cfg.Traces.SpanConditions, component.TelemetrySettings{})
errors = multierr.Append(errors, err)
}
if cfg.Traces.SpanEventConditions != nil {
_, err := common.ParseSpanEvent(cfg.Traces.SpanEventConditions, component.TelemetrySettings{})
errors = multierr.Append(errors, err)
}
if cfg.Metrics.MetricConditions != nil {
_, err := common.ParseMetric(cfg.Metrics.MetricConditions, component.TelemetrySettings{})
errors = multierr.Append(errors, err)
}
if cfg.Metrics.DataPointConditions != nil {
_, err := common.ParseDataPoint(cfg.Metrics.DataPointConditions, component.TelemetrySettings{})
errors = multierr.Append(errors, err)
}
if cfg.Logs.LogConditions != nil {
_, err := common.ParseLog(cfg.Logs.LogConditions, component.TelemetrySettings{})
errors = multierr.Append(errors, err)
}
if cfg.Logs.LogConditions != nil && cfg.Logs.Include != nil {
errors = multierr.Append(errors, cfg.Logs.Include.validate())
}
if cfg.Logs.LogConditions != nil && cfg.Logs.Exclude != nil {
errors = multierr.Append(errors, cfg.Logs.Exclude.validate())
}
return errors
}