Skip to content

Commit

Permalink
[processorhelper] use mdatagen level and noop meter (open-telemetry#1…
Browse files Browse the repository at this point in the history
…0235)

This cleans up the need to have a level in the ObsReport struct. Added
test to validate the change as none existed before

---------

Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
codeboten authored and steves-canva committed Jun 13, 2024
1 parent ab632d5 commit 1b02911
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 32 deletions.
42 changes: 10 additions & 32 deletions processor/processorhelper/obsreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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()),
Expand Down Expand Up @@ -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))
}
62 changes: 62 additions & 0 deletions processor/processorhelper/obsreport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1b02911

Please sign in to comment.