-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
filter_processor.go
188 lines (165 loc) · 5.85 KB
/
filter_processor.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
// 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
//
// 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 (
"context"
"go.uber.org/zap"
"go.opentelemetry.io/collector/model/pdata"
"go.opentelemetry.io/collector/processor/processorhelper"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/processor/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/processor/filtermatcher"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/processor/filtermetric"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/processor/filterset"
)
type filterMetricProcessor struct {
cfg *Config
include filtermetric.Matcher
includeAttribute filtermatcher.AttributesMatcher
exclude filtermetric.Matcher
excludeAttribute filtermatcher.AttributesMatcher
logger *zap.Logger
}
func newFilterMetricProcessor(logger *zap.Logger, cfg *Config) (*filterMetricProcessor, error) {
inc, includeAttr, err := createMatcher(cfg.Metrics.Include)
if err != nil {
return nil, err
}
exc, excludeAttr, err := createMatcher(cfg.Metrics.Exclude)
if err != nil {
return nil, err
}
includeMatchType := ""
var includeExpressions []string
var includeMetricNames []string
var includeResourceAttributes []filterconfig.Attribute
if cfg.Metrics.Include != nil {
includeMatchType = string(cfg.Metrics.Include.MatchType)
includeExpressions = cfg.Metrics.Include.Expressions
includeMetricNames = cfg.Metrics.Include.MetricNames
includeResourceAttributes = cfg.Metrics.Include.ResourceAttributes
}
excludeMatchType := ""
var excludeExpressions []string
var excludeMetricNames []string
var excludeResourceAttributes []filterconfig.Attribute
if cfg.Metrics.Exclude != nil {
excludeMatchType = string(cfg.Metrics.Exclude.MatchType)
excludeExpressions = cfg.Metrics.Exclude.Expressions
excludeMetricNames = cfg.Metrics.Exclude.MetricNames
excludeResourceAttributes = cfg.Metrics.Exclude.ResourceAttributes
}
logger.Info(
"Metric filter configured",
zap.String("include match_type", includeMatchType),
zap.Strings("include expressions", includeExpressions),
zap.Strings("include metric names", includeMetricNames),
zap.Any("include metrics with resource attributes", includeResourceAttributes),
zap.String("exclude match_type", excludeMatchType),
zap.Strings("exclude expressions", excludeExpressions),
zap.Strings("exclude metric names", excludeMetricNames),
zap.Any("exclude metrics with resource attributes", excludeResourceAttributes),
)
return &filterMetricProcessor{
cfg: cfg,
include: inc,
includeAttribute: includeAttr,
exclude: exc,
excludeAttribute: excludeAttr,
logger: logger,
}, nil
}
func createMatcher(mp *filtermetric.MatchProperties) (filtermetric.Matcher, filtermatcher.AttributesMatcher, error) {
// Nothing specified in configuration
if mp == nil {
return nil, nil, nil
}
var attributeMatcher filtermatcher.AttributesMatcher
attributeMatcher, err := filtermatcher.NewAttributesMatcher(
filterset.Config{
MatchType: filterset.MatchType(mp.MatchType),
RegexpConfig: mp.RegexpConfig,
},
mp.ResourceAttributes,
)
if err != nil {
return nil, attributeMatcher, err
}
nameMatcher, err := filtermetric.NewMatcher(mp)
return nameMatcher, attributeMatcher, err
}
// processMetrics filters the given metrics based off the filterMetricProcessor's filters.
func (fmp *filterMetricProcessor) processMetrics(_ context.Context, pdm pdata.Metrics) (pdata.Metrics, error) {
pdm.ResourceMetrics().RemoveIf(func(rm pdata.ResourceMetrics) bool {
keepMetricsForResource := fmp.shouldKeepMetricsForResource(rm.Resource())
if !keepMetricsForResource {
return true
}
rm.InstrumentationLibraryMetrics().RemoveIf(func(ilm pdata.InstrumentationLibraryMetrics) bool {
ilm.Metrics().RemoveIf(func(m pdata.Metric) bool {
keep, err := fmp.shouldKeepMetric(m)
if err != nil {
fmp.logger.Error("shouldKeepMetric failed", zap.Error(err))
// don't `return`, keep the metric if there's an error
}
return !keep
})
// Filter out empty InstrumentationLibraryMetrics
return ilm.Metrics().Len() == 0
})
// Filter out empty ResourceMetrics
return rm.InstrumentationLibraryMetrics().Len() == 0
})
if pdm.ResourceMetrics().Len() == 0 {
return pdm, processorhelper.ErrSkipProcessingData
}
return pdm, nil
}
func (fmp *filterMetricProcessor) shouldKeepMetric(metric pdata.Metric) (bool, error) {
if fmp.include != nil {
matches, err := fmp.include.MatchMetric(metric)
if err != nil {
// default to keep if there's an error
return true, err
}
if !matches {
return false, nil
}
}
if fmp.exclude != nil {
matches, err := fmp.exclude.MatchMetric(metric)
if err != nil {
return true, err
}
if matches {
return false, nil
}
}
return true, nil
}
func (fmp *filterMetricProcessor) shouldKeepMetricsForResource(resource pdata.Resource) bool {
resourceAttributes := resource.Attributes()
if fmp.include != nil && fmp.includeAttribute != nil {
matches := fmp.includeAttribute.Match(resourceAttributes)
if !matches {
return false
}
}
if fmp.exclude != nil && fmp.excludeAttribute != nil {
matches := fmp.excludeAttribute.Match(resourceAttributes)
if matches {
return false
}
}
return true
}