Skip to content

Commit

Permalink
Fix prom exporter panic on invalid metric (#3182)
Browse files Browse the repository at this point in the history
Resolves #3180
  • Loading branch information
MrAlias authored Sep 18, 2022
1 parent 30fcf78 commit 798b423
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions exporters/prometheus/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 28 additions & 0 deletions exporters/prometheus/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 798b423

Please sign in to comment.