From d8bb21ab1bcfeb7e8436a7c7c703719a1f61a270 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Fri, 16 Sep 2022 13:29:25 -0700 Subject: [PATCH] Fix prom exporter panic on invalid metric Resolves #3180 --- exporters/prometheus/exporter.go | 2 ++ exporters/prometheus/exporter_test.go | 28 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/exporters/prometheus/exporter.go b/exporters/prometheus/exporter.go index 8527b1b5025..e210728c482 100644 --- a/exporters/prometheus/exporter.go +++ b/exporters/prometheus/exporter.go @@ -90,12 +90,14 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) { m, err := prometheus.NewConstHistogram(metricData.description, metricData.histogramCount, metricData.histogramSum, metricData.histogramBuckets, metricData.attributeValues...) if err != nil { otel.Handle(err) + continue } ch <- m } else { m, err := prometheus.NewConstMetric(metricData.description, metricData.valueType, metricData.value, metricData.attributeValues...) if err != nil { otel.Handle(err) + continue } ch <- m } diff --git a/exporters/prometheus/exporter_test.go b/exporters/prometheus/exporter_test.go index aaf029e2021..173df4b98a8 100644 --- a/exporters/prometheus/exporter_test.go +++ b/exporters/prometheus/exporter_test.go @@ -103,6 +103,34 @@ func TestPrometheusExporter(t *testing.T) { counter.Add(ctx, 9, attrs...) }, }, + { + name: "invalid instruments are dropped", + expectedFile: "testdata/gauge.txt", + recordMetrics: func(ctx context.Context, meter otelmetric.Meter) { + attrs := []attribute.KeyValue{ + attribute.Key("A").String("B"), + attribute.Key("C").String("D"), + } + // Valid. + gauge, err := meter.SyncFloat64().UpDownCounter("bar", instrument.WithDescription("a fun little gauge")) + require.NoError(t, err) + gauge.Add(ctx, 100, attrs...) + gauge.Add(ctx, -25, attrs...) + + // Invalid, should be dropped. + gauge, err = meter.SyncFloat64().UpDownCounter("invalid.gauge.name") + require.NoError(t, err) + gauge.Add(ctx, 100, attrs...) + + counter, err := meter.SyncFloat64().Counter("invalid.counter.name") + require.NoError(t, err) + counter.Add(ctx, 100, attrs...) + + histogram, err := meter.SyncFloat64().Histogram("invalid.hist.name") + require.NoError(t, err) + histogram.Record(ctx, 23, attrs...) + }, + }, } for _, tc := range testCases {