From 50f9f557a4b6a76f2bdcb6e67a17df9036eb1bc8 Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Thu, 15 Dec 2022 21:11:50 +0000 Subject: [PATCH] return nil for empty metrics --- bridge/opencensus/metric.go | 3 +++ bridge/opencensus/metric_test.go | 24 +++++++++++------------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/bridge/opencensus/metric.go b/bridge/opencensus/metric.go index 8e058ed00b40..e0d197e910ff 100644 --- a/bridge/opencensus/metric.go +++ b/bridge/opencensus/metric.go @@ -53,6 +53,9 @@ func (p *producer) Produce(_ context.Context) ([]metricdata.ScopeMetrics, error) data = append(data, ocProducer.Read()...) } otelmetrics, err := internal.ConvertMetrics(data) + if len(otelmetrics) == 0 { + return nil, err + } return []metricdata.ScopeMetrics{{ Scope: instrumentation.Scope{ Name: scopeName, diff --git a/bridge/opencensus/metric_test.go b/bridge/opencensus/metric_test.go index 49dd46f5698b..ed348901f4e4 100644 --- a/bridge/opencensus/metric_test.go +++ b/bridge/opencensus/metric_test.go @@ -38,16 +38,12 @@ func TestMetricProducer(t *testing.T) { for _, tc := range []struct { desc string input []*ocmetricdata.Metric - expected metricdata.ScopeMetrics + expected []metricdata.ScopeMetrics expectErr bool }{ { - desc: "empty", - expected: metricdata.ScopeMetrics{ - Scope: instrumentation.Scope{ - Name: scopeName, - }, - }, + desc: "empty", + expected: nil, }, { desc: "success", @@ -69,7 +65,7 @@ func TestMetricProducer(t *testing.T) { }, }, }, - expected: metricdata.ScopeMetrics{ + expected: []metricdata.ScopeMetrics{{ Scope: instrumentation.Scope{ Name: scopeName, }, @@ -87,7 +83,7 @@ func TestMetricProducer(t *testing.T) { }, }, }, - }, + }}, }, { desc: "partial success", @@ -117,7 +113,7 @@ func TestMetricProducer(t *testing.T) { }, }, }, - expected: metricdata.ScopeMetrics{ + expected: []metricdata.ScopeMetrics{{ Scope: instrumentation.Scope{ Name: scopeName, }, @@ -135,7 +131,7 @@ func TestMetricProducer(t *testing.T) { }, }, }, - }, + }}, expectErr: true, }, } { @@ -149,8 +145,10 @@ func TestMetricProducer(t *testing.T) { } else { require.Nil(t, err) } - require.Equal(t, len(output), 1) - metricdatatest.AssertEqual(t, tc.expected, output[0]) + require.Equal(t, len(output), len(tc.expected)) + for i := range output { + metricdatatest.AssertEqual(t, tc.expected[i], output[i]) + } }) } }