@@ -17,27 +17,32 @@ package filterprocessor
17
17
import (
18
18
"context"
19
19
20
+ "go.opentelemetry.io/collector/internal/processor/filterconfig"
21
+ "go.opentelemetry.io/collector/internal/processor/filterspan"
22
+
20
23
"go.uber.org/zap"
21
24
22
25
"go.opentelemetry.io/collector/consumer/pdata"
23
26
"go.opentelemetry.io/collector/internal/processor/filtermetric"
24
27
"go.opentelemetry.io/collector/processor/processorhelper"
25
28
)
26
29
27
- type filterMetricProcessor struct {
28
- cfg * Config
29
- include filtermetric.Matcher
30
- exclude filtermetric.Matcher
31
- logger * zap.Logger
30
+ type filterProcessor struct {
31
+ cfg * Config
32
+ includeMetrics filtermetric.Matcher
33
+ excludeMetrics filtermetric.Matcher
34
+ includeSpans filterspan.Matcher
35
+ excludeSpans filterspan.Matcher
36
+ logger * zap.Logger
32
37
}
33
38
34
- func newFilterMetricProcessor (logger * zap.Logger , cfg * Config ) (* filterMetricProcessor , error ) {
35
- inc , err := createMatcher (cfg .Metrics .Include )
39
+ func newFilterMetricProcessor (logger * zap.Logger , cfg * Config ) (* filterProcessor , error ) {
40
+ inc , err := createMetricsMatcher (cfg .Metrics .Include )
36
41
if err != nil {
37
42
return nil , err
38
43
}
39
44
40
- exc , err := createMatcher (cfg .Metrics .Exclude )
45
+ exc , err := createMetricsMatcher (cfg .Metrics .Exclude )
41
46
if err != nil {
42
47
return nil , err
43
48
}
@@ -62,32 +67,86 @@ func newFilterMetricProcessor(logger *zap.Logger, cfg *Config) (*filterMetricPro
62
67
63
68
logger .Info (
64
69
"Metric filter configured" ,
65
- zap .String ("include match_type" , includeMatchType ),
66
- zap .Strings ("include expressions" , includeExpressions ),
67
- zap .Strings ("include metric names" , includeMetricNames ),
68
- zap .String ("exclude match_type" , excludeMatchType ),
69
- zap .Strings ("exclude expressions" , excludeExpressions ),
70
- zap .Strings ("exclude metric names" , excludeMetricNames ),
70
+ zap .String ("includeMetrics match_type" , includeMatchType ),
71
+ zap .Strings ("includeMetrics expressions" , includeExpressions ),
72
+ zap .Strings ("includeMetrics metric names" , includeMetricNames ),
73
+ zap .String ("excludeMetrics match_type" , excludeMatchType ),
74
+ zap .Strings ("excludeMetrics expressions" , excludeExpressions ),
75
+ zap .Strings ("excludeMetrics metric names" , excludeMetricNames ),
76
+ )
77
+
78
+ return & filterProcessor {
79
+ cfg : cfg ,
80
+ includeMetrics : inc ,
81
+ excludeMetrics : exc ,
82
+ logger : logger ,
83
+ }, nil
84
+ }
85
+
86
+ func newFilterSpanProcessor (logger * zap.Logger , cfg * Config ) (* filterProcessor , error ) {
87
+ inc , err := createSpansMatcher (cfg .Spans .Include )
88
+ if err != nil {
89
+ return nil , err
90
+ }
91
+
92
+ exc , err := createSpansMatcher (cfg .Spans .Exclude )
93
+ if err != nil {
94
+ return nil , err
95
+ }
96
+
97
+ includeMatchType := ""
98
+ var includeServices []string
99
+ var includeSpanNames []string
100
+ if cfg .Spans .Include != nil {
101
+ includeMatchType = string (cfg .Spans .Include .MatchType )
102
+ includeServices = cfg .Spans .Include .Services
103
+ includeSpanNames = cfg .Spans .Include .SpanNames
104
+ }
105
+
106
+ excludeMatchType := ""
107
+ var excludeServices []string
108
+ var excludeSpanNames []string
109
+ if cfg .Spans .Exclude != nil {
110
+ excludeMatchType = string (cfg .Spans .Exclude .MatchType )
111
+ excludeServices = cfg .Spans .Exclude .Services
112
+ excludeSpanNames = cfg .Spans .Exclude .SpanNames
113
+ }
114
+
115
+ logger .Info (
116
+ "Span filter configured" ,
117
+ zap .String ("includeSpans match_type" , includeMatchType ),
118
+ zap .Strings ("includeSpans services" , includeServices ),
119
+ zap .Strings ("includeSpans span names" , includeSpanNames ),
120
+ zap .String ("excludeSpans match_type" , excludeMatchType ),
121
+ zap .Strings ("excludeSpans services" , excludeServices ),
122
+ zap .Strings ("excludeSpans span names" , excludeSpanNames ),
71
123
)
72
124
73
- return & filterMetricProcessor {
74
- cfg : cfg ,
75
- include : inc ,
76
- exclude : exc ,
77
- logger : logger ,
125
+ return & filterProcessor {
126
+ cfg : cfg ,
127
+ includeSpans : inc ,
128
+ excludeSpans : exc ,
129
+ logger : logger ,
78
130
}, nil
79
131
}
80
132
81
- func createMatcher (mp * filtermetric.MatchProperties ) (filtermetric.Matcher , error ) {
133
+ func createSpansMatcher (mp * filterconfig.MatchProperties ) (filterspan.Matcher , error ) {
134
+ if mp == nil {
135
+ return nil , nil
136
+ }
137
+ return filterspan .NewMatcher (mp )
138
+ }
139
+
140
+ func createMetricsMatcher (mp * filtermetric.MatchProperties ) (filtermetric.Matcher , error ) {
82
141
// Nothing specified in configuration
83
142
if mp == nil {
84
143
return nil , nil
85
144
}
86
145
return filtermetric .NewMatcher (mp )
87
146
}
88
147
89
- // ProcessMetrics filters the given metrics based off the filterMetricProcessor 's filters.
90
- func (fmp * filterMetricProcessor ) ProcessMetrics (_ context.Context , pdm pdata.Metrics ) (pdata.Metrics , error ) {
148
+ // ProcessMetrics filters the given metrics based off the filterProcessor 's filters.
149
+ func (fmp * filterProcessor ) ProcessMetrics (_ context.Context , pdm pdata.Metrics ) (pdata.Metrics , error ) {
91
150
rms := pdm .ResourceMetrics ()
92
151
idx := newMetricIndex ()
93
152
for i := 0 ; i < rms .Len (); i ++ {
@@ -112,9 +171,34 @@ func (fmp *filterMetricProcessor) ProcessMetrics(_ context.Context, pdm pdata.Me
112
171
return idx .extract (pdm ), nil
113
172
}
114
173
115
- func (fmp * filterMetricProcessor ) shouldKeepMetric (metric pdata.Metric ) (bool , error ) {
116
- if fmp .include != nil {
117
- matches , err := fmp .include .MatchMetric (metric )
174
+ // ProcessTraces filters the given spans based off the filterProcessor's filters.
175
+ func (fmp * filterProcessor ) ProcessTraces (_ context.Context , pdt pdata.Traces ) (pdata.Traces , error ) {
176
+ rs := pdt .ResourceSpans ()
177
+ for i := 0 ; i < rs .Len (); i ++ {
178
+ rss := rs .At (i )
179
+ resource := rss .Resource ()
180
+ ils := rss .InstrumentationLibrarySpans ()
181
+
182
+ for j := 0 ; j < ils .Len (); j ++ {
183
+ ilss := ils .At (j )
184
+ library := ilss .InstrumentationLibrary ()
185
+ inputSpans := pdata .NewSpanSlice ()
186
+ ilss .Spans ().MoveAndAppendTo (inputSpans )
187
+ for k := 0 ; k < inputSpans .Len (); k ++ {
188
+ span := inputSpans .At (k )
189
+ if fmp .shouldKeepSpan (span , resource , library ) {
190
+ ilss .Spans ().Append (span )
191
+ }
192
+ }
193
+ }
194
+ }
195
+
196
+ return pdt , nil
197
+ }
198
+
199
+ func (fmp * filterProcessor ) shouldKeepMetric (metric pdata.Metric ) (bool , error ) {
200
+ if fmp .includeMetrics != nil {
201
+ matches , err := fmp .includeMetrics .MatchMetric (metric )
118
202
if err != nil {
119
203
// default to keep if there's an error
120
204
return true , err
@@ -124,8 +208,8 @@ func (fmp *filterMetricProcessor) shouldKeepMetric(metric pdata.Metric) (bool, e
124
208
}
125
209
}
126
210
127
- if fmp .exclude != nil {
128
- matches , err := fmp .exclude .MatchMetric (metric )
211
+ if fmp .excludeMetrics != nil {
212
+ matches , err := fmp .excludeMetrics .MatchMetric (metric )
129
213
if err != nil {
130
214
return true , err
131
215
}
@@ -136,3 +220,21 @@ func (fmp *filterMetricProcessor) shouldKeepMetric(metric pdata.Metric) (bool, e
136
220
137
221
return true , nil
138
222
}
223
+
224
+ func (fmp * filterProcessor ) shouldKeepSpan (span pdata.Span , resource pdata.Resource , library pdata.InstrumentationLibrary ) bool {
225
+ if fmp .includeSpans != nil {
226
+ matches := fmp .includeSpans .MatchSpan (span , resource , library )
227
+ if ! matches {
228
+ return false
229
+ }
230
+ }
231
+
232
+ if fmp .excludeSpans != nil {
233
+ matches := fmp .excludeSpans .MatchSpan (span , resource , library )
234
+ if matches {
235
+ return false
236
+ }
237
+ }
238
+
239
+ return true
240
+ }
0 commit comments