Skip to content

Commit

Permalink
If filterprocessor filters all data, stop further processing
Browse files Browse the repository at this point in the history
  • Loading branch information
anniefu committed Aug 5, 2020
1 parent 8af0a90 commit 4766920
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
4 changes: 3 additions & 1 deletion processor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,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}:
Expand All @@ -148,7 +150,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
Expand Down
9 changes: 8 additions & 1 deletion processor/filterprocessor/filter_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -62,21 +63,27 @@ 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
}
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
}

Expand Down
22 changes: 14 additions & 8 deletions processor/filterprocessor/filter_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -169,8 +170,8 @@ var (
MatchType: filterset.Strict,
},
},
inMN: [][]*metricspb.Metric{metricsWithName(inMetricNames)},
outMN: [][]string{{}},
inMN: [][]*metricspb.Metric{metricsWithName(inMetricNames)},
allMetricsFiltered: true,
},
{
name: "emptyFilterExclude",
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 8 additions & 0 deletions processor/processorhelper/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions processor/processorhelper/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4766920

Please sign in to comment.