Skip to content

Commit 798b423

Browse files
authored
Fix prom exporter panic on invalid metric (#3182)
Resolves #3180
1 parent 30fcf78 commit 798b423

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

exporters/prometheus/exporter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
9090
m, err := prometheus.NewConstHistogram(metricData.description, metricData.histogramCount, metricData.histogramSum, metricData.histogramBuckets, metricData.attributeValues...)
9191
if err != nil {
9292
otel.Handle(err)
93+
continue
9394
}
9495
ch <- m
9596
} else {
9697
m, err := prometheus.NewConstMetric(metricData.description, metricData.valueType, metricData.value, metricData.attributeValues...)
9798
if err != nil {
9899
otel.Handle(err)
100+
continue
99101
}
100102
ch <- m
101103
}

exporters/prometheus/exporter_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,34 @@ func TestPrometheusExporter(t *testing.T) {
103103
counter.Add(ctx, 9, attrs...)
104104
},
105105
},
106+
{
107+
name: "invalid instruments are dropped",
108+
expectedFile: "testdata/gauge.txt",
109+
recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
110+
attrs := []attribute.KeyValue{
111+
attribute.Key("A").String("B"),
112+
attribute.Key("C").String("D"),
113+
}
114+
// Valid.
115+
gauge, err := meter.SyncFloat64().UpDownCounter("bar", instrument.WithDescription("a fun little gauge"))
116+
require.NoError(t, err)
117+
gauge.Add(ctx, 100, attrs...)
118+
gauge.Add(ctx, -25, attrs...)
119+
120+
// Invalid, should be dropped.
121+
gauge, err = meter.SyncFloat64().UpDownCounter("invalid.gauge.name")
122+
require.NoError(t, err)
123+
gauge.Add(ctx, 100, attrs...)
124+
125+
counter, err := meter.SyncFloat64().Counter("invalid.counter.name")
126+
require.NoError(t, err)
127+
counter.Add(ctx, 100, attrs...)
128+
129+
histogram, err := meter.SyncFloat64().Histogram("invalid.hist.name")
130+
require.NoError(t, err)
131+
histogram.Record(ctx, 23, attrs...)
132+
},
133+
},
106134
}
107135

108136
for _, tc := range testCases {

0 commit comments

Comments
 (0)