Skip to content

Commit a001453

Browse files
committed
feat: 优化 collector metricsfilter dropAction 部分逻辑,补充丰富单元测试用例
1 parent aaa7855 commit a001453

File tree

2 files changed

+83
-13
lines changed

2 files changed

+83
-13
lines changed

pkg/collector/processor/metricsfilter/factory.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,20 @@ func (p *metricsFilter) dropAction(record *define.Record, config Config) {
149149
resourceMetrics.ScopeMetrics().RemoveIf(func(scopeMetrics pmetric.ScopeMetrics) bool {
150150
scopeMetrics.Metrics().RemoveIf(func(metric pmetric.Metric) bool {
151151
ruleMatch := true
152-
if len(action.Rules) > 0 {
153-
for _, rule := range action.Rules {
154-
ff, pk := fields.DecodeFieldFrom(rule.PredicateKey)
155-
switch ff {
156-
// TODO(aivan): 目前 predicateKey 暂时只支持 resource 后续可能会扩展
157-
case fields.FieldFromResource:
158-
rv, ok := rs.Get(pk)
159-
if !ok || !opmatch.Match(rv.AsString(), rule.MatchConfig.Value, rule.MatchConfig.Op) {
160-
ruleMatch = false
161-
}
162-
default:
163-
logger.Errorf("unsupported field %s", rule.PredicateKey)
152+
for _, rule := range action.Rules {
153+
ff, pk := fields.DecodeFieldFrom(rule.PredicateKey)
154+
switch ff {
155+
// TODO(aivan): 目前 predicateKey 暂时只支持 resource 后续可能会扩展
156+
case fields.FieldFromResource:
157+
rv, ok := rs.Get(pk)
158+
if !ok || !opmatch.Match(rv.AsString(), rule.MatchConfig.Value, rule.MatchConfig.Op) {
159+
ruleMatch = false
160+
break
164161
}
162+
default:
163+
logger.Warnf("unsupported field %s", rule.PredicateKey)
164+
ruleMatch = false
165+
break
165166
}
166167
}
167168
return action.MetricMatch(metric.Name()) && ruleMatch

pkg/collector/processor/metricsfilter/factory_test.go

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ processor:
181181
assert.Equal(t, 0, metrics.Len())
182182
})
183183

184-
t.Run("complex_notin", func(t *testing.T) {
184+
t.Run("notin_with_one_rules", func(t *testing.T) {
185185
content := `
186186
processor:
187187
- name: "metrics_filter/drop"
@@ -210,6 +210,75 @@ processor:
210210
metrics := record.Data.(pmetric.Metrics).ResourceMetrics()
211211
assert.Equal(t, 0, metrics.Len())
212212
})
213+
214+
t.Run("notin_with_multiple_rules_all_match", func(t *testing.T) {
215+
content := `
216+
processor:
217+
- name: "metrics_filter/drop"
218+
config:
219+
drop:
220+
metrics:
221+
- "my_metrics111"
222+
op: "notin"
223+
extra_rules:
224+
- predicate_key: "resource.telemetry.distro.name"
225+
match:
226+
op: "eq"
227+
value: "opentelemetry-java-instrumentation"
228+
- predicate_key: "resource.telemetry.sdk.language"
229+
match:
230+
op: "eq"
231+
value: "java"
232+
`
233+
factory := processor.MustCreateFactory(content, NewFactory)
234+
235+
record := define.Record{
236+
RecordType: define.RecordMetrics,
237+
Data: makeMetricsRecordWith(
238+
"my_metrics", 1, map[string]string{
239+
"telemetry.distro.name": "opentelemetry-java-instrumentation",
240+
"telemetry.sdk.language": "java",
241+
}, map[string]string{}),
242+
}
243+
244+
testkits.MustProcess(t, factory, record)
245+
metrics := record.Data.(pmetric.Metrics).ResourceMetrics()
246+
assert.Equal(t, 0, metrics.Len())
247+
})
248+
249+
t.Run("notin_with_multiple_rules_partial_match", func(t *testing.T) {
250+
content := `
251+
processor:
252+
- name: "metrics_filter/drop"
253+
config:
254+
drop:
255+
metrics:
256+
- "my_metrics111"
257+
op: "notin"
258+
extra_rules:
259+
- predicate_key: "resource.telemetry.distro.name"
260+
match:
261+
op: "eq"
262+
value: "opentelemetry-java-instrumentation"
263+
- predicate_key: "attributes.telemetry.sdk.language"
264+
match:
265+
op: "eq"
266+
value: "java"
267+
`
268+
factory := processor.MustCreateFactory(content, NewFactory)
269+
270+
record := define.Record{
271+
RecordType: define.RecordMetrics,
272+
Data: makeMetricsRecordWith(
273+
"my_metrics", 1, map[string]string{
274+
"telemetry.distro.name": "opentelemetry-java-instrumentation",
275+
}, map[string]string{}),
276+
}
277+
278+
testkits.MustProcess(t, factory, record)
279+
metrics := record.Data.(pmetric.Metrics).ResourceMetrics()
280+
assert.Equal(t, 1, metrics.Len())
281+
})
213282
}
214283

215284
func TestMetricsReplaceAction(t *testing.T) {

0 commit comments

Comments
 (0)