Skip to content

Commit 098985d

Browse files
committed
improve error handling in prometheus exporter
1 parent 0d5aa14 commit 098985d

File tree

3 files changed

+315
-82
lines changed

3 files changed

+315
-82
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2828
- `WithInstrumentationAttributes` in `go.opentelemetry.io/otel/trace` synchronously de-duplicates the passed attributes instead of delegating it to the returned `TracerOption`. (#7266)
2929
- `WithInstrumentationAttributes` in `go.opentelemetry.io/otel/meter` synchronously de-duplicates the passed attributes instead of delegating it to the returned `MeterOption`. (#7266)
3030
- `WithInstrumentationAttributes` in `go.opentelemetry.io/otel/log` synchronously de-duplicates the passed attributes instead of delegating it to the returned `LoggerOption`. (#7266)
31+
- Improve error handling of dropping data during translation by using `NewInvalidMetric` in `go.opentelemetry.io/otel/exporters/prometheus`. (#7363)
3132

3233
<!-- Released section -->
3334
<!-- Don't change this section unless doing release -->

exporters/prometheus/exporter.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
215215

216216
attrKeys, attrVals, err := getAttrs(scopeMetrics.Scope.Attributes, c.labelNamer)
217217
if err != nil {
218-
otel.Handle(err)
218+
reportError(ch, nil, err)
219219
continue
220220
}
221221
for i := range attrKeys {
@@ -231,18 +231,20 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
231231
for _, m := range scopeMetrics.Metrics {
232232
typ := c.metricType(m)
233233
if typ == nil {
234+
reportError(ch, nil, errors.New("invalid metric type"))
234235
continue
235236
}
236237
name, err := c.getName(m)
237238
if err != nil {
238239
// TODO(#7066): Handle this error better. It's not clear this can be
239240
// reached, bad metric names should / will be caught at creation time.
240-
otel.Handle(err)
241+
reportError(ch, nil, err)
241242
continue
242243
}
243244

244245
drop, help := c.validateMetrics(name, m.Description, typ)
245246
if drop {
247+
reportError(ch, nil, errors.New("invalid metric"))
246248
continue
247249
}
248250

@@ -336,7 +338,7 @@ func addExponentialHistogramMetric[N int64 | float64](
336338
for _, dp := range histogram.DataPoints {
337339
keys, values, err := getAttrs(dp.Attributes, labelNamer)
338340
if err != nil {
339-
otel.Handle(err)
341+
reportError(ch, nil, err)
340342
continue
341343
}
342344
keys = append(keys, kv.keys...)
@@ -348,9 +350,14 @@ func addExponentialHistogramMetric[N int64 | float64](
348350
scale := dp.Scale
349351
if scale < -4 {
350352
// Reject scales below -4 as they cannot be represented in Prometheus
351-
otel.Handle(fmt.Errorf(
352-
"exponential histogram scale %d is below minimum supported scale -4, skipping data point",
353-
scale))
353+
reportError(
354+
ch,
355+
desc,
356+
fmt.Errorf(
357+
"exponential histogram scale %d is below minimum supported scale -4, skipping data point",
358+
scale,
359+
),
360+
)
354361
continue
355362
}
356363

@@ -395,7 +402,7 @@ func addExponentialHistogramMetric[N int64 | float64](
395402
dp.StartTime,
396403
values...)
397404
if err != nil {
398-
otel.Handle(err)
405+
reportError(ch, desc, err)
399406
continue
400407
}
401408
m = addExemplars(m, dp.Exemplars, labelNamer)
@@ -414,7 +421,7 @@ func addHistogramMetric[N int64 | float64](
414421
for _, dp := range histogram.DataPoints {
415422
keys, values, err := getAttrs(dp.Attributes, labelNamer)
416423
if err != nil {
417-
otel.Handle(err)
424+
reportError(ch, nil, err)
418425
continue
419426
}
420427
keys = append(keys, kv.keys...)
@@ -430,7 +437,7 @@ func addHistogramMetric[N int64 | float64](
430437
}
431438
m, err := prometheus.NewConstHistogram(desc, dp.Count, float64(dp.Sum), buckets, values...)
432439
if err != nil {
433-
otel.Handle(err)
440+
reportError(ch, desc, err)
434441
continue
435442
}
436443
m = addExemplars(m, dp.Exemplars, labelNamer)
@@ -454,7 +461,7 @@ func addSumMetric[N int64 | float64](
454461
for _, dp := range sum.DataPoints {
455462
keys, values, err := getAttrs(dp.Attributes, labelNamer)
456463
if err != nil {
457-
otel.Handle(err)
464+
reportError(ch, nil, err)
458465
continue
459466
}
460467
keys = append(keys, kv.keys...)
@@ -463,7 +470,7 @@ func addSumMetric[N int64 | float64](
463470
desc := prometheus.NewDesc(name, m.Description, keys, nil)
464471
m, err := prometheus.NewConstMetric(desc, valueType, float64(dp.Value), values...)
465472
if err != nil {
466-
otel.Handle(err)
473+
reportError(ch, desc, err)
467474
continue
468475
}
469476
// GaugeValues don't support Exemplars at this time
@@ -486,7 +493,7 @@ func addGaugeMetric[N int64 | float64](
486493
for _, dp := range gauge.DataPoints {
487494
keys, values, err := getAttrs(dp.Attributes, labelNamer)
488495
if err != nil {
489-
otel.Handle(err)
496+
reportError(ch, nil, err)
490497
continue
491498
}
492499
keys = append(keys, kv.keys...)
@@ -495,7 +502,7 @@ func addGaugeMetric[N int64 | float64](
495502
desc := prometheus.NewDesc(name, m.Description, keys, nil)
496503
m, err := prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(dp.Value), values...)
497504
if err != nil {
498-
otel.Handle(err)
505+
reportError(ch, desc, err)
499506
continue
500507
}
501508
ch <- m
@@ -713,3 +720,10 @@ func attributesToLabels(attrs []attribute.KeyValue, labelNamer otlptranslator.La
713720
}
714721
return labels, nil
715722
}
723+
724+
func reportError(ch chan<- prometheus.Metric, desc *prometheus.Desc, err error) {
725+
if desc == nil {
726+
desc = prometheus.NewInvalidDesc(err)
727+
}
728+
ch <- prometheus.NewInvalidMetric(desc, err)
729+
}

0 commit comments

Comments
 (0)