1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00

Fix prom exporter panic on invalid metric (#3182)

Resolves #3180
This commit is contained in:
Tyler Yahn 2022-09-18 07:39:32 -07:00 committed by GitHub
parent 30fcf786c3
commit 798b423dfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -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
}

View File

@ -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 {