diff --git a/.chloggen/deprecate-checkreceivermetrics.yaml b/.chloggen/deprecate-checkreceivermetrics.yaml new file mode 100644 index 00000000000..5895a57377f --- /dev/null +++ b/.chloggen/deprecate-checkreceivermetrics.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: deprecation + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: componenttest + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Deprecate CheckReceiverMetrics in componenenttest + +# One or more tracking issues or pull requests related to the change +issues: [12120] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: Use `metadatatest.AssertMetrics` instead of `obsreporttest.CheckReceiverMetrics` + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] diff --git a/component/componenttest/obsreporttest.go b/component/componenttest/obsreporttest.go index a0eefcda1af..2f1e10582bc 100644 --- a/component/componenttest/obsreporttest.go +++ b/component/componenttest/obsreporttest.go @@ -81,6 +81,7 @@ func (tts *TestTelemetry) CheckReceiverLogs(protocol string, acceptedLogRecords, return checkReceiverLogs(tts.reader, tts.id, protocol, acceptedLogRecords, droppedLogRecords) } +// Deprecated: [v0.118.0] use metadatatest.AssertMetrics instead. // CheckReceiverMetrics checks that for the current exported values for metrics receiver metrics match given values. func (tts *TestTelemetry) CheckReceiverMetrics(protocol string, acceptedMetricPoints, droppedMetricPoints int64) error { return checkReceiverMetrics(tts.reader, tts.id, protocol, acceptedMetricPoints, droppedMetricPoints) diff --git a/receiver/receiverhelper/obsreport_test.go b/receiver/receiverhelper/obsreport_test.go index b6ae6da7b68..8de4315f049 100644 --- a/receiver/receiverhelper/obsreport_test.go +++ b/receiver/receiverhelper/obsreport_test.go @@ -12,16 +12,21 @@ import ( "github.com/stretchr/testify/require" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" + "go.opentelemetry.io/otel/sdk/metric/metricdata" + "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/receiver" "go.opentelemetry.io/collector/receiver/receiverhelper/internal" + "go.opentelemetry.io/collector/receiver/receiverhelper/internal/metadatatest" ) const ( - transport = "fakeTransport" - format = "fakeFormat" + receiverKey = "receiver" + transportKey = "transport" + transport = "fakeTransport" + format = "fakeFormat" ) var ( @@ -131,8 +136,9 @@ func TestReceiveLogsOp(t *testing.T) { } func TestReceiveMetricsOp(t *testing.T) { - testTelemetry(t, receiverID, func(t *testing.T, tt componenttest.TestTelemetry) { - parentCtx, parentSpan := tt.TelemetrySettings().TracerProvider.Tracer("test").Start(context.Background(), t.Name()) + testMetadataTelemetry(t, func(t *testing.T, tt metadatatest.Telemetry) { + tel := tt.NewTelemetrySettings() + parentCtx, parentSpan := tel.TracerProvider.Tracer("test").Start(context.Background(), t.Name()) defer parentSpan.End() params := []testParams{ @@ -143,7 +149,7 @@ func TestReceiveMetricsOp(t *testing.T) { rec, err := newReceiver(ObsReportSettings{ ReceiverID: receiverID, Transport: transport, - ReceiverCreateSettings: receiver.Settings{ID: receiverID, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()}, + ReceiverCreateSettings: receiver.Settings{ID: receiverID, TelemetrySettings: tel, BuildInfo: component.NewDefaultBuildInfo()}, }) require.NoError(t, err) @@ -175,7 +181,7 @@ func TestReceiveMetricsOp(t *testing.T) { } } - require.NoError(t, tt.CheckReceiverMetrics(transport, int64(acceptedMetricPoints), int64(refusedMetricPoints))) + assertMetrics(t, tt, receiverID, transport, int64(acceptedMetricPoints), int64(refusedMetricPoints)) }) } @@ -255,24 +261,20 @@ func TestCheckReceiverTracesViews(t *testing.T) { } func TestCheckReceiverMetricsViews(t *testing.T) { - tt, err := componenttest.SetupTelemetry(receiverID) - require.NoError(t, err) + tt := metadatatest.SetupTelemetry() t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) }) rec, err := NewObsReport(ObsReportSettings{ ReceiverID: receiverID, Transport: transport, - ReceiverCreateSettings: receiver.Settings{ID: receiverID, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()}, + ReceiverCreateSettings: receiver.Settings{ID: receiverID, TelemetrySettings: tt.NewTelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()}, }) require.NoError(t, err) ctx := rec.StartMetricsOp(context.Background()) require.NotNil(t, ctx) rec.EndMetricsOp(ctx, format, 7, nil) - require.NoError(t, tt.CheckReceiverMetrics(transport, 7, 0)) - require.Error(t, tt.CheckReceiverMetrics(transport, 7, 7)) - require.Error(t, tt.CheckReceiverMetrics(transport, 0, 0)) - assert.Error(t, tt.CheckReceiverMetrics(transport, 0, 7)) + assertMetrics(t, tt, receiverID, transport, 7, 0) } func TestCheckReceiverLogsViews(t *testing.T) { @@ -303,3 +305,48 @@ func testTelemetry(t *testing.T, id component.ID, testFunc func(t *testing.T, tt testFunc(t, tt) } + +func testMetadataTelemetry(t *testing.T, testFunc func(t *testing.T, tt metadatatest.Telemetry)) { + tt := metadatatest.SetupTelemetry() + t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) }) + testFunc(t, tt) +} + +func assertMetrics(t *testing.T, tt metadatatest.Telemetry, receiver component.ID, transport string, expectedAccepted int64, expectedRefused int64) { + tt.AssertMetrics(t, []metricdata.Metrics{ + { + Name: "otelcol_receiver_accepted_metric_points", + Description: "Number of metric points successfully pushed into the pipeline. [alpha]", + Unit: "{datapoints}", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: true, + DataPoints: []metricdata.DataPoint[int64]{ + { + Attributes: attribute.NewSet( + attribute.String(receiverKey, receiver.String()), + attribute.String(transportKey, transport)), + Value: expectedAccepted, + }, + }, + }, + }, + { + Name: "otelcol_receiver_refused_metric_points", + Description: "Number of metric points that could not be pushed into the pipeline. [alpha]", + Unit: "{datapoints}", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: true, + DataPoints: []metricdata.DataPoint[int64]{ + { + Attributes: attribute.NewSet( + attribute.String(receiverKey, receiver.String()), + attribute.String(transportKey, transport)), + Value: expectedRefused, + }, + }, + }, + }, + }, metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreExemplars()) +}