diff --git a/exporter/exporterhelper/logshelper_test.go b/exporter/exporterhelper/logshelper_test.go index 97dd44f9a13e..4bbe80af717f 100644 --- a/exporter/exporterhelper/logshelper_test.go +++ b/exporter/exporterhelper/logshelper_test.go @@ -26,9 +26,8 @@ import ( "go.opentelemetry.io/collector/config/configmodels" "go.opentelemetry.io/collector/internal/data" "go.opentelemetry.io/collector/internal/data/testdata" - "go.opentelemetry.io/collector/observability" - "go.opentelemetry.io/collector/observability/observabilitytest" "go.opentelemetry.io/collector/obsreport" + "go.opentelemetry.io/collector/obsreport/obsreporttest" ) const ( @@ -153,20 +152,20 @@ func newPushLogsData(droppedTimeSeries int, retError error) PushLogsData { } func checkRecordedMetricsForLogsExporter(t *testing.T, me component.LogExporter, wantError error, droppedTimeSeries int) { - doneFn := observabilitytest.SetupRecordedMetricsTest() + doneFn := obsreporttest.SetupRecordedMetricsTest() defer doneFn() ld := testdata.GenerateLogDataTwoLogsSameResource() - ctx := observability.ContextWithReceiverName(context.Background(), fakeLogsReceiverName) + ctx := obsreport.LegacyContextWithReceiverName(context.Background(), fakeLogsReceiverName) const numBatches = 7 for i := 0; i < numBatches; i++ { require.Equal(t, wantError, me.ConsumeLogs(ctx, ld)) } - err := observabilitytest.CheckValueViewExporterReceivedLogRecords(fakeLogsReceiverName, fakeLogsExporterName, numBatches*ld.LogRecordCount()) + err := obsreporttest.CheckValueViewExporterReceivedLogRecords(fakeLogsReceiverName, fakeLogsExporterName, numBatches*ld.LogRecordCount()) require.Nilf(t, err, "CheckValueViewExporterTimeSeries: Want nil Got %v", err) - err = observabilitytest.CheckValueViewExporterDroppedLogRecords(fakeLogsReceiverName, fakeLogsExporterName, numBatches*droppedTimeSeries) + err = obsreporttest.CheckValueViewExporterDroppedLogRecords(fakeLogsReceiverName, fakeLogsExporterName, numBatches*droppedTimeSeries) require.Nilf(t, err, "CheckValueViewExporterTimeSeries: Want nil Got %v", err) } diff --git a/exporter/exporterhelper/metricshelper_test.go b/exporter/exporterhelper/metricshelper_test.go index a66ef1d9923a..40067e7a4cb2 100644 --- a/exporter/exporterhelper/metricshelper_test.go +++ b/exporter/exporterhelper/metricshelper_test.go @@ -29,9 +29,8 @@ import ( "go.opentelemetry.io/collector/consumer/pdata" "go.opentelemetry.io/collector/consumer/pdatautil" "go.opentelemetry.io/collector/internal/data/testdata" - "go.opentelemetry.io/collector/observability" - "go.opentelemetry.io/collector/observability/observabilitytest" "go.opentelemetry.io/collector/obsreport" + "go.opentelemetry.io/collector/obsreport/obsreporttest" ) const ( @@ -257,20 +256,20 @@ func newPushMetricsData(droppedTimeSeries int, retError error) PushMetricsData { } func checkRecordedMetricsForMetricsExporter(t *testing.T, me component.MetricsExporter, wantError error, droppedTimeSeries int) { - doneFn := observabilitytest.SetupRecordedMetricsTest() + doneFn := obsreporttest.SetupRecordedMetricsTest() defer doneFn() md := testdata.GenerateMetricDataTwoMetrics() - ctx := observability.ContextWithReceiverName(context.Background(), fakeMetricsReceiverName) + ctx := obsreport.LegacyContextWithReceiverName(context.Background(), fakeMetricsReceiverName) const numBatches = 7 for i := 0; i < numBatches; i++ { require.Equal(t, wantError, me.ConsumeMetrics(ctx, pdatautil.MetricsFromInternalMetrics(md))) } - err := observabilitytest.CheckValueViewExporterReceivedTimeSeries(fakeMetricsReceiverName, fakeMetricsExporterName, numBatches*md.MetricCount()) + err := obsreporttest.CheckValueViewExporterReceivedTimeSeries(fakeMetricsReceiverName, fakeMetricsExporterName, numBatches*md.MetricCount()) require.Nilf(t, err, "CheckValueViewExporterTimeSeries: Want nil Got %v", err) - err = observabilitytest.CheckValueViewExporterDroppedTimeSeries(fakeMetricsReceiverName, fakeMetricsExporterName, numBatches*droppedTimeSeries) + err = obsreporttest.CheckValueViewExporterDroppedTimeSeries(fakeMetricsReceiverName, fakeMetricsExporterName, numBatches*droppedTimeSeries) require.Nilf(t, err, "CheckValueViewExporterTimeSeries: Want nil Got %v", err) } @@ -324,7 +323,7 @@ func newPushMetricsDataOld(droppedTimeSeries int, retError error) PushMetricsDat } func checkRecordedMetricsForMetricsExporterOld(t *testing.T, me component.MetricsExporterOld, wantError error, droppedTimeSeries int) { - doneFn := observabilitytest.SetupRecordedMetricsTest() + doneFn := obsreporttest.SetupRecordedMetricsTest() defer doneFn() metrics := []*metricspb.Metric{ { @@ -335,16 +334,16 @@ func checkRecordedMetricsForMetricsExporterOld(t *testing.T, me component.Metric }, } md := consumerdata.MetricsData{Metrics: metrics} - ctx := observability.ContextWithReceiverName(context.Background(), fakeMetricsReceiverName) + ctx := obsreport.LegacyContextWithReceiverName(context.Background(), fakeMetricsReceiverName) const numBatches = 7 for i := 0; i < numBatches; i++ { require.Equal(t, wantError, me.ConsumeMetricsData(ctx, md)) } - err := observabilitytest.CheckValueViewExporterReceivedTimeSeries(fakeMetricsReceiverName, fakeMetricsExporterName, numBatches*NumTimeSeries(md)) + err := obsreporttest.CheckValueViewExporterReceivedTimeSeries(fakeMetricsReceiverName, fakeMetricsExporterName, numBatches*NumTimeSeries(md)) require.Nilf(t, err, "CheckValueViewExporterTimeSeries: Want nil Got %v", err) - err = observabilitytest.CheckValueViewExporterDroppedTimeSeries(fakeMetricsReceiverName, fakeMetricsExporterName, numBatches*droppedTimeSeries) + err = obsreporttest.CheckValueViewExporterDroppedTimeSeries(fakeMetricsReceiverName, fakeMetricsExporterName, numBatches*droppedTimeSeries) require.Nilf(t, err, "CheckValueViewExporterTimeSeries: Want nil Got %v", err) } diff --git a/exporter/exporterhelper/tracehelper_test.go b/exporter/exporterhelper/tracehelper_test.go index b380a68ecb00..ade9c423930c 100644 --- a/exporter/exporterhelper/tracehelper_test.go +++ b/exporter/exporterhelper/tracehelper_test.go @@ -28,9 +28,8 @@ import ( "go.opentelemetry.io/collector/config/configmodels" "go.opentelemetry.io/collector/consumer/consumerdata" "go.opentelemetry.io/collector/consumer/pdata" - "go.opentelemetry.io/collector/observability" - "go.opentelemetry.io/collector/observability/observabilitytest" "go.opentelemetry.io/collector/obsreport" + "go.opentelemetry.io/collector/obsreport/obsreporttest" ) const ( @@ -162,21 +161,21 @@ func newTraceDataPusherOld(droppedSpans int, retError error) traceDataPusherOld } func checkRecordedMetricsForTraceExporterOld(t *testing.T, te component.TraceExporterOld, wantError error, droppedSpans int) { - doneFn := observabilitytest.SetupRecordedMetricsTest() + doneFn := obsreporttest.SetupRecordedMetricsTest() defer doneFn() spans := make([]*tracepb.Span, 2) td := consumerdata.TraceData{Spans: spans} - ctx := observability.ContextWithReceiverName(context.Background(), fakeTraceReceiverName) + ctx := obsreport.LegacyContextWithReceiverName(context.Background(), fakeTraceReceiverName) const numBatches = 7 for i := 0; i < numBatches; i++ { require.Equal(t, wantError, te.ConsumeTraceData(ctx, td)) } - err := observabilitytest.CheckValueViewExporterReceivedSpans(fakeTraceReceiverName, fakeTraceExporterName, numBatches*len(spans)) + err := obsreporttest.CheckValueViewExporterReceivedSpans(fakeTraceReceiverName, fakeTraceExporterName, numBatches*len(spans)) require.Nilf(t, err, "CheckValueViewExporterReceivedSpans: Want nil Got %v", err) - err = observabilitytest.CheckValueViewExporterDroppedSpans(fakeTraceReceiverName, fakeTraceExporterName, numBatches*droppedSpans) + err = obsreporttest.CheckValueViewExporterDroppedSpans(fakeTraceReceiverName, fakeTraceExporterName, numBatches*droppedSpans) require.Nilf(t, err, "CheckValueViewExporterDroppedSpans: Want nil Got %v", err) } @@ -349,7 +348,7 @@ func newTraceDataPusher(droppedSpans int, retError error) traceDataPusher { } func checkRecordedMetricsForTraceExporter(t *testing.T, te component.TraceExporter, wantError error, droppedSpans int) { - doneFn := observabilitytest.SetupRecordedMetricsTest() + doneFn := obsreporttest.SetupRecordedMetricsTest() defer doneFn() const spansLen = 2 @@ -358,16 +357,16 @@ func checkRecordedMetricsForTraceExporter(t *testing.T, te component.TraceExport rs.Resize(1) rs.At(0).InstrumentationLibrarySpans().Resize(1) rs.At(0).InstrumentationLibrarySpans().At(0).Spans().Resize(spansLen) - ctx := observability.ContextWithReceiverName(context.Background(), fakeTraceReceiverName) + ctx := obsreport.LegacyContextWithReceiverName(context.Background(), fakeTraceReceiverName) const numBatches = 7 for i := 0; i < numBatches; i++ { require.Equal(t, wantError, te.ConsumeTraces(ctx, td)) } - err := observabilitytest.CheckValueViewExporterReceivedSpans(fakeTraceReceiverName, fakeTraceExporterName, numBatches*spansLen) + err := obsreporttest.CheckValueViewExporterReceivedSpans(fakeTraceReceiverName, fakeTraceExporterName, numBatches*spansLen) require.Nilf(t, err, "CheckValueViewExporterReceivedSpans: Want nil Got %v", err) - err = observabilitytest.CheckValueViewExporterDroppedSpans(fakeTraceReceiverName, fakeTraceExporterName, numBatches*droppedSpans) + err = obsreporttest.CheckValueViewExporterDroppedSpans(fakeTraceReceiverName, fakeTraceExporterName, numBatches*droppedSpans) require.Nilf(t, err, "CheckValueViewExporterDroppedSpans: Want nil Got %v", err) } diff --git a/exporter/otlpexporter/otlp_test.go b/exporter/otlpexporter/otlp_test.go index 9ac9d6ee863f..0a9f40b5fd43 100644 --- a/exporter/otlpexporter/otlp_test.go +++ b/exporter/otlpexporter/otlp_test.go @@ -35,7 +35,7 @@ import ( otlptracecol "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/trace/v1" otlplogs "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/logs/v1" "go.opentelemetry.io/collector/internal/data/testdata" - "go.opentelemetry.io/collector/observability" + "go.opentelemetry.io/collector/obsreport" "go.opentelemetry.io/collector/testutil" ) @@ -68,7 +68,7 @@ func otlpTraceReceiverOnGRPCServer(ln net.Listener) *mockTraceReceiver { rcv := &mockTraceReceiver{} // Now run it as a gRPC server - rcv.srv = observability.GRPCServerWithObservabilityEnabled() + rcv.srv = obsreport.GRPCServerWithObservabilityEnabled() otlptracecol.RegisterTraceServiceServer(rcv.srv, rcv) go func() { _ = rcv.srv.Serve(ln) @@ -99,7 +99,7 @@ func otlpLogsReceiverOnGRPCServer(ln net.Listener) *mockLogsReceiver { rcv := &mockLogsReceiver{} // Now run it as a gRPC server - rcv.srv = observability.GRPCServerWithObservabilityEnabled() + rcv.srv = obsreport.GRPCServerWithObservabilityEnabled() otlplogs.RegisterLogServiceServer(rcv.srv, rcv) go func() { _ = rcv.srv.Serve(ln) diff --git a/observability/observability_test.go b/observability/observability_test.go deleted file mode 100644 index 7a39eb045dc7..000000000000 --- a/observability/observability_test.go +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// observability_test instead of just observability to avoid dependency cycle between -// observability and observabilitytest -package observability_test - -import ( - "context" - "testing" - - "github.com/stretchr/testify/require" - - "go.opentelemetry.io/collector/observability" - "go.opentelemetry.io/collector/observability/observabilitytest" -) - -const ( - receiverName = "fake_receiver" - exporterName = "fake_exporter" -) - -func TestTracePieplineRecordedMetrics(t *testing.T) { - doneFn := observabilitytest.SetupRecordedMetricsTest() - defer doneFn() - - receiverCtx := observability.ContextWithReceiverName(context.Background(), receiverName) - observability.RecordMetricsForTraceReceiver(receiverCtx, 17, 13) - exporterCtx := observability.ContextWithExporterName(receiverCtx, exporterName) - observability.RecordMetricsForTraceExporter(exporterCtx, 27, 23) - - err := observabilitytest.CheckValueViewReceiverReceivedSpans(receiverName, 17) - require.Nil(t, err, "When check receiver received spans") - - err = observabilitytest.CheckValueViewReceiverDroppedSpans(receiverName, 13) - require.Nil(t, err, "When check receiver dropped spans") - - err = observabilitytest.CheckValueViewExporterReceivedSpans(receiverName, exporterName, 27) - require.Nil(t, err, "When check exporter received spans") - - err = observabilitytest.CheckValueViewExporterDroppedSpans(receiverName, exporterName, 23) - require.Nil(t, err, "When check exporter dropped spans") -} - -func TestMetricsPieplineRecordedMetrics(t *testing.T) { - doneFn := observabilitytest.SetupRecordedMetricsTest() - defer doneFn() - - receiverCtx := observability.ContextWithReceiverName(context.Background(), receiverName) - observability.RecordMetricsForMetricsReceiver(receiverCtx, 17, 13) - exporterCtx := observability.ContextWithExporterName(receiverCtx, exporterName) - observability.RecordMetricsForMetricsExporter(exporterCtx, 27, 23) - - err := observabilitytest.CheckValueViewReceiverReceivedTimeSeries(receiverName, 17) - require.Nil(t, err, "When check receiver received timeseries") - - err = observabilitytest.CheckValueViewReceiverDroppedTimeSeries(receiverName, 13) - require.Nil(t, err, "When check receiver dropped timeseries") - - err = observabilitytest.CheckValueViewExporterReceivedTimeSeries(receiverName, exporterName, 27) - require.Nil(t, err, "When check exporter received timeseries") - - err = observabilitytest.CheckValueViewExporterDroppedTimeSeries(receiverName, exporterName, 23) - require.Nil(t, err, "When check exporter dropped timeseries") -} diff --git a/observability/observabilitytest/observabilitytest_test.go b/observability/observabilitytest/observabilitytest_test.go deleted file mode 100644 index af7a7be55577..000000000000 --- a/observability/observabilitytest/observabilitytest_test.go +++ /dev/null @@ -1,142 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// observabilitytest_test instead of just observabilitytest to avoid dependency cycle between -// observabilitytest and other test packages. -package observabilitytest_test - -import ( - "context" - "testing" - - "github.com/stretchr/testify/require" - - "go.opentelemetry.io/collector/observability" - "go.opentelemetry.io/collector/observability/observabilitytest" -) - -const ( - receiverName = "fake_receiver" - exporterName = "fake_exporter" -) - -func TestCheckValueViewTraceReceiverViews(t *testing.T) { - doneFn := observabilitytest.SetupRecordedMetricsTest() - defer doneFn() - - receiverCtx := observability.ContextWithReceiverName(context.Background(), receiverName) - observability.RecordMetricsForTraceReceiver(receiverCtx, 17, 13) - // Test expected values. - err := observabilitytest.CheckValueViewReceiverReceivedSpans(receiverName, 17) - require.Nil(t, err, "When check receiver received spans") - err = observabilitytest.CheckValueViewReceiverDroppedSpans(receiverName, 13) - require.Nil(t, err, "When check receiver dropped spans") - - // Test unexpected tag values - err = observabilitytest.CheckValueViewReceiverReceivedSpans(exporterName, 17) - require.NotNil(t, err, "When check for unexpected tag value") - err = observabilitytest.CheckValueViewReceiverDroppedSpans(exporterName, 13) - require.NotNil(t, err, "When check for unexpected tag value") - - // Test unexpected recorded values - err = observabilitytest.CheckValueViewReceiverReceivedSpans(receiverName, 13) - require.NotNil(t, err, "When check for unexpected value") - err = observabilitytest.CheckValueViewReceiverDroppedSpans(receiverName, 17) - require.NotNil(t, err, "When check for unexpected value") -} - -func TestCheckValueViewMetricsReceiverViews(t *testing.T) { - doneFn := observabilitytest.SetupRecordedMetricsTest() - defer doneFn() - - receiverCtx := observability.ContextWithReceiverName(context.Background(), receiverName) - observability.RecordMetricsForMetricsReceiver(receiverCtx, 17, 13) - // Test expected values. - err := observabilitytest.CheckValueViewReceiverReceivedTimeSeries(receiverName, 17) - require.Nil(t, err, "When check receiver received timeseries") - err = observabilitytest.CheckValueViewReceiverDroppedTimeSeries(receiverName, 13) - require.Nil(t, err, "When check receiver dropped timeseries") - - // Test unexpected tag values - err = observabilitytest.CheckValueViewReceiverReceivedTimeSeries(exporterName, 17) - require.NotNil(t, err, "When check for unexpected tag value") - err = observabilitytest.CheckValueViewReceiverDroppedTimeSeries(exporterName, 13) - require.NotNil(t, err, "When check for unexpected tag value") - - // Test unexpected recorded values - err = observabilitytest.CheckValueViewReceiverReceivedTimeSeries(receiverName, 13) - require.NotNil(t, err, "When check for unexpected value") - err = observabilitytest.CheckValueViewReceiverDroppedTimeSeries(receiverName, 17) - require.NotNil(t, err, "When check for unexpected value") -} - -func TestCheckValueViewTraceExporterViews(t *testing.T) { - doneFn := observabilitytest.SetupRecordedMetricsTest() - defer doneFn() - - receiverCtx := observability.ContextWithReceiverName(context.Background(), receiverName) - exporterCtx := observability.ContextWithExporterName(receiverCtx, exporterName) - observability.RecordMetricsForTraceExporter(exporterCtx, 17, 13) - // Test expected values. - err := observabilitytest.CheckValueViewExporterReceivedSpans(receiverName, exporterName, 17) - require.Nil(t, err, "When check exporter received spans") - err = observabilitytest.CheckValueViewExporterDroppedSpans(receiverName, exporterName, 13) - require.Nil(t, err, "When check exporter dropped spans") - - // Test unexpected tag values - err = observabilitytest.CheckValueViewExporterReceivedSpans(receiverName, receiverName, 17) - require.NotNil(t, err, "When check for unexpected tag value") - err = observabilitytest.CheckValueViewExporterDroppedSpans(receiverName, receiverName, 13) - require.NotNil(t, err, "When check for unexpected tag value") - - // Test unexpected recorded values - err = observabilitytest.CheckValueViewExporterReceivedSpans(receiverName, exporterName, 13) - require.NotNil(t, err, "When check for unexpected value") - err = observabilitytest.CheckValueViewExporterDroppedSpans(receiverName, exporterName, 17) - require.NotNil(t, err, "When check for unexpected value") -} - -func TestCheckValueViewMetricsExporterViews(t *testing.T) { - doneFn := observabilitytest.SetupRecordedMetricsTest() - defer doneFn() - - receiverCtx := observability.ContextWithReceiverName(context.Background(), receiverName) - exporterCtx := observability.ContextWithExporterName(receiverCtx, exporterName) - observability.RecordMetricsForMetricsExporter(exporterCtx, 17, 13) - // Test expected values. - err := observabilitytest.CheckValueViewExporterReceivedTimeSeries(receiverName, exporterName, 17) - require.Nil(t, err, "When check exporter received timeseries") - err = observabilitytest.CheckValueViewExporterDroppedTimeSeries(receiverName, exporterName, 13) - require.Nil(t, err, "When check exporter dropped timeseries") - - // Test unexpected tag values - err = observabilitytest.CheckValueViewExporterReceivedTimeSeries(receiverName, receiverName, 17) - require.NotNil(t, err, "When check for unexpected tag value") - err = observabilitytest.CheckValueViewExporterDroppedTimeSeries(receiverName, receiverName, 13) - require.NotNil(t, err, "When check for unexpected tag value") - - // Test unexpected recorded values - err = observabilitytest.CheckValueViewExporterReceivedTimeSeries(receiverName, exporterName, 13) - require.NotNil(t, err, "When check for unexpected value") - err = observabilitytest.CheckValueViewExporterDroppedTimeSeries(receiverName, exporterName, 17) - require.NotNil(t, err, "When check for unexpected value") -} - -func TestNoSetupCalled(t *testing.T) { - receiverCtx := observability.ContextWithReceiverName(context.Background(), receiverName) - observability.RecordMetricsForTraceReceiver(receiverCtx, 17, 13) - // Failed to check because views are not registered. - err := observabilitytest.CheckValueViewReceiverReceivedSpans(receiverName, 17) - require.NotNil(t, err, "When check with no views registered") -} diff --git a/observability/observability.go b/obsreport/observability.go similarity index 62% rename from observability/observability.go rename to obsreport/observability.go index 9a46006ea10c..de0a6bbb9043 100644 --- a/observability/observability.go +++ b/obsreport/observability.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package observability +package obsreport // This file contains helpers that are useful to add observability // with metrics and tracing using OpenCensus to the various pieces @@ -48,8 +48,8 @@ var TagKeyReceiver, _ = tag.NewKey("otelsvc_receiver") // TagKeyExporter defines tag key for Exporter. var TagKeyExporter, _ = tag.NewKey("otelsvc_exporter") -// ViewReceiverReceivedSpans defines the view for the receiver received spans metric. -var ViewReceiverReceivedSpans = &view.View{ +// LegacyViewReceiverReceivedSpans defines the view for the receiver received spans metric. +var LegacyViewReceiverReceivedSpans = &view.View{ Name: mReceiverReceivedSpans.Name(), Description: mReceiverReceivedSpans.Description(), Measure: mReceiverReceivedSpans, @@ -57,8 +57,8 @@ var ViewReceiverReceivedSpans = &view.View{ TagKeys: []tag.Key{TagKeyReceiver}, } -// ViewReceiverDroppedSpans defines the view for the receiver dropped spans metric. -var ViewReceiverDroppedSpans = &view.View{ +// LegacyViewReceiverDroppedSpans defines the view for the receiver dropped spans metric. +var LegacyViewReceiverDroppedSpans = &view.View{ Name: mReceiverDroppedSpans.Name(), Description: mReceiverDroppedSpans.Description(), Measure: mReceiverDroppedSpans, @@ -66,8 +66,8 @@ var ViewReceiverDroppedSpans = &view.View{ TagKeys: []tag.Key{TagKeyReceiver}, } -// ViewReceiverReceivedTimeSeries defines the view for the receiver received timeseries metric. -var ViewReceiverReceivedTimeSeries = &view.View{ +// LegacyViewReceiverReceivedTimeSeries defines the view for the receiver received timeseries metric. +var LegacyViewReceiverReceivedTimeSeries = &view.View{ Name: mReceiverReceivedTimeSeries.Name(), Description: mReceiverReceivedTimeSeries.Description(), Measure: mReceiverReceivedTimeSeries, @@ -75,8 +75,8 @@ var ViewReceiverReceivedTimeSeries = &view.View{ TagKeys: []tag.Key{TagKeyReceiver}, } -// ViewReceiverDroppedTimeSeries defines the view for the receiver dropped timeseries metric. -var ViewReceiverDroppedTimeSeries = &view.View{ +// LegacyViewReceiverDroppedTimeSeries defines the view for the receiver dropped timeseries metric. +var LegacyViewReceiverDroppedTimeSeries = &view.View{ Name: mReceiverDroppedTimeSeries.Name(), Description: mReceiverDroppedTimeSeries.Description(), Measure: mReceiverDroppedTimeSeries, @@ -84,8 +84,8 @@ var ViewReceiverDroppedTimeSeries = &view.View{ TagKeys: []tag.Key{TagKeyReceiver}, } -// ViewExporterReceivedSpans defines the view for the exporter received spans metric. -var ViewExporterReceivedSpans = &view.View{ +// LegacyViewExporterReceivedSpans defines the view for the exporter received spans metric. +var LegacyViewExporterReceivedSpans = &view.View{ Name: mExporterReceivedSpans.Name(), Description: mExporterReceivedSpans.Description(), Measure: mExporterReceivedSpans, @@ -93,8 +93,8 @@ var ViewExporterReceivedSpans = &view.View{ TagKeys: []tag.Key{TagKeyReceiver, TagKeyExporter}, } -// ViewExporterDroppedSpans defines the view for the exporter dropped spans metric. -var ViewExporterDroppedSpans = &view.View{ +// LegacyViewExporterDroppedSpans defines the view for the exporter dropped spans metric. +var LegacyViewExporterDroppedSpans = &view.View{ Name: mExporterDroppedSpans.Name(), Description: mExporterDroppedSpans.Description(), Measure: mExporterDroppedSpans, @@ -102,8 +102,8 @@ var ViewExporterDroppedSpans = &view.View{ TagKeys: []tag.Key{TagKeyReceiver, TagKeyExporter}, } -// ViewExporterReceivedTimeSeries defines the view for the exporter received timeseries metric. -var ViewExporterReceivedTimeSeries = &view.View{ +// LegacyViewExporterReceivedTimeSeries defines the view for the exporter received timeseries metric. +var LegacyViewExporterReceivedTimeSeries = &view.View{ Name: mExporterReceivedTimeSeries.Name(), Description: mExporterReceivedTimeSeries.Description(), Measure: mExporterReceivedTimeSeries, @@ -111,8 +111,8 @@ var ViewExporterReceivedTimeSeries = &view.View{ TagKeys: []tag.Key{TagKeyReceiver, TagKeyExporter}, } -// ViewExporterDroppedTimeSeries defines the view for the exporter dropped timeseries metric. -var ViewExporterDroppedTimeSeries = &view.View{ +// LegacyViewExporterDroppedTimeSeries defines the view for the exporter dropped timeseries metric. +var LegacyViewExporterDroppedTimeSeries = &view.View{ Name: mExporterDroppedTimeSeries.Name(), Description: mExporterDroppedTimeSeries.Description(), Measure: mExporterDroppedTimeSeries, @@ -120,8 +120,8 @@ var ViewExporterDroppedTimeSeries = &view.View{ TagKeys: []tag.Key{TagKeyReceiver, TagKeyExporter}, } -// ViewExporterReceivedLogRecords defines the view for the exporter received logs metric. -var ViewExporterReceivedLogRecords = &view.View{ +// LegacyViewExporterReceivedLogRecords defines the view for the exporter received logs metric. +var LegacyViewExporterReceivedLogRecords = &view.View{ Name: mExporterReceivedLogRecords.Name(), Description: mExporterReceivedLogRecords.Description(), Measure: mExporterReceivedLogRecords, @@ -129,8 +129,8 @@ var ViewExporterReceivedLogRecords = &view.View{ TagKeys: []tag.Key{TagKeyReceiver, TagKeyExporter}, } -// ViewExporterDroppedLogRecords defines the view for the exporter dropped logs metric. -var ViewExporterDroppedLogRecords = &view.View{ +// LegacyViewExporterDroppedLogRecords defines the view for the exporter dropped logs metric. +var LegacyViewExporterDroppedLogRecords = &view.View{ Name: mExporterDroppedLogRecords.Name(), Description: mExporterDroppedLogRecords.Description(), Measure: mExporterDroppedLogRecords, @@ -138,63 +138,63 @@ var ViewExporterDroppedLogRecords = &view.View{ TagKeys: []tag.Key{TagKeyReceiver, TagKeyExporter}, } -// AllViews has the views for the metrics provided by the agent. -var AllViews = []*view.View{ - ViewReceiverReceivedSpans, - ViewReceiverDroppedSpans, - ViewReceiverReceivedTimeSeries, - ViewReceiverDroppedTimeSeries, - ViewExporterReceivedSpans, - ViewExporterDroppedSpans, - ViewExporterReceivedLogRecords, - ViewExporterDroppedLogRecords, - ViewExporterReceivedTimeSeries, - ViewExporterDroppedTimeSeries, +// LegacyAllViews has the views for the metrics provided by the agent. +var LegacyAllViews = []*view.View{ + LegacyViewReceiverReceivedSpans, + LegacyViewReceiverDroppedSpans, + LegacyViewReceiverReceivedTimeSeries, + LegacyViewReceiverDroppedTimeSeries, + LegacyViewExporterReceivedSpans, + LegacyViewExporterDroppedSpans, + LegacyViewExporterReceivedLogRecords, + LegacyViewExporterDroppedLogRecords, + LegacyViewExporterReceivedTimeSeries, + LegacyViewExporterDroppedTimeSeries, } -// ContextWithReceiverName adds the tag "receiver" and the name of the receiver as the value, +// LegacyContextWithReceiverName adds the tag "receiver" and the name of the receiver as the value, // and returns the newly created context. For receivers that can receive multiple signals it is // recommended to encode the signal as suffix (e.g. "oc_trace" and "oc_metrics"). -func ContextWithReceiverName(ctx context.Context, receiverName string) context.Context { +func LegacyContextWithReceiverName(ctx context.Context, receiverName string) context.Context { ctx, _ = tag.New(ctx, tag.Upsert(TagKeyReceiver, receiverName, tag.WithTTL(tag.TTLNoPropagation))) return ctx } -// RecordMetricsForTraceReceiver records the number of spans received and dropped by the receiver. -// Use it with a context.Context generated using ContextWithReceiverName(). -func RecordMetricsForTraceReceiver(ctxWithTraceReceiverName context.Context, receivedSpans int, droppedSpans int) { +// LegacyRecordMetricsForTraceReceiver records the number of spans received and dropped by the receiver. +// Use it with a context.Context generated using LegacyContextWithReceiverName(). +func LegacyRecordMetricsForTraceReceiver(ctxWithTraceReceiverName context.Context, receivedSpans int, droppedSpans int) { stats.Record(ctxWithTraceReceiverName, mReceiverReceivedSpans.M(int64(receivedSpans)), mReceiverDroppedSpans.M(int64(droppedSpans))) } -// RecordMetricsForMetricsReceiver records the number of timeseries received and dropped by the receiver. -// Use it with a context.Context generated using ContextWithReceiverName(). -func RecordMetricsForMetricsReceiver(ctxWithTraceReceiverName context.Context, receivedTimeSeries int, droppedTimeSeries int) { +// LegacyRecordMetricsForMetricsReceiver records the number of timeseries received and dropped by the receiver. +// Use it with a context.Context generated using LegacyContextWithReceiverName(). +func LegacyRecordMetricsForMetricsReceiver(ctxWithTraceReceiverName context.Context, receivedTimeSeries int, droppedTimeSeries int) { stats.Record(ctxWithTraceReceiverName, mReceiverReceivedTimeSeries.M(int64(receivedTimeSeries)), mReceiverDroppedTimeSeries.M(int64(droppedTimeSeries))) } -// ContextWithExporterName adds the tag "exporter" and the name of the exporter as the value, +// LegacyContextWithExporterName adds the tag "exporter" and the name of the exporter as the value, // and returns the newly created context. For exporters that can export multiple signals it is // recommended to encode the signal as suffix (e.g. "oc_trace" and "oc_metrics"). -func ContextWithExporterName(ctx context.Context, exporterName string) context.Context { +func LegacyContextWithExporterName(ctx context.Context, exporterName string) context.Context { ctx, _ = tag.New(ctx, tag.Upsert(TagKeyExporter, exporterName, tag.WithTTL(tag.TTLNoPropagation))) return ctx } -// RecordMetricsForTraceExporter records the number of spans received and dropped by the exporter. -// Use it with a context.Context generated using ContextWithExporterName(). -func RecordMetricsForTraceExporter(ctx context.Context, receivedSpans int, droppedSpans int) { +// LegacyRecordMetricsForTraceExporter records the number of spans received and dropped by the exporter. +// Use it with a context.Context generated using LegacyContextWithExporterName(). +func LegacyRecordMetricsForTraceExporter(ctx context.Context, receivedSpans int, droppedSpans int) { stats.Record(ctx, mExporterReceivedSpans.M(int64(receivedSpans)), mExporterDroppedSpans.M(int64(droppedSpans))) } -// RecordMetricsForMetricsExporter records the number of timeseries received and dropped by the exporter. -// Use it with a context.Context generated using ContextWithExporterName(). -func RecordMetricsForMetricsExporter(ctx context.Context, receivedTimeSeries int, droppedTimeSeries int) { +// LegacyRecordMetricsForMetricsExporter records the number of timeseries received and dropped by the exporter. +// Use it with a context.Context generated using LegacyContextWithExporterName(). +func LegacyRecordMetricsForMetricsExporter(ctx context.Context, receivedTimeSeries int, droppedTimeSeries int) { stats.Record(ctx, mExporterReceivedTimeSeries.M(int64(receivedTimeSeries)), mExporterDroppedTimeSeries.M(int64(droppedTimeSeries))) } -// RecordMetricsForLogsExporter records the number of timeseries received and dropped by the exporter. -// Use it with a context.Context generated using ContextWithExporterName(). -func RecordMetricsForLogsExporter(ctx context.Context, receivedLogs int, droppedLogs int) { +// LegacyRecordMetricsForLogsExporter records the number of timeseries received and dropped by the exporter. +// Use it with a context.Context generated using LegacyContextWithExporterName(). +func LegacyRecordMetricsForLogsExporter(ctx context.Context, receivedLogs int, droppedLogs int) { stats.Record(ctx, mExporterReceivedLogRecords.M(int64(receivedLogs)), mExporterDroppedLogRecords.M(int64(droppedLogs))) } diff --git a/obsreport/observability_test.go b/obsreport/observability_test.go new file mode 100644 index 000000000000..279323ee5320 --- /dev/null +++ b/obsreport/observability_test.go @@ -0,0 +1,76 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// obsreport_test instead of just obsreport to avoid dependency cycle between +// obsreport_test and obsreporttest +package obsreport_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + + "go.opentelemetry.io/collector/obsreport" + "go.opentelemetry.io/collector/obsreport/obsreporttest" +) + +const ( + receiverName = "fake_receiver" + exporterName = "fake_exporter" +) + +func TestTracePieplineRecordedMetrics(t *testing.T) { + doneFn := obsreporttest.SetupRecordedMetricsTest() + defer doneFn() + + receiverCtx := obsreport.LegacyContextWithReceiverName(context.Background(), receiverName) + obsreport.LegacyRecordMetricsForTraceReceiver(receiverCtx, 17, 13) + exporterCtx := obsreport.LegacyContextWithExporterName(receiverCtx, exporterName) + obsreport.LegacyRecordMetricsForTraceExporter(exporterCtx, 27, 23) + + err := obsreporttest.CheckValueViewReceiverReceivedSpans(receiverName, 17) + require.Nil(t, err, "When check receiver received spans") + + err = obsreporttest.CheckValueViewReceiverDroppedSpans(receiverName, 13) + require.Nil(t, err, "When check receiver dropped spans") + + err = obsreporttest.CheckValueViewExporterReceivedSpans(receiverName, exporterName, 27) + require.Nil(t, err, "When check exporter received spans") + + err = obsreporttest.CheckValueViewExporterDroppedSpans(receiverName, exporterName, 23) + require.Nil(t, err, "When check exporter dropped spans") +} + +func TestMetricsPieplineRecordedMetrics(t *testing.T) { + doneFn := obsreporttest.SetupRecordedMetricsTest() + defer doneFn() + + receiverCtx := obsreport.LegacyContextWithReceiverName(context.Background(), receiverName) + obsreport.LegacyRecordMetricsForMetricsReceiver(receiverCtx, 17, 13) + exporterCtx := obsreport.LegacyContextWithExporterName(receiverCtx, exporterName) + obsreport.LegacyRecordMetricsForMetricsExporter(exporterCtx, 27, 23) + + err := obsreporttest.CheckValueViewReceiverReceivedTimeSeries(receiverName, 17) + require.Nil(t, err, "When check receiver received timeseries") + + err = obsreporttest.CheckValueViewReceiverDroppedTimeSeries(receiverName, 13) + require.Nil(t, err, "When check receiver dropped timeseries") + + err = obsreporttest.CheckValueViewExporterReceivedTimeSeries(receiverName, exporterName, 27) + require.Nil(t, err, "When check exporter received timeseries") + + err = obsreporttest.CheckValueViewExporterDroppedTimeSeries(receiverName, exporterName, 23) + require.Nil(t, err, "When check exporter dropped timeseries") +} diff --git a/obsreport/obsreport.go b/obsreport/obsreport.go index 2d68dc6f3f37..f705a2581e95 100644 --- a/obsreport/obsreport.go +++ b/obsreport/obsreport.go @@ -24,7 +24,6 @@ import ( "go.opencensus.io/trace" "go.opentelemetry.io/collector/consumer/consumerdata" - "go.opentelemetry.io/collector/observability" ) const ( @@ -65,9 +64,7 @@ func setParentLink(parentCtx context.Context, childSpan *trace.Span) bool { // Configure is used to control the settings that will be used by the obsreport // package. -func Configure( - generateLegacy, generateNew bool, -) (views []*view.View) { +func Configure(generateLegacy, generateNew bool) (views []*view.View) { // TODO: expose some level control, similar to telemetry.Level @@ -75,11 +72,11 @@ func Configure( useNew = generateNew if useLegacy { - views = append(views, observability.AllViews...) + views = append(views, LegacyAllViews...) } if useNew { - views = append(views, genAllViews()...) + views = append(views, AllViews()...) } return views @@ -108,26 +105,29 @@ func buildComponentPrefix(componentPrefix, configType string) string { return componentPrefix + configType + nameSep } -func genAllViews() (views []*view.View) { +// AllViews return the list of all views that needs to be configured. +func AllViews() (views []*view.View) { // Receiver views. measures := []*stats.Int64Measure{ - mReceiverAcceptedSpans, mReceiverRefusedSpans, - mReceiverAcceptedMetricPoints, mReceiverRefusedMetricPoints, + mReceiverAcceptedSpans, + mReceiverRefusedSpans, + mReceiverAcceptedMetricPoints, + mReceiverRefusedMetricPoints, } tagKeys := []tag.Key{ tagKeyReceiver, tagKeyTransport, } - views = append(views, genViews( - measures, tagKeys, view.Sum())...) + views = append(views, genViews(measures, tagKeys, view.Sum())...) // Exporter views. measures = []*stats.Int64Measure{ - mExporterSentSpans, mExporterFailedToSendSpans, - mExporterSentMetricPoints, mExporterFailedToSendMetricPoints, + mExporterSentSpans, + mExporterFailedToSendSpans, + mExporterSentMetricPoints, + mExporterFailedToSendMetricPoints, } tagKeys = []tag.Key{tagKeyExporter} - views = append(views, genViews( - measures, tagKeys, view.Sum())...) + views = append(views, genViews(measures, tagKeys, view.Sum())...) // Processor views. measures = []*stats.Int64Measure{ @@ -139,8 +139,7 @@ func genAllViews() (views []*view.View) { mProcessorDroppedMetricPoints, } tagKeys = []tag.Key{tagKeyProcessor} - views = append(views, genViews( - measures, tagKeys, view.Sum())...) + views = append(views, genViews(measures, tagKeys, view.Sum())...) return views } diff --git a/obsreport/obsreport_exporter.go b/obsreport/obsreport_exporter.go index a796c108f67a..90eface458bb 100644 --- a/obsreport/obsreport_exporter.go +++ b/obsreport/obsreport_exporter.go @@ -22,7 +22,6 @@ import ( "go.opencensus.io/trace" "go.opentelemetry.io/collector/config/configmodels" - "go.opentelemetry.io/collector/observability" ) const ( @@ -106,7 +105,7 @@ func EndTraceDataExportOp( err error, ) { if useLegacy { - observability.RecordMetricsForTraceExporter( + LegacyRecordMetricsForTraceExporter( exporterCtx, numExportedSpans, numDroppedSpans) } @@ -141,7 +140,7 @@ func EndMetricsExportOp( err error, ) { if useLegacy { - observability.RecordMetricsForMetricsExporter( + LegacyRecordMetricsForMetricsExporter( exporterCtx, numExportedTimeSeries, numDroppedTimeSeries) } @@ -175,7 +174,7 @@ func EndLogsExportOp( err error, ) { if useLegacy { - observability.RecordMetricsForLogsExporter( + LegacyRecordMetricsForLogsExporter( exporterCtx, numExportedLogs, numDroppedLogs) } @@ -196,7 +195,7 @@ func ExporterContext( exporter string, ) context.Context { if useLegacy { - ctx = observability.ContextWithExporterName(ctx, exporter) + ctx = LegacyContextWithExporterName(ctx, exporter) } if useNew { diff --git a/obsreport/obsreport_receiver.go b/obsreport/obsreport_receiver.go index 2ae932be92e9..b927c20e54a2 100644 --- a/obsreport/obsreport_receiver.go +++ b/obsreport/obsreport_receiver.go @@ -22,7 +22,6 @@ import ( "go.opencensus.io/trace" "go.opentelemetry.io/collector/config/configmodels" - "go.opentelemetry.io/collector/observability" ) const ( @@ -160,7 +159,7 @@ func EndTraceDataReceiveOp( numDroppedSpans = numReceivedSpans numReceivedLegacy = 0 } - observability.RecordMetricsForTraceReceiver( + LegacyRecordMetricsForTraceReceiver( receiverCtx, numReceivedLegacy, numDroppedSpans) } @@ -205,7 +204,7 @@ func EndMetricsReceiveOp( numDroppedTimeSeries = numReceivedTimeSeries numReceivedTimeSeries = 0 } - observability.RecordMetricsForMetricsReceiver( + LegacyRecordMetricsForMetricsReceiver( receiverCtx, numReceivedTimeSeries, numDroppedTimeSeries) } @@ -233,7 +232,7 @@ func ReceiverContext( if legacyName != "" { name = legacyName } - ctx = observability.ContextWithReceiverName(ctx, name) + ctx = LegacyContextWithReceiverName(ctx, name) } if useNew { diff --git a/obsreport/obsreport_test.go b/obsreport/obsreport_test.go index 18c42cba93e7..9ee7663f8fb4 100644 --- a/obsreport/obsreport_test.go +++ b/obsreport/obsreport_test.go @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -package obsreport +// obsreport_test instead of just obsreport to avoid dependency cycle between +// obsreport_test and obsreporttest +package obsreport_test import ( "context" @@ -29,8 +31,8 @@ import ( "go.opencensus.io/tag" "go.opencensus.io/trace" - "go.opentelemetry.io/collector/observability" - "go.opentelemetry.io/collector/observability/observabilitytest" + "go.opentelemetry.io/collector/obsreport" + "go.opentelemetry.io/collector/obsreport/obsreporttest" ) const ( @@ -80,14 +82,14 @@ func TestConfigure(t *testing.T) { args: args{ generateLegacy: true, }, - wantViews: observability.AllViews, + wantViews: obsreport.LegacyAllViews, }, { name: "new_only", args: args{ generateNew: true, }, - wantViews: genAllViews(), + wantViews: obsreport.AllViews(), }, { name: "new_only", @@ -96,13 +98,13 @@ func TestConfigure(t *testing.T) { generateLegacy: true, }, wantViews: append( - observability.AllViews, - genAllViews()...), + obsreport.LegacyAllViews, + obsreport.AllViews()...), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotViews := Configure(tt.args.generateLegacy, tt.args.generateNew) + gotViews := obsreport.Configure(tt.args.generateLegacy, tt.args.generateNew) assert.Equal(t, tt.wantViews, gotViews) }) } @@ -110,8 +112,8 @@ func TestConfigure(t *testing.T) { func Test_obsreport_ReceiveTraceDataOp(t *testing.T) { doneFn, err := setupViews() - defer doneFn() require.NoError(t, err) + defer doneFn() ss := &spanStore{} trace.RegisterExporter(ss) @@ -121,17 +123,17 @@ func Test_obsreport_ReceiveTraceDataOp(t *testing.T) { t.Name(), trace.WithSampler(trace.AlwaysSample())) defer parentSpan.End() - receiverCtx := ReceiverContext(parentCtx, receiver, transport, legacyName) + receiverCtx := obsreport.ReceiverContext(parentCtx, receiver, transport, legacyName) params := []receiveTestParams{ {transport, errFake}, {"", nil}, } rcvdSpans := []int{13, 42} for i, param := range params { - ctx := StartTraceDataReceiveOp(receiverCtx, receiver, param.transport) + ctx := obsreport.StartTraceDataReceiveOp(receiverCtx, receiver, param.transport) assert.NotNil(t, ctx) - EndTraceDataReceiveOp( + obsreport.EndTraceDataReceiveOp( ctx, format, rcvdSpans[i], @@ -143,32 +145,32 @@ func Test_obsreport_ReceiveTraceDataOp(t *testing.T) { var acceptedSpans, refusedSpans int for i, span := range spans { - assert.Equal(t, receiverPrefix+receiver+receiveTraceDataOperationSuffix, span.Name) + assert.Equal(t, "receiver/"+receiver+"/TraceDataReceived", span.Name) switch params[i].err { case nil: acceptedSpans += rcvdSpans[i] - assert.Equal(t, int64(rcvdSpans[i]), span.Attributes[AcceptedSpansKey]) - assert.Equal(t, int64(0), span.Attributes[RefusedSpansKey]) - assert.Equal(t, okStatus, span.Status) + assert.Equal(t, int64(rcvdSpans[i]), span.Attributes[obsreport.AcceptedSpansKey]) + assert.Equal(t, int64(0), span.Attributes[obsreport.RefusedSpansKey]) + assert.Equal(t, trace.Status{Code: trace.StatusCodeOK}, span.Status) case errFake: refusedSpans += rcvdSpans[i] - assert.Equal(t, int64(0), span.Attributes[AcceptedSpansKey]) - assert.Equal(t, int64(rcvdSpans[i]), span.Attributes[RefusedSpansKey]) + assert.Equal(t, int64(0), span.Attributes[obsreport.AcceptedSpansKey]) + assert.Equal(t, int64(rcvdSpans[i]), span.Attributes[obsreport.RefusedSpansKey]) assert.Equal(t, params[i].err.Error(), span.Status.Message) default: t.Fatalf("unexpected param: %v", params[i]) } switch params[i].transport { case "": - assert.NotContains(t, span.Attributes, TransportKey) + assert.NotContains(t, span.Attributes, obsreport.TransportKey) default: - assert.Equal(t, params[i].transport, span.Attributes[TransportKey]) + assert.Equal(t, params[i].transport, span.Attributes[obsreport.TransportKey]) } } // Check legacy metrics. - assert.NoError(t, observabilitytest.CheckValueViewReceiverReceivedSpans(legacyName, acceptedSpans)) - assert.NoError(t, observabilitytest.CheckValueViewReceiverDroppedSpans(legacyName, refusedSpans)) + assert.NoError(t, obsreporttest.CheckValueViewReceiverReceivedSpans(legacyName, acceptedSpans)) + assert.NoError(t, obsreporttest.CheckValueViewReceiverDroppedSpans(legacyName, refusedSpans)) // Check new metrics. receiverTags := receiverViewTags(receiver, transport) checkValueForSumView(t, "receiver/accepted_spans", receiverTags, acceptedSpans) @@ -177,8 +179,8 @@ func Test_obsreport_ReceiveTraceDataOp(t *testing.T) { func Test_obsreport_ReceiveMetricsOp(t *testing.T) { doneFn, err := setupViews() - defer doneFn() require.NoError(t, err) + defer doneFn() ss := &spanStore{} trace.RegisterExporter(ss) @@ -188,7 +190,7 @@ func Test_obsreport_ReceiveMetricsOp(t *testing.T) { t.Name(), trace.WithSampler(trace.AlwaysSample())) defer parentSpan.End() - receiverCtx := ReceiverContext(parentCtx, receiver, transport, legacyName) + receiverCtx := obsreport.ReceiverContext(parentCtx, receiver, transport, legacyName) params := []receiveTestParams{ {transport, errFake}, {"", nil}, @@ -196,10 +198,10 @@ func Test_obsreport_ReceiveMetricsOp(t *testing.T) { rcvdMetricPts := []int{23, 29} rcvdTimeSeries := []int{2, 3} for i, param := range params { - ctx := StartMetricsReceiveOp(receiverCtx, receiver, param.transport) + ctx := obsreport.StartMetricsReceiveOp(receiverCtx, receiver, param.transport) assert.NotNil(t, ctx) - EndMetricsReceiveOp( + obsreport.EndMetricsReceiveOp( ctx, format, rcvdMetricPts[i], @@ -213,34 +215,34 @@ func Test_obsreport_ReceiveMetricsOp(t *testing.T) { var receivedTimeSeries, droppedTimeSeries int var acceptedMetricPoints, refusedMetricPoints int for i, span := range spans { - assert.Equal(t, receiverPrefix+receiver+receiverMetricsOperationSuffix, span.Name) + assert.Equal(t, "receiver/"+receiver+"/MetricsReceived", span.Name) switch params[i].err { case nil: receivedTimeSeries += rcvdTimeSeries[i] acceptedMetricPoints += rcvdMetricPts[i] - assert.Equal(t, int64(rcvdMetricPts[i]), span.Attributes[AcceptedMetricPointsKey]) - assert.Equal(t, int64(0), span.Attributes[RefusedMetricPointsKey]) - assert.Equal(t, okStatus, span.Status) + assert.Equal(t, int64(rcvdMetricPts[i]), span.Attributes[obsreport.AcceptedMetricPointsKey]) + assert.Equal(t, int64(0), span.Attributes[obsreport.RefusedMetricPointsKey]) + assert.Equal(t, trace.Status{Code: trace.StatusCodeOK}, span.Status) case errFake: droppedTimeSeries += rcvdTimeSeries[i] refusedMetricPoints += rcvdMetricPts[i] - assert.Equal(t, int64(0), span.Attributes[AcceptedMetricPointsKey]) - assert.Equal(t, int64(rcvdMetricPts[i]), span.Attributes[RefusedMetricPointsKey]) + assert.Equal(t, int64(0), span.Attributes[obsreport.AcceptedMetricPointsKey]) + assert.Equal(t, int64(rcvdMetricPts[i]), span.Attributes[obsreport.RefusedMetricPointsKey]) assert.Equal(t, params[i].err.Error(), span.Status.Message) default: t.Fatalf("unexpected param: %v", params[i]) } switch params[i].transport { case "": - assert.NotContains(t, span.Attributes, TransportKey) + assert.NotContains(t, span.Attributes, obsreport.TransportKey) default: - assert.Equal(t, params[i].transport, span.Attributes[TransportKey]) + assert.Equal(t, params[i].transport, span.Attributes[obsreport.TransportKey]) } } // Check legacy metrics. - assert.NoError(t, observabilitytest.CheckValueViewReceiverReceivedTimeSeries(legacyName, receivedTimeSeries)) - assert.NoError(t, observabilitytest.CheckValueViewReceiverDroppedTimeSeries(legacyName, droppedTimeSeries)) + assert.NoError(t, obsreporttest.CheckValueViewReceiverReceivedTimeSeries(legacyName, receivedTimeSeries)) + assert.NoError(t, obsreporttest.CheckValueViewReceiverDroppedTimeSeries(legacyName, droppedTimeSeries)) // Check new metrics. receiverTags := receiverViewTags(receiver, transport) checkValueForSumView(t, "receiver/accepted_metric_points", receiverTags, acceptedMetricPoints) @@ -249,8 +251,8 @@ func Test_obsreport_ReceiveMetricsOp(t *testing.T) { func Test_obsreport_ExportTraceDataOp(t *testing.T) { doneFn, err := setupViews() - defer doneFn() require.NoError(t, err) + defer doneFn() ss := &spanStore{} trace.RegisterExporter(ss) @@ -260,15 +262,15 @@ func Test_obsreport_ExportTraceDataOp(t *testing.T) { t.Name(), trace.WithSampler(trace.AlwaysSample())) defer parentSpan.End() - // observabilitytest for exporters expects the context to flow the original + // obsreporttest for exporters expects the context to flow the original // receiver tags, adding that to parent span. - parentCtx = observability.ContextWithReceiverName(parentCtx, receiver) + parentCtx = obsreport.LegacyContextWithReceiverName(parentCtx, receiver) - exporterCtx := ExporterContext(parentCtx, exporter) + exporterCtx := obsreport.ExporterContext(parentCtx, exporter) errs := []error{nil, errFake} numExportedSpans := []int{22, 14} for i, err := range errs { - ctx := StartTraceDataExportOp(exporterCtx, exporter) + ctx := obsreport.StartTraceDataExportOp(exporterCtx, exporter) assert.NotNil(t, ctx) var numDroppedSpans int @@ -276,7 +278,7 @@ func Test_obsreport_ExportTraceDataOp(t *testing.T) { numDroppedSpans = numExportedSpans[i] } - EndTraceDataExportOp(ctx, numExportedSpans[i], numDroppedSpans, err) + obsreport.EndTraceDataExportOp(ctx, numExportedSpans[i], numDroppedSpans, err) } spans := ss.PullAllSpans() @@ -284,17 +286,17 @@ func Test_obsreport_ExportTraceDataOp(t *testing.T) { var sentSpans, failedToSendSpans int for i, span := range spans { - assert.Equal(t, exporterPrefix+exporter+exportTraceDataOperationSuffix, span.Name) + assert.Equal(t, "exporter/"+exporter+"/TraceDataExported", span.Name) switch errs[i] { case nil: sentSpans += numExportedSpans[i] - assert.Equal(t, int64(numExportedSpans[i]), span.Attributes[SentSpansKey]) - assert.Equal(t, int64(0), span.Attributes[FailedToSendSpansKey]) - assert.Equal(t, okStatus, span.Status) + assert.Equal(t, int64(numExportedSpans[i]), span.Attributes[obsreport.SentSpansKey]) + assert.Equal(t, int64(0), span.Attributes[obsreport.FailedToSendSpansKey]) + assert.Equal(t, trace.Status{Code: trace.StatusCodeOK}, span.Status) case errFake: failedToSendSpans += numExportedSpans[i] - assert.Equal(t, int64(0), span.Attributes[SentSpansKey]) - assert.Equal(t, int64(numExportedSpans[i]), span.Attributes[FailedToSendSpansKey]) + assert.Equal(t, int64(0), span.Attributes[obsreport.SentSpansKey]) + assert.Equal(t, int64(numExportedSpans[i]), span.Attributes[obsreport.FailedToSendSpansKey]) assert.Equal(t, errs[i].Error(), span.Status.Message) default: t.Fatalf("unexpected error: %v", errs[i]) @@ -302,8 +304,8 @@ func Test_obsreport_ExportTraceDataOp(t *testing.T) { } // Check legacy metrics. - assert.NoError(t, observabilitytest.CheckValueViewExporterReceivedSpans(receiver, exporter, sentSpans+failedToSendSpans)) - assert.NoError(t, observabilitytest.CheckValueViewExporterDroppedSpans(receiver, exporter, failedToSendSpans)) + assert.NoError(t, obsreporttest.CheckValueViewExporterReceivedSpans(receiver, exporter, sentSpans+failedToSendSpans)) + assert.NoError(t, obsreporttest.CheckValueViewExporterDroppedSpans(receiver, exporter, failedToSendSpans)) // Check new metrics. exporterTags := exporterViewTags(exporter) checkValueForSumView(t, "exporter/sent_spans", exporterTags, sentSpans) @@ -312,8 +314,8 @@ func Test_obsreport_ExportTraceDataOp(t *testing.T) { func Test_obsreport_ExportMetricsOp(t *testing.T) { doneFn, err := setupViews() - defer doneFn() require.NoError(t, err) + defer doneFn() ss := &spanStore{} trace.RegisterExporter(ss) @@ -323,16 +325,16 @@ func Test_obsreport_ExportMetricsOp(t *testing.T) { t.Name(), trace.WithSampler(trace.AlwaysSample())) defer parentSpan.End() - // observabilitytest for exporters expects the context to flow the original + // obsreporttest for exporters expects the context to flow the original // receiver tags, adding that to parent span. - parentCtx = observability.ContextWithReceiverName(parentCtx, receiver) + parentCtx = obsreport.LegacyContextWithReceiverName(parentCtx, receiver) - exporterCtx := ExporterContext(parentCtx, exporter) + exporterCtx := obsreport.ExporterContext(parentCtx, exporter) errs := []error{nil, errFake} toSendMetricPts := []int{17, 23} toSendTimeSeries := []int{3, 5} for i, err := range errs { - ctx := StartMetricsExportOp(exporterCtx, exporter) + ctx := obsreport.StartMetricsExportOp(exporterCtx, exporter) assert.NotNil(t, ctx) var numDroppedTimeSeires int @@ -340,7 +342,7 @@ func Test_obsreport_ExportMetricsOp(t *testing.T) { numDroppedTimeSeires = toSendTimeSeries[i] } - EndMetricsExportOp( + obsreport.EndMetricsExportOp( ctx, toSendMetricPts[i], toSendTimeSeries[i], @@ -354,19 +356,19 @@ func Test_obsreport_ExportMetricsOp(t *testing.T) { var receivedTimeSeries, droppedTimeSeries int var sentPoints, failedToSendPoints int for i, span := range spans { - assert.Equal(t, exporterPrefix+exporter+exportMetricsOperationSuffix, span.Name) + assert.Equal(t, "exporter/"+exporter+"/MetricsExported", span.Name) receivedTimeSeries += toSendTimeSeries[i] switch errs[i] { case nil: sentPoints += toSendMetricPts[i] - assert.Equal(t, int64(toSendMetricPts[i]), span.Attributes[SentMetricPointsKey]) - assert.Equal(t, int64(0), span.Attributes[FailedToSendMetricPointsKey]) - assert.Equal(t, okStatus, span.Status) + assert.Equal(t, int64(toSendMetricPts[i]), span.Attributes[obsreport.SentMetricPointsKey]) + assert.Equal(t, int64(0), span.Attributes[obsreport.FailedToSendMetricPointsKey]) + assert.Equal(t, trace.Status{Code: trace.StatusCodeOK}, span.Status) case errFake: failedToSendPoints += toSendMetricPts[i] droppedTimeSeries += toSendTimeSeries[i] - assert.Equal(t, int64(0), span.Attributes[SentMetricPointsKey]) - assert.Equal(t, int64(toSendMetricPts[i]), span.Attributes[FailedToSendMetricPointsKey]) + assert.Equal(t, int64(0), span.Attributes[obsreport.SentMetricPointsKey]) + assert.Equal(t, int64(toSendMetricPts[i]), span.Attributes[obsreport.FailedToSendMetricPointsKey]) assert.Equal(t, errs[i].Error(), span.Status.Message) default: t.Fatalf("unexpected error: %v", errs[i]) @@ -374,8 +376,8 @@ func Test_obsreport_ExportMetricsOp(t *testing.T) { } // Check legacy metrics. - assert.NoError(t, observabilitytest.CheckValueViewExporterReceivedTimeSeries(receiver, exporter, receivedTimeSeries)) - assert.NoError(t, observabilitytest.CheckValueViewExporterDroppedTimeSeries(receiver, exporter, droppedTimeSeries)) + assert.NoError(t, obsreporttest.CheckValueViewExporterReceivedTimeSeries(receiver, exporter, receivedTimeSeries)) + assert.NoError(t, obsreporttest.CheckValueViewExporterDroppedTimeSeries(receiver, exporter, droppedTimeSeries)) // Check new metrics. exporterTags := exporterViewTags(exporter) checkValueForSumView(t, "exporter/sent_metric_points", exporterTags, sentPoints) @@ -399,7 +401,7 @@ func Test_obsreport_ReceiveWithLongLivedCtx(t *testing.T) { parentCtx, parentSpan := trace.StartSpan(context.Background(), t.Name()) defer parentSpan.End() - longLivedCtx := ReceiverContext(parentCtx, receiver, transport, legacyName) + longLivedCtx := obsreport.ReceiverContext(parentCtx, receiver, transport, legacyName) ops := []struct { numSpans int err error @@ -410,14 +412,14 @@ func Test_obsreport_ReceiveWithLongLivedCtx(t *testing.T) { for _, op := range ops { // Use a new context on each operation to simulate distinct operations // under the same long lived context. - ctx := StartTraceDataReceiveOp( + ctx := obsreport.StartTraceDataReceiveOp( longLivedCtx, receiver, transport, - WithLongLivedCtx()) + obsreport.WithLongLivedCtx()) assert.NotNil(t, ctx) - EndTraceDataReceiveOp( + obsreport.EndTraceDataReceiveOp( ctx, format, op.numSpans, @@ -434,16 +436,16 @@ func Test_obsreport_ReceiveWithLongLivedCtx(t *testing.T) { assert.Equal(t, trace.LinkTypeParent, link.Type) assert.Equal(t, parentSpan.SpanContext().TraceID, link.TraceID) assert.Equal(t, parentSpan.SpanContext().SpanID, link.SpanID) - assert.Equal(t, receiverPrefix+receiver+receiveTraceDataOperationSuffix, span.Name) - assert.Equal(t, transport, span.Attributes[TransportKey]) + assert.Equal(t, "receiver/"+receiver+"/TraceDataReceived", span.Name) + assert.Equal(t, transport, span.Attributes[obsreport.TransportKey]) switch ops[i].err { case nil: - assert.Equal(t, int64(ops[i].numSpans), span.Attributes[AcceptedSpansKey]) - assert.Equal(t, int64(0), span.Attributes[RefusedSpansKey]) - assert.Equal(t, okStatus, span.Status) + assert.Equal(t, int64(ops[i].numSpans), span.Attributes[obsreport.AcceptedSpansKey]) + assert.Equal(t, int64(0), span.Attributes[obsreport.RefusedSpansKey]) + assert.Equal(t, trace.Status{Code: trace.StatusCodeOK}, span.Status) case errFake: - assert.Equal(t, int64(0), span.Attributes[AcceptedSpansKey]) - assert.Equal(t, int64(ops[i].numSpans), span.Attributes[RefusedSpansKey]) + assert.Equal(t, int64(0), span.Attributes[obsreport.AcceptedSpansKey]) + assert.Equal(t, int64(ops[i].numSpans), span.Attributes[obsreport.RefusedSpansKey]) assert.Equal(t, ops[i].err.Error(), span.Status.Message) default: t.Fatalf("unexpected error: %v", ops[i].err) @@ -453,18 +455,18 @@ func Test_obsreport_ReceiveWithLongLivedCtx(t *testing.T) { func Test_obsreport_ProcessorTraceData(t *testing.T) { doneFn, err := setupViews() - defer doneFn() require.NoError(t, err) + defer doneFn() const acceptedSpans = 27 const refusedSpans = 19 const droppedSpans = 13 - processorCtx := ProcessorContext(context.Background(), processor) + processorCtx := obsreport.ProcessorContext(context.Background(), processor) - ProcessorTraceDataAccepted(processorCtx, acceptedSpans) - ProcessorTraceDataRefused(processorCtx, refusedSpans) - ProcessorTraceDataDropped(processorCtx, droppedSpans) + obsreport.ProcessorTraceDataAccepted(processorCtx, acceptedSpans) + obsreport.ProcessorTraceDataRefused(processorCtx, refusedSpans) + obsreport.ProcessorTraceDataDropped(processorCtx, droppedSpans) processorTags := processorViewTags(processor) checkValueForSumView(t, "processor/accepted_spans", processorTags, acceptedSpans) @@ -474,17 +476,17 @@ func Test_obsreport_ProcessorTraceData(t *testing.T) { func Test_obsreport_ProcessorMetricsData(t *testing.T) { doneFn, err := setupViews() - defer doneFn() require.NoError(t, err) + defer doneFn() const acceptedPoints = 29 const refusedPoints = 11 const droppedPoints = 17 - processorCtx := ProcessorContext(context.Background(), processor) - ProcessorMetricsDataAccepted(processorCtx, acceptedPoints) - ProcessorMetricsDataRefused(processorCtx, refusedPoints) - ProcessorMetricsDataDropped(processorCtx, droppedPoints) + processorCtx := obsreport.ProcessorContext(context.Background(), processor) + obsreport.ProcessorMetricsDataAccepted(processorCtx, acceptedPoints) + obsreport.ProcessorMetricsDataRefused(processorCtx, refusedPoints) + obsreport.ProcessorMetricsDataDropped(processorCtx, droppedPoints) processorTags := processorViewTags(processor) checkValueForSumView(t, "processor/accepted_metric_points", processorTags, acceptedPoints) @@ -510,12 +512,6 @@ func Test_obsreport_ProcessorMetricViews(t *testing.T) { }, } - // Ensure that the settings for useLegace and useNew a restored. - defer func(prevUseLegacy, prevUseNew bool) { - useLegacy = prevUseLegacy - useNew = prevUseNew - }(useLegacy, useNew) - tests := []struct { name string withLegacy bool @@ -550,17 +546,14 @@ func Test_obsreport_ProcessorMetricViews(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - useLegacy = tt.withLegacy - useNew = tt.withNew - got := ProcessorMetricViews("test_type", legacyViews) + obsreport.Configure(tt.withLegacy, tt.withNew) + got := obsreport.ProcessorMetricViews("test_type", legacyViews) assert.Equal(t, tt.want, got) }) } } func setupViews() (doneFn func(), err error) { - genLegacy := true - genNew := true - views := Configure(genLegacy, genNew) + views := obsreport.Configure(true, true) err = view.Register(views...) return func() { diff --git a/observability/observabilitytest/observabilitytest.go b/obsreport/obsreporttest/obsreporttest.go similarity index 78% rename from observability/observabilitytest/observabilitytest.go rename to obsreport/obsreporttest/obsreporttest.go index 0754674b4d7f..d6f6a16e5f44 100644 --- a/observability/observabilitytest/observabilitytest.go +++ b/obsreport/obsreporttest/obsreporttest.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package observabilitytest +package obsreporttest import ( "fmt" @@ -22,97 +22,97 @@ import ( "go.opencensus.io/stats/view" "go.opencensus.io/tag" - "go.opentelemetry.io/collector/observability" + "go.opentelemetry.io/collector/obsreport" ) // SetupRecordedMetricsTest does setup the testing environment to check the metrics recorded by receivers, producers or exporters. // The returned function should be deferred. func SetupRecordedMetricsTest() (doneFn func()) { // Register views - view.Register(observability.AllViews...) + view.Register(obsreport.LegacyAllViews...) return func() { - view.Unregister(observability.AllViews...) + view.Unregister(obsreport.LegacyAllViews...) } } -// CheckValueViewExporterReceivedSpans checks that for the current exported value in the ViewExporterReceivedSpans +// CheckValueViewExporterReceivedSpans checks that for the current exported value in the LegacyViewExporterReceivedSpans // for {TagKeyReceiver: receiverName, TagKeyExporter: exporterTagName} is equal to "value". // When this function is called it is required to also call SetupRecordedMetricsTest as first thing. func CheckValueViewExporterReceivedSpans(receiverName string, exporterTagName string, value int) error { - return checkValueForView(observability.ViewExporterReceivedSpans.Name, + return checkValueForView(obsreport.LegacyViewExporterReceivedSpans.Name, wantsTagsForExporterView(receiverName, exporterTagName), int64(value)) } -// CheckValueViewExporterDroppedSpans checks that for the current exported value in the ViewExporterDroppedSpans +// CheckValueViewExporterDroppedSpans checks that for the current exported value in the LegacyViewExporterDroppedSpans // for {TagKeyReceiver: receiverName} is equal to "value". // In tests that this function is called it is required to also call SetupRecordedMetricsTest as first thing. func CheckValueViewExporterDroppedSpans(receiverName string, exporterTagName string, value int) error { - return checkValueForView(observability.ViewExporterDroppedSpans.Name, + return checkValueForView(obsreport.LegacyViewExporterDroppedSpans.Name, wantsTagsForExporterView(receiverName, exporterTagName), int64(value)) } -// CheckValueViewExporterReceivedTimeSeries checks that for the current exported value in the ViewExporterReceivedTimeSeries +// CheckValueViewExporterReceivedTimeSeries checks that for the current exported value in the LegacyViewExporterReceivedTimeSeries // for {TagKeyReceiver: receiverName, TagKeyExporter: exporterTagName} is equal to "value". // When this function is called it is required to also call SetupRecordedMetricsTest as first thing. func CheckValueViewExporterReceivedTimeSeries(receiverName string, exporterTagName string, value int) error { - return checkValueForView(observability.ViewExporterReceivedTimeSeries.Name, + return checkValueForView(obsreport.LegacyViewExporterReceivedTimeSeries.Name, wantsTagsForExporterView(receiverName, exporterTagName), int64(value)) } -// CheckValueViewExporterDroppedTimeSeries checks that for the current exported value in the ViewExporterDroppedTimeSeries +// CheckValueViewExporterDroppedTimeSeries checks that for the current exported value in the LegacyViewExporterDroppedTimeSeries // for {TagKeyReceiver: receiverName} is equal to "value". // In tests that this function is called it is required to also call SetupRecordedMetricsTest as first thing. func CheckValueViewExporterDroppedTimeSeries(receiverName string, exporterTagName string, value int) error { - return checkValueForView(observability.ViewExporterDroppedTimeSeries.Name, + return checkValueForView(obsreport.LegacyViewExporterDroppedTimeSeries.Name, wantsTagsForExporterView(receiverName, exporterTagName), int64(value)) } -// CheckValueViewExporterReceivedLogRecords checks that for the current exported value in the ViewExporterReceivedLogRecords +// CheckValueViewExporterReceivedLogRecords checks that for the current exported value in the LegacyViewExporterReceivedLogRecords // for {TagKeyReceiver: receiverName, TagKeyExporter: exporterTagName} is equal to "value". // When this function is called it is required to also call SetupRecordedMetricsTest as first thing. func CheckValueViewExporterReceivedLogRecords(receiverName string, exporterTagName string, value int) error { - return checkValueForView(observability.ViewExporterReceivedLogRecords.Name, + return checkValueForView(obsreport.LegacyViewExporterReceivedLogRecords.Name, wantsTagsForExporterView(receiverName, exporterTagName), int64(value)) } -// CheckValueViewExporterDroppedLogRecords checks that for the current exported value in the ViewExporterDroppedLogRecords +// CheckValueViewExporterDroppedLogRecords checks that for the current exported value in the LegacyViewExporterDroppedLogRecords // for {TagKeyReceiver: receiverName} is equal to "value". // In tests that this function is called it is required to also call SetupRecordedMetricsTest as first thing. func CheckValueViewExporterDroppedLogRecords(receiverName string, exporterTagName string, value int) error { - return checkValueForView(observability.ViewExporterDroppedLogRecords.Name, + return checkValueForView(obsreport.LegacyViewExporterDroppedLogRecords.Name, wantsTagsForExporterView(receiverName, exporterTagName), int64(value)) } -// CheckValueViewReceiverReceivedSpans checks that for the current exported value in the ViewReceiverReceivedSpans +// CheckValueViewReceiverReceivedSpans checks that for the current exported value in the LegacyViewReceiverReceivedSpans // for {TagKeyReceiver: receiverName, TagKeyExporter: exporterTagName} is equal to "value". // In tests that this function is called it is required to also call SetupRecordedMetricsTest as first thing. func CheckValueViewReceiverReceivedSpans(receiverName string, value int) error { - return checkValueForView(observability.ViewReceiverReceivedSpans.Name, + return checkValueForView(obsreport.LegacyViewReceiverReceivedSpans.Name, wantsTagsForReceiverView(receiverName), int64(value)) } -// CheckValueViewReceiverDroppedSpans checks that for the current exported value in the ViewReceiverDroppedSpans +// CheckValueViewReceiverDroppedSpans checks that for the current exported value in the LegacyViewReceiverDroppedSpans // for {TagKeyReceiver: receiverName} is equal to "value". // In tests that this function is called it is required to also call SetupRecordedMetricsTest as first thing. func CheckValueViewReceiverDroppedSpans(receiverName string, value int) error { - return checkValueForView(observability.ViewReceiverDroppedSpans.Name, + return checkValueForView(obsreport.LegacyViewReceiverDroppedSpans.Name, wantsTagsForReceiverView(receiverName), int64(value)) } -// CheckValueViewReceiverReceivedTimeSeries checks that for the current exported value in the ViewReceiverReceivedTimeSeries +// CheckValueViewReceiverReceivedTimeSeries checks that for the current exported value in the LegacyViewReceiverReceivedTimeSeries // for {TagKeyReceiver: receiverName, TagKeyExporter: exporterTagName} is equal to "value". // In tests that this function is called it is required to also call SetupRecordedMetricsTest as first thing. func CheckValueViewReceiverReceivedTimeSeries(receiverName string, value int) error { - return checkValueForView(observability.ViewReceiverReceivedTimeSeries.Name, + return checkValueForView(obsreport.LegacyViewReceiverReceivedTimeSeries.Name, wantsTagsForReceiverView(receiverName), int64(value)) } -// CheckValueViewReceiverDroppedTimeSeries checks that for the current exported value in the ViewReceiverDroppedTimeSeries +// CheckValueViewReceiverDroppedTimeSeries checks that for the current exported value in the LegacyViewReceiverDroppedTimeSeries // for {TagKeyReceiver: receiverName} is equal to "value". // In tests that this function is called it is required to also call SetupRecordedMetricsTest as first thing. func CheckValueViewReceiverDroppedTimeSeries(receiverName string, value int) error { - return checkValueForView(observability.ViewReceiverDroppedTimeSeries.Name, + return checkValueForView(obsreport.LegacyViewReceiverDroppedTimeSeries.Name, wantsTagsForReceiverView(receiverName), int64(value)) } @@ -142,14 +142,14 @@ func checkValueForView(vName string, wantTags []tag.Tag, value int64) error { func wantsTagsForExporterView(receiverName string, exporterTagName string) []tag.Tag { return []tag.Tag{ - {Key: observability.TagKeyReceiver, Value: receiverName}, - {Key: observability.TagKeyExporter, Value: exporterTagName}, + {Key: obsreport.TagKeyReceiver, Value: receiverName}, + {Key: obsreport.TagKeyExporter, Value: exporterTagName}, } } func wantsTagsForReceiverView(receiverName string) []tag.Tag { return []tag.Tag{ - {Key: observability.TagKeyReceiver, Value: receiverName}, + {Key: obsreport.TagKeyReceiver, Value: receiverName}, } } diff --git a/obsreport/obsreporttest/obsreporttest_test.go b/obsreport/obsreporttest/obsreporttest_test.go new file mode 100644 index 000000000000..8eb37ca75bbd --- /dev/null +++ b/obsreport/obsreporttest/obsreporttest_test.go @@ -0,0 +1,142 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// observabilitytest_test instead of just obsreporttest to avoid dependency cycle between +// obsreporttest and other test packages. +package obsreporttest_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + + "go.opentelemetry.io/collector/obsreport" + "go.opentelemetry.io/collector/obsreport/obsreporttest" +) + +const ( + receiverName = "fake_receiver" + exporterName = "fake_exporter" +) + +func TestCheckValueViewTraceReceiverViews(t *testing.T) { + doneFn := obsreporttest.SetupRecordedMetricsTest() + defer doneFn() + + receiverCtx := obsreport.LegacyContextWithReceiverName(context.Background(), receiverName) + obsreport.LegacyRecordMetricsForTraceReceiver(receiverCtx, 17, 13) + // Test expected values. + err := obsreporttest.CheckValueViewReceiverReceivedSpans(receiverName, 17) + require.Nil(t, err, "When check receiver received spans") + err = obsreporttest.CheckValueViewReceiverDroppedSpans(receiverName, 13) + require.Nil(t, err, "When check receiver dropped spans") + + // Test unexpected tag values + err = obsreporttest.CheckValueViewReceiverReceivedSpans(exporterName, 17) + require.NotNil(t, err, "When check for unexpected tag value") + err = obsreporttest.CheckValueViewReceiverDroppedSpans(exporterName, 13) + require.NotNil(t, err, "When check for unexpected tag value") + + // Test unexpected recorded values + err = obsreporttest.CheckValueViewReceiverReceivedSpans(receiverName, 13) + require.NotNil(t, err, "When check for unexpected value") + err = obsreporttest.CheckValueViewReceiverDroppedSpans(receiverName, 17) + require.NotNil(t, err, "When check for unexpected value") +} + +func TestCheckValueViewMetricsReceiverViews(t *testing.T) { + doneFn := obsreporttest.SetupRecordedMetricsTest() + defer doneFn() + + receiverCtx := obsreport.LegacyContextWithReceiverName(context.Background(), receiverName) + obsreport.LegacyRecordMetricsForMetricsReceiver(receiverCtx, 17, 13) + // Test expected values. + err := obsreporttest.CheckValueViewReceiverReceivedTimeSeries(receiverName, 17) + require.Nil(t, err, "When check receiver received timeseries") + err = obsreporttest.CheckValueViewReceiverDroppedTimeSeries(receiverName, 13) + require.Nil(t, err, "When check receiver dropped timeseries") + + // Test unexpected tag values + err = obsreporttest.CheckValueViewReceiverReceivedTimeSeries(exporterName, 17) + require.NotNil(t, err, "When check for unexpected tag value") + err = obsreporttest.CheckValueViewReceiverDroppedTimeSeries(exporterName, 13) + require.NotNil(t, err, "When check for unexpected tag value") + + // Test unexpected recorded values + err = obsreporttest.CheckValueViewReceiverReceivedTimeSeries(receiverName, 13) + require.NotNil(t, err, "When check for unexpected value") + err = obsreporttest.CheckValueViewReceiverDroppedTimeSeries(receiverName, 17) + require.NotNil(t, err, "When check for unexpected value") +} + +func TestCheckValueViewTraceExporterViews(t *testing.T) { + doneFn := obsreporttest.SetupRecordedMetricsTest() + defer doneFn() + + receiverCtx := obsreport.LegacyContextWithReceiverName(context.Background(), receiverName) + exporterCtx := obsreport.LegacyContextWithExporterName(receiverCtx, exporterName) + obsreport.LegacyRecordMetricsForTraceExporter(exporterCtx, 17, 13) + // Test expected values. + err := obsreporttest.CheckValueViewExporterReceivedSpans(receiverName, exporterName, 17) + require.Nil(t, err, "When check exporter received spans") + err = obsreporttest.CheckValueViewExporterDroppedSpans(receiverName, exporterName, 13) + require.Nil(t, err, "When check exporter dropped spans") + + // Test unexpected tag values + err = obsreporttest.CheckValueViewExporterReceivedSpans(receiverName, receiverName, 17) + require.NotNil(t, err, "When check for unexpected tag value") + err = obsreporttest.CheckValueViewExporterDroppedSpans(receiverName, receiverName, 13) + require.NotNil(t, err, "When check for unexpected tag value") + + // Test unexpected recorded values + err = obsreporttest.CheckValueViewExporterReceivedSpans(receiverName, exporterName, 13) + require.NotNil(t, err, "When check for unexpected value") + err = obsreporttest.CheckValueViewExporterDroppedSpans(receiverName, exporterName, 17) + require.NotNil(t, err, "When check for unexpected value") +} + +func TestCheckValueViewMetricsExporterViews(t *testing.T) { + doneFn := obsreporttest.SetupRecordedMetricsTest() + defer doneFn() + + receiverCtx := obsreport.LegacyContextWithReceiverName(context.Background(), receiverName) + exporterCtx := obsreport.LegacyContextWithExporterName(receiverCtx, exporterName) + obsreport.LegacyRecordMetricsForMetricsExporter(exporterCtx, 17, 13) + // Test expected values. + err := obsreporttest.CheckValueViewExporterReceivedTimeSeries(receiverName, exporterName, 17) + require.Nil(t, err, "When check exporter received timeseries") + err = obsreporttest.CheckValueViewExporterDroppedTimeSeries(receiverName, exporterName, 13) + require.Nil(t, err, "When check exporter dropped timeseries") + + // Test unexpected tag values + err = obsreporttest.CheckValueViewExporterReceivedTimeSeries(receiverName, receiverName, 17) + require.NotNil(t, err, "When check for unexpected tag value") + err = obsreporttest.CheckValueViewExporterDroppedTimeSeries(receiverName, receiverName, 13) + require.NotNil(t, err, "When check for unexpected tag value") + + // Test unexpected recorded values + err = obsreporttest.CheckValueViewExporterReceivedTimeSeries(receiverName, exporterName, 13) + require.NotNil(t, err, "When check for unexpected value") + err = obsreporttest.CheckValueViewExporterDroppedTimeSeries(receiverName, exporterName, 17) + require.NotNil(t, err, "When check for unexpected value") +} + +func TestNoSetupCalled(t *testing.T) { + receiverCtx := obsreport.LegacyContextWithReceiverName(context.Background(), receiverName) + obsreport.LegacyRecordMetricsForTraceReceiver(receiverCtx, 17, 13) + // Failed to check because views are not registered. + err := obsreporttest.CheckValueViewReceiverReceivedSpans(receiverName, 17) + require.NotNil(t, err, "When check with no views registered") +} diff --git a/receiver/opencensusreceiver/ocmetrics/opencensus_test.go b/receiver/opencensusreceiver/ocmetrics/opencensus_test.go index 63793d9946ee..ef9dcfbb2678 100644 --- a/receiver/opencensusreceiver/ocmetrics/opencensus_test.go +++ b/receiver/opencensusreceiver/ocmetrics/opencensus_test.go @@ -36,7 +36,7 @@ import ( "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/consumer/consumerdata" "go.opentelemetry.io/collector/internal" - "go.opentelemetry.io/collector/observability" + "go.opentelemetry.io/collector/obsreport" "go.opentelemetry.io/collector/testutil" ) @@ -352,7 +352,7 @@ func ocReceiverOnGRPCServer(t *testing.T, sr consumer.MetricsConsumerOld) (oci * require.NoError(t, err, "Failed to create the Receiver: %v", err) // Now run it as a gRPC server - srv := observability.GRPCServerWithObservabilityEnabled() + srv := obsreport.GRPCServerWithObservabilityEnabled() agentmetricspb.RegisterMetricsServiceServer(srv, oci) go func() { _ = srv.Serve(ln) diff --git a/receiver/opencensusreceiver/octrace/observability_test.go b/receiver/opencensusreceiver/octrace/observability_test.go index e3e374d1c0df..8231a6f87ee4 100644 --- a/receiver/opencensusreceiver/octrace/observability_test.go +++ b/receiver/opencensusreceiver/octrace/observability_test.go @@ -29,7 +29,7 @@ import ( "go.opencensus.io/trace" "go.opentelemetry.io/collector/exporter/exportertest" - "go.opentelemetry.io/collector/observability/observabilitytest" + "go.opentelemetry.io/collector/obsreport/obsreporttest" ) // Ensure that if we add a metrics exporter that our target metrics @@ -40,7 +40,7 @@ import ( // test is to ensure exactness, but with the mentioned views registered, the // output will be quite noisy. func TestEnsureRecordedMetrics(t *testing.T) { - doneFn := observabilitytest.SetupRecordedMetricsTest() + doneFn := obsreporttest.SetupRecordedMetricsTest() defer doneFn() _, port, doneReceiverFn := ocReceiverOnGRPCServer(t, exportertest.NewNopTraceExporterOld()) @@ -57,14 +57,14 @@ func TestEnsureRecordedMetrics(t *testing.T) { } flush(traceSvcDoneFn) - err = observabilitytest.CheckValueViewReceiverReceivedSpans("oc_trace", n) + err = obsreporttest.CheckValueViewReceiverReceivedSpans("oc_trace", n) require.NoError(t, err, "When check recorded values: want nil got %v", err) - err = observabilitytest.CheckValueViewReceiverDroppedSpans("oc_trace", 0) + err = obsreporttest.CheckValueViewReceiverDroppedSpans("oc_trace", 0) require.NoError(t, err, "When check recorded values: want nil got %v", err) } func TestEnsureRecordedMetrics_zeroLengthSpansSender(t *testing.T) { - doneFn := observabilitytest.SetupRecordedMetricsTest() + doneFn := obsreporttest.SetupRecordedMetricsTest() defer doneFn() _, port, doneFn := ocReceiverOnGRPCServer(t, exportertest.NewNopTraceExporterOld()) @@ -80,9 +80,9 @@ func TestEnsureRecordedMetrics_zeroLengthSpansSender(t *testing.T) { } flush(traceSvcDoneFn) - err = observabilitytest.CheckValueViewReceiverReceivedSpans("oc_trace", 0) + err = obsreporttest.CheckValueViewReceiverReceivedSpans("oc_trace", 0) require.NoError(t, err, "When check recorded values: want nil got %v", err) - err = observabilitytest.CheckValueViewReceiverDroppedSpans("oc_trace", 0) + err = obsreporttest.CheckValueViewReceiverDroppedSpans("oc_trace", 0) require.NoError(t, err, "When check recorded values: want nil got %v", err) } diff --git a/receiver/opencensusreceiver/octrace/opencensus_test.go b/receiver/opencensusreceiver/octrace/opencensus_test.go index 20799a33dc4e..d5e3d3bc853b 100644 --- a/receiver/opencensusreceiver/octrace/opencensus_test.go +++ b/receiver/opencensusreceiver/octrace/opencensus_test.go @@ -40,7 +40,7 @@ import ( "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/consumer/consumerdata" "go.opentelemetry.io/collector/internal" - "go.opentelemetry.io/collector/observability" + "go.opentelemetry.io/collector/obsreport" "go.opentelemetry.io/collector/testutil" ) @@ -481,7 +481,7 @@ func ocReceiverOnGRPCServer(t *testing.T, sr consumer.TraceConsumerOld, opts ... require.NoError(t, err, "Failed to create the Receiver: %v", err) // Now run it as a gRPC server - srv := observability.GRPCServerWithObservabilityEnabled() + srv := obsreport.GRPCServerWithObservabilityEnabled() agenttracepb.RegisterTraceServiceServer(srv, oci) go func() { _ = srv.Serve(ln) diff --git a/receiver/opencensusreceiver/opencensus.go b/receiver/opencensusreceiver/opencensus.go index 65df15e88eb3..f944349a31a8 100644 --- a/receiver/opencensusreceiver/opencensus.go +++ b/receiver/opencensusreceiver/opencensus.go @@ -32,7 +32,7 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenterror" "go.opentelemetry.io/collector/consumer" - "go.opentelemetry.io/collector/observability" + "go.opentelemetry.io/collector/obsreport" "go.opentelemetry.io/collector/receiver/opencensusreceiver/ocmetrics" "go.opentelemetry.io/collector/receiver/opencensusreceiver/octrace" ) @@ -137,7 +137,7 @@ func (ocr *Receiver) grpcServer() *grpc.Server { defer ocr.mu.Unlock() if ocr.serverGRPC == nil { - ocr.serverGRPC = observability.GRPCServerWithObservabilityEnabled(ocr.grpcServerOptions...) + ocr.serverGRPC = obsreport.GRPCServerWithObservabilityEnabled(ocr.grpcServerOptions...) } return ocr.serverGRPC diff --git a/receiver/opencensusreceiver/opencensus_test.go b/receiver/opencensusreceiver/opencensus_test.go index 63788898beeb..3e2b882e5681 100644 --- a/receiver/opencensusreceiver/opencensus_test.go +++ b/receiver/opencensusreceiver/opencensus_test.go @@ -46,7 +46,7 @@ import ( "go.opentelemetry.io/collector/consumer/consumerdata" "go.opentelemetry.io/collector/exporter/exportertest" "go.opentelemetry.io/collector/internal" - "go.opentelemetry.io/collector/observability/observabilitytest" + "go.opentelemetry.io/collector/obsreport/obsreporttest" "go.opentelemetry.io/collector/testutil" ) @@ -517,7 +517,7 @@ func TestOCReceiverTrace_HandleNextConsumerResponse(t *testing.T) { for _, exporter := range exporters { for _, tt := range tests { t.Run(tt.name+"/"+exporter.receiverTag, func(t *testing.T) { - doneFn := observabilitytest.SetupRecordedMetricsTest() + doneFn := obsreporttest.SetupRecordedMetricsTest() defer doneFn() sink := new(sinkTraceConsumer) @@ -554,7 +554,7 @@ func TestOCReceiverTrace_HandleNextConsumerResponse(t *testing.T) { require.Equal(t, tt.expectedReceivedBatches, len(sink.AllTraces())) require.Nil( t, - observabilitytest.CheckValueViewReceiverReceivedSpans( + obsreporttest.CheckValueViewReceiverReceivedSpans( exporter.receiverTag, tt.expectedReceivedBatches), ) @@ -670,7 +670,7 @@ func TestOCReceiverMetrics_HandleNextConsumerResponse(t *testing.T) { for _, exporter := range exporters { for _, tt := range tests { t.Run(tt.name+"/"+exporter.receiverTag, func(t *testing.T) { - doneFn := observabilitytest.SetupRecordedMetricsTest() + doneFn := obsreporttest.SetupRecordedMetricsTest() defer doneFn() sink := new(sinkMetricsConsumer) @@ -707,7 +707,7 @@ func TestOCReceiverMetrics_HandleNextConsumerResponse(t *testing.T) { require.Equal(t, tt.expectedReceivedBatches, len(sink.AllMetrics())) require.Nil( t, - observabilitytest.CheckValueViewReceiverReceivedTimeSeries( + obsreporttest.CheckValueViewReceiverReceivedTimeSeries( exporter.receiverTag, tt.expectedReceivedBatches), ) diff --git a/receiver/otlpreceiver/metrics/otlp_test.go b/receiver/otlpreceiver/metrics/otlp_test.go index 3aa735a9e8a0..76fbe8a61023 100644 --- a/receiver/otlpreceiver/metrics/otlp_test.go +++ b/receiver/otlpreceiver/metrics/otlp_test.go @@ -31,7 +31,7 @@ import ( collectormetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/metrics/v1" otlpcommon "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1" otlpmetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/metrics/v1" - "go.opentelemetry.io/collector/observability" + "go.opentelemetry.io/collector/obsreport" "go.opentelemetry.io/collector/testutil" ) @@ -208,7 +208,7 @@ func otlpReceiverOnGRPCServer(t *testing.T, mc consumer.MetricsConsumer) (r *Rec r = New(receiverTagValue, mc) // Now run it as a gRPC server - srv := observability.GRPCServerWithObservabilityEnabled() + srv := obsreport.GRPCServerWithObservabilityEnabled() collectormetrics.RegisterMetricsServiceServer(srv, r) go func() { _ = srv.Serve(ln) diff --git a/receiver/otlpreceiver/otlp_test.go b/receiver/otlpreceiver/otlp_test.go index 557275f57d4e..f5631b411fbc 100644 --- a/receiver/otlpreceiver/otlp_test.go +++ b/receiver/otlpreceiver/otlp_test.go @@ -48,7 +48,7 @@ import ( otlpresource "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/resource/v1" otlptrace "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/trace/v1" "go.opentelemetry.io/collector/internal/data/testdata" - "go.opentelemetry.io/collector/observability/observabilitytest" + "go.opentelemetry.io/collector/obsreport/obsreporttest" "go.opentelemetry.io/collector/testutil" "go.opentelemetry.io/collector/translator/conventions" ) @@ -351,7 +351,7 @@ func TestOTLPReceiverTrace_HandleNextConsumerResponse(t *testing.T) { for _, exporter := range exporters { for _, tt := range tests { t.Run(tt.name+"/"+exporter.receiverTag, func(t *testing.T) { - doneFn := observabilitytest.SetupRecordedMetricsTest() + doneFn := obsreporttest.SetupRecordedMetricsTest() defer doneFn() sink := new(exportertest.SinkTraceExporter) @@ -382,7 +382,7 @@ func TestOTLPReceiverTrace_HandleNextConsumerResponse(t *testing.T) { require.Equal(t, tt.expectedReceivedBatches, len(sink.AllTraces())) require.Nil( t, - observabilitytest.CheckValueViewReceiverReceivedSpans( + obsreporttest.CheckValueViewReceiverReceivedSpans( exporter.receiverTag, tt.expectedReceivedBatches), ) diff --git a/receiver/otlpreceiver/trace/otlp_test.go b/receiver/otlpreceiver/trace/otlp_test.go index c4a6e9cbcb69..c281d4201004 100644 --- a/receiver/otlpreceiver/trace/otlp_test.go +++ b/receiver/otlpreceiver/trace/otlp_test.go @@ -30,7 +30,7 @@ import ( "go.opentelemetry.io/collector/exporter/exportertest" collectortrace "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/collector/trace/v1" otlptrace "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/trace/v1" - "go.opentelemetry.io/collector/observability" + "go.opentelemetry.io/collector/obsreport" "go.opentelemetry.io/collector/testutil" ) @@ -179,7 +179,7 @@ func otlpReceiverOnGRPCServer(t *testing.T, tc consumer.TraceConsumer) (r *Recei require.NoError(t, err) // Now run it as a gRPC server - srv := observability.GRPCServerWithObservabilityEnabled() + srv := obsreport.GRPCServerWithObservabilityEnabled() collectortrace.RegisterTraceServiceServer(srv, r) go func() { _ = srv.Serve(ln)