Skip to content

Commit a9c4983

Browse files
codebotenf7o
authored andcommitted
[chore] refactor filter processor test to use generated test harness (open-telemetry#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 <[email protected]>
1 parent 2afacae commit a9c4983

File tree

4 files changed

+164
-216
lines changed

4 files changed

+164
-216
lines changed

processor/filterprocessor/logs_test.go

+29-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"go.opentelemetry.io/collector/pdata/plog"
1616
"go.opentelemetry.io/collector/processor/processorhelper"
1717
"go.opentelemetry.io/collector/processor/processortest"
18+
"go.opentelemetry.io/otel/attribute"
19+
"go.opentelemetry.io/otel/sdk/metric/metricdata"
1820

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

766768
func TestFilterLogProcessorTelemetry(t *testing.T) {
767-
telemetryTest(t, "FilterLogProcessorTelemetry", func(t *testing.T, tel testTelemetry) {
768-
processor, err := newFilterLogsProcessor(tel.NewProcessorCreateSettings(), &Config{
769-
Logs: LogFilters{LogConditions: []string{`IsMatch(body, "operationA")`}},
770-
})
771-
assert.NoError(t, err)
769+
tel := setupTestTelemetry()
770+
processor, err := newFilterLogsProcessor(tel.NewSettings(), &Config{
771+
Logs: LogFilters{LogConditions: []string{`IsMatch(body, "operationA")`}},
772+
})
773+
assert.NoError(t, err)
772774

773-
_, err = processor.processLogs(context.Background(), constructLogs())
774-
assert.NoError(t, err)
775+
_, err = processor.processLogs(context.Background(), constructLogs())
776+
assert.NoError(t, err)
777+
778+
want := []metricdata.Metrics{
779+
{
780+
Name: "otelcol_processor_filter_logs.filtered",
781+
Description: "Number of logs dropped by the filter processor",
782+
Unit: "1",
783+
Data: metricdata.Sum[int64]{
784+
Temporality: metricdata.CumulativeTemporality,
785+
IsMonotonic: true,
786+
DataPoints: []metricdata.DataPoint[int64]{
787+
{
788+
Value: 2,
789+
Attributes: attribute.NewSet(attribute.String("filter", "filter")),
790+
},
791+
},
792+
},
793+
},
794+
}
795+
796+
tel.assertMetrics(t, want)
775797

776-
tel.assertMetrics(t, expectedMetrics{
777-
logsFiltered: 2,
778-
})
779-
})
780798
}
781799

782800
func constructLogs() plog.Logs {

processor/filterprocessor/metrics_test.go

+103-56
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"go.opentelemetry.io/collector/pdata/pmetric"
1717
"go.opentelemetry.io/collector/processor/processorhelper"
1818
"go.opentelemetry.io/collector/processor/processortest"
19+
"go.opentelemetry.io/otel/attribute"
20+
"go.opentelemetry.io/otel/sdk/metric/metricdata"
1921

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

368370
func TestFilterMetricProcessorTelemetry(t *testing.T) {
369-
telemetryTest(t, "FilterMetricProcessorTelemetry", func(t *testing.T, tel testTelemetry) {
370-
next := new(consumertest.MetricsSink)
371-
cfg := &Config{
372-
Metrics: MetricFilters{
373-
MetricConditions: []string{
374-
"name==\"metric1\"",
375-
},
371+
tel := setupTestTelemetry()
372+
next := new(consumertest.MetricsSink)
373+
cfg := &Config{
374+
Metrics: MetricFilters{
375+
MetricConditions: []string{
376+
"name==\"metric1\"",
376377
},
377-
}
378-
factory := NewFactory()
379-
fmp, err := factory.CreateMetricsProcessor(
380-
context.Background(),
381-
tel.NewProcessorCreateSettings(),
382-
cfg,
383-
next,
384-
)
385-
assert.NotNil(t, fmp)
386-
assert.NoError(t, err)
387-
388-
caps := fmp.Capabilities()
389-
assert.True(t, caps.MutatesData)
390-
ctx := context.Background()
391-
assert.NoError(t, fmp.Start(ctx, nil))
392-
393-
err = fmp.ConsumeMetrics(context.Background(), testResourceMetrics([]metricWithResource{
394-
{
395-
metricNames: []string{"foo", "bar"},
396-
resourceAttributes: map[string]any{
397-
"attr1": "attr1/val1",
378+
},
379+
}
380+
factory := NewFactory()
381+
fmp, err := factory.CreateMetricsProcessor(
382+
context.Background(),
383+
tel.NewSettings(),
384+
cfg,
385+
next,
386+
)
387+
assert.NotNil(t, fmp)
388+
assert.NoError(t, err)
389+
390+
caps := fmp.Capabilities()
391+
assert.True(t, caps.MutatesData)
392+
ctx := context.Background()
393+
assert.NoError(t, fmp.Start(ctx, nil))
394+
395+
err = fmp.ConsumeMetrics(context.Background(), testResourceMetrics([]metricWithResource{
396+
{
397+
metricNames: []string{"foo", "bar"},
398+
resourceAttributes: map[string]any{
399+
"attr1": "attr1/val1",
400+
},
401+
},
402+
}))
403+
assert.NoError(t, err)
404+
405+
want := []metricdata.Metrics{
406+
{
407+
Name: "otelcol_processor_filter_datapoints.filtered",
408+
Description: "Number of metric data points dropped by the filter processor",
409+
Unit: "1",
410+
Data: metricdata.Sum[int64]{
411+
Temporality: metricdata.CumulativeTemporality,
412+
IsMonotonic: true,
413+
DataPoints: []metricdata.DataPoint[int64]{
414+
{
415+
Value: 0,
416+
Attributes: attribute.NewSet(attribute.String("filter", "filter")),
417+
},
398418
},
399419
},
400-
}))
401-
assert.NoError(t, err)
420+
},
421+
}
402422

403-
tel.assertMetrics(t, expectedMetrics{
404-
metricDataPointsFiltered: 0,
405-
})
423+
tel.assertMetrics(t, want)
424+
425+
err = fmp.ConsumeMetrics(context.Background(), testResourceMetrics([]metricWithResource{
426+
{
427+
metricNames: []string{"metric1", "metric2"},
428+
resourceAttributes: map[string]any{
429+
"attr1": "attr1/val1",
430+
},
431+
},
432+
}))
433+
assert.NoError(t, err)
406434

407-
err = fmp.ConsumeMetrics(context.Background(), testResourceMetrics([]metricWithResource{
408-
{
409-
metricNames: []string{"metric1", "metric2"},
410-
resourceAttributes: map[string]any{
411-
"attr1": "attr1/val1",
435+
want = []metricdata.Metrics{
436+
{
437+
Name: "otelcol_processor_filter_datapoints.filtered",
438+
Description: "Number of metric data points dropped by the filter processor",
439+
Unit: "1",
440+
Data: metricdata.Sum[int64]{
441+
Temporality: metricdata.CumulativeTemporality,
442+
IsMonotonic: true,
443+
DataPoints: []metricdata.DataPoint[int64]{
444+
{
445+
Value: 1,
446+
Attributes: attribute.NewSet(attribute.String("filter", "filter")),
447+
},
412448
},
413449
},
414-
}))
415-
assert.NoError(t, err)
450+
},
451+
}
452+
tel.assertMetrics(t, want)
416453

417-
tel.assertMetrics(t, expectedMetrics{
418-
metricDataPointsFiltered: 1,
419-
})
454+
err = fmp.ConsumeMetrics(context.Background(), testResourceMetrics([]metricWithResource{
455+
{
456+
metricNames: []string{"metric1"},
457+
resourceAttributes: map[string]any{
458+
"attr1": "attr1/val1",
459+
},
460+
},
461+
}))
462+
assert.NoError(t, err)
420463

421-
err = fmp.ConsumeMetrics(context.Background(), testResourceMetrics([]metricWithResource{
422-
{
423-
metricNames: []string{"metric1"},
424-
resourceAttributes: map[string]any{
425-
"attr1": "attr1/val1",
464+
want = []metricdata.Metrics{
465+
{
466+
Name: "otelcol_processor_filter_datapoints.filtered",
467+
Description: "Number of metric data points dropped by the filter processor",
468+
Unit: "1",
469+
Data: metricdata.Sum[int64]{
470+
Temporality: metricdata.CumulativeTemporality,
471+
IsMonotonic: true,
472+
DataPoints: []metricdata.DataPoint[int64]{
473+
{
474+
Value: 2,
475+
Attributes: attribute.NewSet(attribute.String("filter", "filter")),
476+
},
426477
},
427478
},
428-
}))
429-
assert.NoError(t, err)
430-
431-
tel.assertMetrics(t, expectedMetrics{
432-
metricDataPointsFiltered: 2,
433-
})
479+
},
480+
}
481+
tel.assertMetrics(t, want)
434482

435-
assert.NoError(t, fmp.Shutdown(ctx))
436-
})
483+
assert.NoError(t, fmp.Shutdown(ctx))
437484
}
438485

439486
func testResourceMetrics(mwrs []metricWithResource) pmetric.Metrics {

processor/filterprocessor/telemetry_test.go

-134
This file was deleted.

0 commit comments

Comments
 (0)