diff --git a/processor/filterprocessor/documentation.md b/processor/filterprocessor/documentation.md new file mode 100644 index 000000000000..b9bb78c5b78d --- /dev/null +++ b/processor/filterprocessor/documentation.md @@ -0,0 +1,31 @@ +[comment]: <> (Code generated by mdatagen. DO NOT EDIT.) + +# filter + +## Internal Telemetry + +The following telemetry is emitted by this component. + +### processor_filter_datapoints.filtered + +Number of metric data points dropped by the filter processor + +| Unit | Metric Type | Value Type | Monotonic | +| ---- | ----------- | ---------- | --------- | +| 1 | Sum | Int | true | + +### processor_filter_logs.filtered + +Number of logs dropped by the filter processor + +| Unit | Metric Type | Value Type | Monotonic | +| ---- | ----------- | ---------- | --------- | +| 1 | Sum | Int | true | + +### processor_filter_spans.filtered + +Number of spans dropped by the filter processor + +| Unit | Metric Type | Value Type | Monotonic | +| ---- | ----------- | ---------- | --------- | +| 1 | Sum | Int | true | diff --git a/processor/filterprocessor/internal/metadata/generated_telemetry.go b/processor/filterprocessor/internal/metadata/generated_telemetry.go index 9befd241aef7..d04a9bca0885 100644 --- a/processor/filterprocessor/internal/metadata/generated_telemetry.go +++ b/processor/filterprocessor/internal/metadata/generated_telemetry.go @@ -3,6 +3,8 @@ package metadata import ( + "errors" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/trace" @@ -15,3 +17,44 @@ func Meter(settings component.TelemetrySettings) metric.Meter { func Tracer(settings component.TelemetrySettings) trace.Tracer { return settings.TracerProvider.Tracer("otelcol/filter") } + +// TelemetryBuilder provides an interface for components to report telemetry +// as defined in metadata and user config. +type TelemetryBuilder struct { + ProcessorFilterDatapointsFiltered metric.Int64Counter + ProcessorFilterLogsFiltered metric.Int64Counter + ProcessorFilterSpansFiltered metric.Int64Counter +} + +// telemetryBuilderOption applies changes to default builder. +type telemetryBuilderOption func(*TelemetryBuilder) + +// NewTelemetryBuilder provides a struct with methods to update all internal telemetry +// for a component +func NewTelemetryBuilder(settings component.TelemetrySettings, options ...telemetryBuilderOption) (*TelemetryBuilder, error) { + builder := TelemetryBuilder{} + for _, op := range options { + op(&builder) + } + var err, errs error + meter := Meter(settings) + builder.ProcessorFilterDatapointsFiltered, err = meter.Int64Counter( + "processor_filter_datapoints.filtered", + metric.WithDescription("Number of metric data points dropped by the filter processor"), + metric.WithUnit("1"), + ) + errs = errors.Join(errs, err) + builder.ProcessorFilterLogsFiltered, err = meter.Int64Counter( + "processor_filter_logs.filtered", + metric.WithDescription("Number of logs dropped by the filter processor"), + metric.WithUnit("1"), + ) + errs = errors.Join(errs, err) + builder.ProcessorFilterSpansFiltered, err = meter.Int64Counter( + "processor_filter_spans.filtered", + metric.WithDescription("Number of spans dropped by the filter processor"), + metric.WithUnit("1"), + ) + errs = errors.Join(errs, err) + return &builder, errs +} diff --git a/processor/filterprocessor/internal/metadata/generated_telemetry_test.go b/processor/filterprocessor/internal/metadata/generated_telemetry_test.go index ce46fec06769..65e6db21aa00 100644 --- a/processor/filterprocessor/internal/metadata/generated_telemetry_test.go +++ b/processor/filterprocessor/internal/metadata/generated_telemetry_test.go @@ -61,3 +61,16 @@ func TestProviders(t *testing.T) { require.Fail(t, "returned Meter not mockTracer") } } + +func TestNewTelemetryBuilder(t *testing.T) { + set := component.TelemetrySettings{ + MeterProvider: mockMeterProvider{}, + TracerProvider: mockTracerProvider{}, + } + applied := false + _, err := NewTelemetryBuilder(set, func(b *TelemetryBuilder) { + applied = true + }) + require.NoError(t, err) + require.True(t, applied) +} diff --git a/processor/filterprocessor/metadata.yaml b/processor/filterprocessor/metadata.yaml index 9f990cd5fa27..516979b85d34 100644 --- a/processor/filterprocessor/metadata.yaml +++ b/processor/filterprocessor/metadata.yaml @@ -11,3 +11,27 @@ status: active: [TylerHelmuth, boostchicken] tests: config: + +telemetry: + metrics: + processor_filter_datapoints.filtered: + enabled: true + description: Number of metric data points dropped by the filter processor + unit: 1 + sum: + value_type: int + monotonic: true + processor_filter_logs.filtered: + enabled: true + description: Number of logs dropped by the filter processor + unit: 1 + sum: + value_type: int + monotonic: true + processor_filter_spans.filtered: + enabled: true + description: Number of spans dropped by the filter processor + unit: 1 + sum: + value_type: int + monotonic: true diff --git a/processor/filterprocessor/telemetry.go b/processor/filterprocessor/telemetry.go index 9267a142475f..302bc3b1a7af 100644 --- a/processor/filterprocessor/telemetry.go +++ b/processor/filterprocessor/telemetry.go @@ -7,7 +7,6 @@ import ( "context" "go.opentelemetry.io/collector/processor" - "go.opentelemetry.io/collector/processor/processorhelper" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" @@ -27,61 +26,31 @@ type filterProcessorTelemetry struct { processorAttr []attribute.KeyValue - datapointsFiltered metric.Int64Counter - logsFiltered metric.Int64Counter - spansFiltered metric.Int64Counter + telemetryBuilder *metadata.TelemetryBuilder } func newfilterProcessorTelemetry(set processor.CreateSettings) (*filterProcessorTelemetry, error) { - processorID := set.ID.String() - - fpt := &filterProcessorTelemetry{ - processorAttr: []attribute.KeyValue{attribute.String(metadata.Type.String(), processorID)}, - exportCtx: context.Background(), - } - - counter, err := metadata.Meter(set.TelemetrySettings).Int64Counter( - processorhelper.BuildCustomMetricName(metadata.Type.String(), "datapoints.filtered"), - metric.WithDescription("Number of metric data points dropped by the filter processor"), - metric.WithUnit("1"), - ) - if err != nil { - return nil, err - } - fpt.datapointsFiltered = counter - - counter, err = metadata.Meter(set.TelemetrySettings).Int64Counter( - processorhelper.BuildCustomMetricName(metadata.Type.String(), "logs.filtered"), - metric.WithDescription("Number of logs dropped by the filter processor"), - metric.WithUnit("1"), - ) - if err != nil { - return nil, err - } - fpt.logsFiltered = counter - - counter, err = metadata.Meter(set.TelemetrySettings).Int64Counter( - processorhelper.BuildCustomMetricName(metadata.Type.String(), "spans.filtered"), - metric.WithDescription("Number of spans dropped by the filter processor"), - metric.WithUnit("1"), - ) + telemetryBuilder, err := metadata.NewTelemetryBuilder(set.TelemetrySettings) if err != nil { return nil, err } - fpt.spansFiltered = counter - return fpt, nil + return &filterProcessorTelemetry{ + processorAttr: []attribute.KeyValue{attribute.String(metadata.Type.String(), set.ID.String())}, + exportCtx: context.Background(), + telemetryBuilder: telemetryBuilder, + }, nil } func (fpt *filterProcessorTelemetry) record(trigger trigger, dropped int64) { var triggerMeasure metric.Int64Counter switch trigger { case triggerMetricDataPointsDropped: - triggerMeasure = fpt.datapointsFiltered + triggerMeasure = fpt.telemetryBuilder.ProcessorFilterDatapointsFiltered case triggerLogsDropped: - triggerMeasure = fpt.logsFiltered + triggerMeasure = fpt.telemetryBuilder.ProcessorFilterLogsFiltered case triggerSpansDropped: - triggerMeasure = fpt.spansFiltered + triggerMeasure = fpt.telemetryBuilder.ProcessorFilterSpansFiltered } triggerMeasure.Add(fpt.exportCtx, dropped, metric.WithAttributes(fpt.processorAttr...))