diff --git a/processor/processorhelper/obsreport.go b/processor/processorhelper/obsreport.go index 2e074713f87..51aa26a9a88 100644 --- a/processor/processorhelper/obsreport.go +++ b/processor/processorhelper/obsreport.go @@ -12,7 +12,6 @@ import ( "go.uber.org/zap" "go.opentelemetry.io/collector/component" - "go.opentelemetry.io/collector/config/configtelemetry" "go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics" "go.opentelemetry.io/collector/processor" "go.opentelemetry.io/collector/processor/processorhelper/internal/metadata" @@ -34,8 +33,6 @@ func BuildCustomMetricName(configType, metric string) string { // ObsReport is a helper to add observability to a processor. type ObsReport struct { - level configtelemetry.Level - logger *zap.Logger otelAttrs []attribute.KeyValue @@ -54,12 +51,11 @@ func NewObsReport(cfg ObsReportSettings) (*ObsReport, error) { } func newObsReport(cfg ObsReportSettings) (*ObsReport, error) { - telemetryBuilder, err := metadata.NewTelemetryBuilder(cfg.ProcessorCreateSettings.TelemetrySettings) + telemetryBuilder, err := metadata.NewTelemetryBuilder(cfg.ProcessorCreateSettings.TelemetrySettings, metadata.WithLevel(cfg.ProcessorCreateSettings.MetricsLevel)) if err != nil { return nil, err } return &ObsReport{ - level: cfg.ProcessorCreateSettings.MetricsLevel, logger: cfg.ProcessorCreateSettings.Logger, otelAttrs: []attribute.KeyValue{ attribute.String(obsmetrics.ProcessorKey, cfg.ProcessorID.String()), @@ -92,63 +88,45 @@ func (or *ObsReport) recordData(ctx context.Context, dataType component.DataType // TracesAccepted reports that the trace data was accepted. func (or *ObsReport) TracesAccepted(ctx context.Context, numSpans int) { - if or.level != configtelemetry.LevelNone { - or.recordData(ctx, component.DataTypeTraces, int64(numSpans), int64(0), int64(0)) - } + or.recordData(ctx, component.DataTypeTraces, int64(numSpans), int64(0), int64(0)) } // TracesRefused reports that the trace data was refused. func (or *ObsReport) TracesRefused(ctx context.Context, numSpans int) { - if or.level != configtelemetry.LevelNone { - or.recordData(ctx, component.DataTypeTraces, int64(0), int64(numSpans), int64(0)) - } + or.recordData(ctx, component.DataTypeTraces, int64(0), int64(numSpans), int64(0)) } // TracesDropped reports that the trace data was dropped. func (or *ObsReport) TracesDropped(ctx context.Context, numSpans int) { - if or.level != configtelemetry.LevelNone { - or.recordData(ctx, component.DataTypeTraces, int64(0), int64(0), int64(numSpans)) - } + or.recordData(ctx, component.DataTypeTraces, int64(0), int64(0), int64(numSpans)) } // MetricsAccepted reports that the metrics were accepted. func (or *ObsReport) MetricsAccepted(ctx context.Context, numPoints int) { - if or.level != configtelemetry.LevelNone { - or.recordData(ctx, component.DataTypeMetrics, int64(numPoints), int64(0), int64(0)) - } + or.recordData(ctx, component.DataTypeMetrics, int64(numPoints), int64(0), int64(0)) } // MetricsRefused reports that the metrics were refused. func (or *ObsReport) MetricsRefused(ctx context.Context, numPoints int) { - if or.level != configtelemetry.LevelNone { - or.recordData(ctx, component.DataTypeMetrics, int64(0), int64(numPoints), int64(0)) - } + or.recordData(ctx, component.DataTypeMetrics, int64(0), int64(numPoints), int64(0)) } // MetricsDropped reports that the metrics were dropped. func (or *ObsReport) MetricsDropped(ctx context.Context, numPoints int) { - if or.level != configtelemetry.LevelNone { - or.recordData(ctx, component.DataTypeMetrics, int64(0), int64(0), int64(numPoints)) - } + or.recordData(ctx, component.DataTypeMetrics, int64(0), int64(0), int64(numPoints)) } // LogsAccepted reports that the logs were accepted. func (or *ObsReport) LogsAccepted(ctx context.Context, numRecords int) { - if or.level != configtelemetry.LevelNone { - or.recordData(ctx, component.DataTypeLogs, int64(numRecords), int64(0), int64(0)) - } + or.recordData(ctx, component.DataTypeLogs, int64(numRecords), int64(0), int64(0)) } // LogsRefused reports that the logs were refused. func (or *ObsReport) LogsRefused(ctx context.Context, numRecords int) { - if or.level != configtelemetry.LevelNone { - or.recordData(ctx, component.DataTypeLogs, int64(0), int64(numRecords), int64(0)) - } + or.recordData(ctx, component.DataTypeLogs, int64(0), int64(numRecords), int64(0)) } // LogsDropped reports that the logs were dropped. func (or *ObsReport) LogsDropped(ctx context.Context, numRecords int) { - if or.level != configtelemetry.LevelNone { - or.recordData(ctx, component.DataTypeLogs, int64(0), int64(0), int64(numRecords)) - } + or.recordData(ctx, component.DataTypeLogs, int64(0), int64(0), int64(numRecords)) } diff --git a/processor/processorhelper/obsreport_test.go b/processor/processorhelper/obsreport_test.go index f9df6456e08..d9313234874 100644 --- a/processor/processorhelper/obsreport_test.go +++ b/processor/processorhelper/obsreport_test.go @@ -12,6 +12,7 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenttest" + "go.opentelemetry.io/collector/config/configtelemetry" "go.opentelemetry.io/collector/processor" ) @@ -172,6 +173,67 @@ func TestCheckProcessorLogViews(t *testing.T) { assert.Error(t, tt.CheckProcessorLogs(0, 0, 9)) } +func TestNoMetrics(t *testing.T) { + // ensure if LevelNone is configured, no metrics are emitted by the component + testTelemetry(t, processorID, func(t *testing.T, tt componenttest.TestTelemetry) { + const accepted = 29 + const refused = 11 + const dropped = 17 + set := tt.TelemetrySettings() + set.MetricsLevel = configtelemetry.LevelNone + + por, err := NewObsReport(ObsReportSettings{ + ProcessorID: processorID, + ProcessorCreateSettings: processor.CreateSettings{ID: processorID, TelemetrySettings: set, BuildInfo: component.NewDefaultBuildInfo()}, + }) + assert.NoError(t, err) + + por.TracesAccepted(context.Background(), accepted) + por.TracesRefused(context.Background(), refused) + por.TracesDropped(context.Background(), dropped) + + require.Error(t, tt.CheckProcessorTraces(accepted, refused, dropped)) + }) + testTelemetry(t, processorID, func(t *testing.T, tt componenttest.TestTelemetry) { + const accepted = 29 + const refused = 11 + const dropped = 17 + set := tt.TelemetrySettings() + set.MetricsLevel = configtelemetry.LevelNone + + por, err := NewObsReport(ObsReportSettings{ + ProcessorID: processorID, + ProcessorCreateSettings: processor.CreateSettings{ID: processorID, TelemetrySettings: set, BuildInfo: component.NewDefaultBuildInfo()}, + }) + assert.NoError(t, err) + + por.MetricsAccepted(context.Background(), accepted) + por.MetricsRefused(context.Background(), refused) + por.MetricsDropped(context.Background(), dropped) + + require.Error(t, tt.CheckProcessorMetrics(accepted, refused, dropped)) + }) + testTelemetry(t, processorID, func(t *testing.T, tt componenttest.TestTelemetry) { + const accepted = 29 + const refused = 11 + const dropped = 17 + set := tt.TelemetrySettings() + set.MetricsLevel = configtelemetry.LevelNone + + por, err := NewObsReport(ObsReportSettings{ + ProcessorID: processorID, + ProcessorCreateSettings: processor.CreateSettings{ID: processorID, TelemetrySettings: set, BuildInfo: component.NewDefaultBuildInfo()}, + }) + assert.NoError(t, err) + + por.LogsAccepted(context.Background(), accepted) + por.LogsRefused(context.Background(), refused) + por.LogsDropped(context.Background(), dropped) + + require.Error(t, tt.CheckProcessorLogs(accepted, refused, dropped)) + }) +} + func testTelemetry(t *testing.T, id component.ID, testFunc func(t *testing.T, tt componenttest.TestTelemetry)) { tt, err := componenttest.SetupTelemetry(id) require.NoError(t, err)