Skip to content

Commit ed1e7f4

Browse files
authored
receiver/prometheusremotewrite: Correctly handle metrics without type metadata (#41005)
#### Description While trying the component end-to-end, I've noticed we would hit nilpointer references almost immediately. While accessing this code path: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/57704cea35e430e3c5de76add038b99e06f79d73/receiver/prometheusremotewritereceiver/receiver.go#L371 `metric` was nil The `metric` variable is generated in this code path: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/57704cea35e430e3c5de76add038b99e06f79d73/receiver/prometheusremotewritereceiver/receiver.go#L340-L367 Suppose it's present in the cache—perfect. It was already assigned from the start. If it was not present in the cache, we create it in the next if conditional, but only for the types we support! With this PR, we're now skipping all metrics without type metadata. Signed-off-by: Arthur Silva Sens <[email protected]>
1 parent 78e365c commit ed1e7f4

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
change_type: bug_fix
2+
component: receiver/prometheusremotewrite
3+
note: Handle metrics with unspecified types without panicking.
4+
issues: [41005]
5+
subtext:
6+
change_logs: [user]

receiver/prometheusremotewritereceiver/receiver.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ func (prw *prometheusRemoteWriteReceiver) translateV2(_ context.Context, req *wr
361361
case writev2.Metadata_METRIC_TYPE_SUMMARY:
362362
// Drop summary series as we will not handle them.
363363
continue
364+
default:
365+
badRequestErrors = errors.Join(badRequestErrors, fmt.Errorf("unsupported metric type %q for metric %q", ts.Metadata.Type, metricName))
366+
continue
364367
}
365368

366369
metricCache[metricKey] = metric

receiver/prometheusremotewritereceiver/receiver_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,26 @@ func TestTranslateV2(t *testing.T) {
253253
},
254254
expectError: "help ref 3 is out of bounds of symbolsTable",
255255
},
256+
{
257+
name: "unsupported metric type UNSPECIFIED",
258+
request: &writev2.Request{
259+
Symbols: []string{"", "__name__", "test_metric", "job", "test_job", "instance", "test_instance"},
260+
Timeseries: []writev2.TimeSeries{
261+
{
262+
Metadata: writev2.Metadata{Type: writev2.Metadata_METRIC_TYPE_UNSPECIFIED},
263+
LabelsRefs: []uint32{1, 2, 3, 4, 5, 6},
264+
Samples: []writev2.Sample{{Value: 1, Timestamp: 1}},
265+
},
266+
},
267+
},
268+
expectError: `unsupported metric type "METRIC_TYPE_UNSPECIFIED" for metric "test_metric"`,
269+
expectedStats: remote.WriteResponseStats{
270+
Confirmed: false,
271+
Samples: 0,
272+
Histograms: 0,
273+
Exemplars: 0,
274+
},
275+
},
256276
{
257277
name: "valid request",
258278
request: writeV2RequestFixture,

0 commit comments

Comments
 (0)