Skip to content

Commit

Permalink
[chore] refactor filter processor test to use generated test harness (#…
Browse files Browse the repository at this point in the history
…34940)

The code in telemetry_test.go was mostly doing something the code
generated by mdatagen was doing. Updated the tests to use that instead.

Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
codeboten authored Aug 30, 2024
1 parent 6b3237a commit 2d63c17
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 216 deletions.
40 changes: 29 additions & 11 deletions processor/filterprocessor/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/processor/processorhelper"
"go.opentelemetry.io/collector/processor/processortest"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric/metricdata"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
Expand Down Expand Up @@ -764,19 +766,35 @@ func TestFilterLogProcessorWithOTTL(t *testing.T) {
}

func TestFilterLogProcessorTelemetry(t *testing.T) {
telemetryTest(t, "FilterLogProcessorTelemetry", func(t *testing.T, tel testTelemetry) {
processor, err := newFilterLogsProcessor(tel.NewProcessorCreateSettings(), &Config{
Logs: LogFilters{LogConditions: []string{`IsMatch(body, "operationA")`}},
})
assert.NoError(t, err)
tel := setupTestTelemetry()
processor, err := newFilterLogsProcessor(tel.NewSettings(), &Config{
Logs: LogFilters{LogConditions: []string{`IsMatch(body, "operationA")`}},
})
assert.NoError(t, err)

_, err = processor.processLogs(context.Background(), constructLogs())
assert.NoError(t, err)
_, err = processor.processLogs(context.Background(), constructLogs())
assert.NoError(t, err)

want := []metricdata.Metrics{
{
Name: "otelcol_processor_filter_logs.filtered",
Description: "Number of logs dropped by the filter processor",
Unit: "1",
Data: metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[int64]{
{
Value: 2,
Attributes: attribute.NewSet(attribute.String("filter", "filter")),
},
},
},
},
}

tel.assertMetrics(t, want)

tel.assertMetrics(t, expectedMetrics{
logsFiltered: 2,
})
})
}

func constructLogs() plog.Logs {
Expand Down
159 changes: 103 additions & 56 deletions processor/filterprocessor/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/processor/processorhelper"
"go.opentelemetry.io/collector/processor/processortest"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric/metricdata"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/goldendataset"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterconfig"
Expand Down Expand Up @@ -366,74 +368,119 @@ func TestFilterMetricProcessor(t *testing.T) {
}

func TestFilterMetricProcessorTelemetry(t *testing.T) {
telemetryTest(t, "FilterMetricProcessorTelemetry", func(t *testing.T, tel testTelemetry) {
next := new(consumertest.MetricsSink)
cfg := &Config{
Metrics: MetricFilters{
MetricConditions: []string{
"name==\"metric1\"",
},
tel := setupTestTelemetry()
next := new(consumertest.MetricsSink)
cfg := &Config{
Metrics: MetricFilters{
MetricConditions: []string{
"name==\"metric1\"",
},
}
factory := NewFactory()
fmp, err := factory.CreateMetricsProcessor(
context.Background(),
tel.NewProcessorCreateSettings(),
cfg,
next,
)
assert.NotNil(t, fmp)
assert.NoError(t, err)

caps := fmp.Capabilities()
assert.True(t, caps.MutatesData)
ctx := context.Background()
assert.NoError(t, fmp.Start(ctx, nil))

err = fmp.ConsumeMetrics(context.Background(), testResourceMetrics([]metricWithResource{
{
metricNames: []string{"foo", "bar"},
resourceAttributes: map[string]any{
"attr1": "attr1/val1",
},
}
factory := NewFactory()
fmp, err := factory.CreateMetricsProcessor(
context.Background(),
tel.NewSettings(),
cfg,
next,
)
assert.NotNil(t, fmp)
assert.NoError(t, err)

caps := fmp.Capabilities()
assert.True(t, caps.MutatesData)
ctx := context.Background()
assert.NoError(t, fmp.Start(ctx, nil))

err = fmp.ConsumeMetrics(context.Background(), testResourceMetrics([]metricWithResource{
{
metricNames: []string{"foo", "bar"},
resourceAttributes: map[string]any{
"attr1": "attr1/val1",
},
},
}))
assert.NoError(t, err)

want := []metricdata.Metrics{
{
Name: "otelcol_processor_filter_datapoints.filtered",
Description: "Number of metric data points dropped by the filter processor",
Unit: "1",
Data: metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[int64]{
{
Value: 0,
Attributes: attribute.NewSet(attribute.String("filter", "filter")),
},
},
},
}))
assert.NoError(t, err)
},
}

tel.assertMetrics(t, expectedMetrics{
metricDataPointsFiltered: 0,
})
tel.assertMetrics(t, want)

err = fmp.ConsumeMetrics(context.Background(), testResourceMetrics([]metricWithResource{
{
metricNames: []string{"metric1", "metric2"},
resourceAttributes: map[string]any{
"attr1": "attr1/val1",
},
},
}))
assert.NoError(t, err)

err = fmp.ConsumeMetrics(context.Background(), testResourceMetrics([]metricWithResource{
{
metricNames: []string{"metric1", "metric2"},
resourceAttributes: map[string]any{
"attr1": "attr1/val1",
want = []metricdata.Metrics{
{
Name: "otelcol_processor_filter_datapoints.filtered",
Description: "Number of metric data points dropped by the filter processor",
Unit: "1",
Data: metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[int64]{
{
Value: 1,
Attributes: attribute.NewSet(attribute.String("filter", "filter")),
},
},
},
}))
assert.NoError(t, err)
},
}
tel.assertMetrics(t, want)

tel.assertMetrics(t, expectedMetrics{
metricDataPointsFiltered: 1,
})
err = fmp.ConsumeMetrics(context.Background(), testResourceMetrics([]metricWithResource{
{
metricNames: []string{"metric1"},
resourceAttributes: map[string]any{
"attr1": "attr1/val1",
},
},
}))
assert.NoError(t, err)

err = fmp.ConsumeMetrics(context.Background(), testResourceMetrics([]metricWithResource{
{
metricNames: []string{"metric1"},
resourceAttributes: map[string]any{
"attr1": "attr1/val1",
want = []metricdata.Metrics{
{
Name: "otelcol_processor_filter_datapoints.filtered",
Description: "Number of metric data points dropped by the filter processor",
Unit: "1",
Data: metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[int64]{
{
Value: 2,
Attributes: attribute.NewSet(attribute.String("filter", "filter")),
},
},
},
}))
assert.NoError(t, err)

tel.assertMetrics(t, expectedMetrics{
metricDataPointsFiltered: 2,
})
},
}
tel.assertMetrics(t, want)

assert.NoError(t, fmp.Shutdown(ctx))
})
assert.NoError(t, fmp.Shutdown(ctx))
}

func testResourceMetrics(mwrs []metricWithResource) pmetric.Metrics {
Expand Down
134 changes: 0 additions & 134 deletions processor/filterprocessor/telemetry_test.go

This file was deleted.

Loading

0 comments on commit 2d63c17

Please sign in to comment.