Skip to content

Commit ae23d5c

Browse files
committed
return nil for empty metrics
1 parent dddbfed commit ae23d5c

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

bridge/opencensus/metric.go

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func (p *producer) Produce(_ context.Context) ([]metricdata.ScopeMetrics, error)
5353
data = append(data, ocProducer.Read()...)
5454
}
5555
otelmetrics, err := internal.ConvertMetrics(data)
56+
if len(otelmetrics) == 0 {
57+
return nil, err
58+
}
5659
return []metricdata.ScopeMetrics{{
5760
Scope: instrumentation.Scope{
5861
Name: scopeName,

bridge/opencensus/metric_test.go

+11-13
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,12 @@ func TestMetricProducer(t *testing.T) {
3838
for _, tc := range []struct {
3939
desc string
4040
input []*ocmetricdata.Metric
41-
expected metricdata.ScopeMetrics
41+
expected []metricdata.ScopeMetrics
4242
expectErr bool
4343
}{
4444
{
45-
desc: "empty",
46-
expected: metricdata.ScopeMetrics{
47-
Scope: instrumentation.Scope{
48-
Name: scopeName,
49-
},
50-
},
45+
desc: "empty",
46+
expected: nil,
5147
},
5248
{
5349
desc: "success",
@@ -69,7 +65,7 @@ func TestMetricProducer(t *testing.T) {
6965
},
7066
},
7167
},
72-
expected: metricdata.ScopeMetrics{
68+
expected: []metricdata.ScopeMetrics{{
7369
Scope: instrumentation.Scope{
7470
Name: scopeName,
7571
},
@@ -87,7 +83,7 @@ func TestMetricProducer(t *testing.T) {
8783
},
8884
},
8985
},
90-
},
86+
}},
9187
},
9288
{
9389
desc: "partial success",
@@ -117,7 +113,7 @@ func TestMetricProducer(t *testing.T) {
117113
},
118114
},
119115
},
120-
expected: metricdata.ScopeMetrics{
116+
expected: []metricdata.ScopeMetrics{{
121117
Scope: instrumentation.Scope{
122118
Name: scopeName,
123119
},
@@ -135,7 +131,7 @@ func TestMetricProducer(t *testing.T) {
135131
},
136132
},
137133
},
138-
},
134+
}},
139135
expectErr: true,
140136
},
141137
} {
@@ -149,8 +145,10 @@ func TestMetricProducer(t *testing.T) {
149145
} else {
150146
require.Nil(t, err)
151147
}
152-
require.Equal(t, len(output), 1)
153-
metricdatatest.AssertEqual(t, tc.expected, output[0])
148+
require.Equal(t, len(output), len(tc.expected))
149+
for i := range output {
150+
metricdatatest.AssertEqual(t, tc.expected[i], output[i])
151+
}
154152
})
155153
}
156154
}

0 commit comments

Comments
 (0)