If filterprocessor filters all data, stop further processing (#1500)
This commit is contained in:
parent
e68440edbb
commit
052b36eac7
|
@ -135,6 +135,8 @@ are checked before the `exclude` properties.
|
|||
|
||||
```yaml
|
||||
filter:
|
||||
# metrics indicates this processor applies to metrics
|
||||
metrics:
|
||||
# include and/or exclude can be specified. However, the include properties
|
||||
# are always checked before the exclude properties.
|
||||
{include, exclude}:
|
||||
|
@ -149,7 +151,7 @@ filter:
|
|||
|
||||
# metric_names specify an array of items to match the metric name against.
|
||||
# This is a required field.
|
||||
metric_name: [<item1>, ..., <itemN>]
|
||||
metric_names: [<item1>, ..., <itemN>]
|
||||
```
|
||||
|
||||
#### Match Configuration
|
||||
|
|
|
@ -22,6 +22,7 @@ import (
|
|||
"go.opentelemetry.io/collector/consumer/pdata"
|
||||
"go.opentelemetry.io/collector/consumer/pdatautil"
|
||||
"go.opentelemetry.io/collector/internal/processor/filtermetric"
|
||||
"go.opentelemetry.io/collector/processor/processorhelper"
|
||||
)
|
||||
|
||||
type filterMetricProcessor struct {
|
||||
|
@ -62,9 +63,10 @@ func createMatcher(mp *filtermetric.MatchProperties) (*filtermetric.Matcher, err
|
|||
return &matcher, nil
|
||||
}
|
||||
|
||||
// ProcessMetrics filters the given spans based off the filterMetricProcessor's filters.
|
||||
// ProcessMetrics filters the given metrics based off the filterMetricProcessor's filters.
|
||||
func (fmp *filterMetricProcessor) ProcessMetrics(_ context.Context, md pdata.Metrics) (pdata.Metrics, error) {
|
||||
mds := pdatautil.MetricsToMetricsData(md)
|
||||
foundMetricToKeep := false
|
||||
for i := range mds {
|
||||
if len(mds[i].Metrics) == 0 {
|
||||
continue
|
||||
|
@ -72,11 +74,16 @@ func (fmp *filterMetricProcessor) ProcessMetrics(_ context.Context, md pdata.Met
|
|||
keep := make([]*metricspb.Metric, 0, len(mds[i].Metrics))
|
||||
for _, m := range mds[i].Metrics {
|
||||
if fmp.shouldKeepMetric(m) {
|
||||
foundMetricToKeep = true
|
||||
keep = append(keep, m)
|
||||
}
|
||||
}
|
||||
mds[i].Metrics = keep
|
||||
}
|
||||
|
||||
if !foundMetricToKeep {
|
||||
return md, processorhelper.ErrSkipProcessingData
|
||||
}
|
||||
return pdatautil.MetricsFromMetricsData(mds), nil
|
||||
}
|
||||
|
||||
|
|
|
@ -32,11 +32,12 @@ import (
|
|||
)
|
||||
|
||||
type metricNameTest struct {
|
||||
name string
|
||||
inc *filtermetric.MatchProperties
|
||||
exc *filtermetric.MatchProperties
|
||||
inMN [][]*metricspb.Metric // input Metric batches
|
||||
outMN [][]string // output Metric names
|
||||
name string
|
||||
inc *filtermetric.MatchProperties
|
||||
exc *filtermetric.MatchProperties
|
||||
inMN [][]*metricspb.Metric // input Metric batches
|
||||
outMN [][]string // output Metric names
|
||||
allMetricsFiltered bool
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -169,8 +170,8 @@ var (
|
|||
MatchType: filterset.Strict,
|
||||
},
|
||||
},
|
||||
inMN: [][]*metricspb.Metric{metricsWithName(inMetricNames)},
|
||||
outMN: [][]string{{}},
|
||||
inMN: [][]*metricspb.Metric{metricsWithName(inMetricNames)},
|
||||
allMetricsFiltered: true,
|
||||
},
|
||||
{
|
||||
name: "emptyFilterExclude",
|
||||
|
@ -220,8 +221,13 @@ func TestFilterMetricProcessor(t *testing.T) {
|
|||
context.Background(),
|
||||
pdatautil.MetricsFromMetricsData(mds))
|
||||
assert.Nil(t, cErr)
|
||||
|
||||
got := next.AllMetrics()
|
||||
|
||||
if test.allMetricsFiltered {
|
||||
require.Equal(t, 0, len(got))
|
||||
return
|
||||
}
|
||||
|
||||
require.Equal(t, 1, len(got))
|
||||
gotMD := pdatautil.MetricsToMetricsData(got[0])
|
||||
require.Equal(t, len(test.outMN), len(gotMD))
|
||||
|
|
|
@ -26,6 +26,11 @@ import (
|
|||
"go.opentelemetry.io/collector/obsreport"
|
||||
)
|
||||
|
||||
// ErrSkipProcessingData is a sentinel value to indicate when traces or metrics should intentionally be dropped
|
||||
// from further processing in the pipeline because the data is determined to be irrelevant. A processor can return this error
|
||||
// to stop further processing without propagating an error back up the pipeline to logs.
|
||||
var ErrSkipProcessingData = errors.New("sentinel error to skip processing data from the remainder of the pipeline")
|
||||
|
||||
// Start specifies the function invoked when the processor is being started.
|
||||
type Start func(context.Context, component.Host) error
|
||||
|
||||
|
@ -172,6 +177,9 @@ func (mp *metricsProcessor) ConsumeMetrics(ctx context.Context, md pdata.Metrics
|
|||
var err error
|
||||
md, err = mp.processor.ProcessMetrics(processorCtx, md)
|
||||
if err != nil {
|
||||
if err == ErrSkipProcessingData {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return mp.nextConsumer.ConsumeMetrics(ctx, md)
|
||||
|
|
|
@ -129,6 +129,12 @@ func TestNewMetricsExporter_ProcessMetricsError(t *testing.T) {
|
|||
assert.Equal(t, want, me.ConsumeMetrics(context.Background(), pdatautil.MetricsFromInternalMetrics(testdata.GenerateMetricDataEmpty())))
|
||||
}
|
||||
|
||||
func TestNewMetricsExporter_ProcessMetricsErrSkipProcessingData(t *testing.T) {
|
||||
me, err := NewMetricsProcessor(testCfg, exportertest.NewNopMetricsExporter(), newTestMProcessor(ErrSkipProcessingData))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, nil, me.ConsumeMetrics(context.Background(), pdatautil.MetricsFromInternalMetrics(testdata.GenerateMetricDataEmpty())))
|
||||
}
|
||||
|
||||
func TestNewLogsExporter(t *testing.T) {
|
||||
me, err := NewLogsProcessor(testCfg, exportertest.NewNopLogsExporter(), newTestLProcessor(nil))
|
||||
require.NoError(t, err)
|
||||
|
|
Loading…
Reference in New Issue