Skip to content

Commit

Permalink
prometheus exporter no longer collects metrics after shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Oct 20, 2023
1 parent 960e6ab commit 6593aaf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- `go.opentelemetry.io/otel/bridge/opencensus.NewMetricProducer` returns a `*MetricProducer` struct instead of the metric.Producer interface. (#4583)

### Fixed

- In `go.opentelemetry.op/otel/exporters/prometheus`, the exporter no longer `Collect`s metrics after `Shutdown` is invoked. (#4648)

## [1.19.0/0.42.0/0.0.7] 2023-09-28

This release contains the first stable release of the OpenTelemetry Go [metric SDK].
Expand Down
3 changes: 3 additions & 0 deletions exporters/prometheus/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
metrics := metricdata.ResourceMetrics{}
err := c.reader.Collect(context.TODO(), &metrics)
if err != nil {
if err == metric.ErrReaderShutdown {
return
}
otel.Handle(err)
if err == metric.ErrReaderNotRegistered {
return
Expand Down
34 changes: 34 additions & 0 deletions exporters/prometheus/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package prometheus

import (
"context"
"errors"
"io"
"os"
"sync"
Expand Down Expand Up @@ -835,3 +836,36 @@ func TestIncompatibleMeterName(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, 1, len(errs))
}

func TestShutdownExporter(t *testing.T) {
var handledError error
eh := otel.ErrorHandlerFunc(func(e error) { handledError = errors.Join(handledError, e) })
otel.SetErrorHandler(eh)

ctx := context.Background()
registry := prometheus.NewRegistry()

for i := 0; i < 3; i++ {
exporter, err := New(WithRegisterer(registry))
require.NoError(t, err)
provider := metric.NewMeterProvider(
metric.WithResource(resource.Default()),
metric.WithReader(exporter))
meter := provider.Meter("testmeter")
cnt, err := meter.Int64Counter("foo")
require.NoError(t, err)
cnt.Add(ctx, 100)

// verify that metrics added to a previously shutdown MeterProvider
// do not conflict with metrics added in this loop.
_, err = registry.Gather()
require.NoError(t, err)

// Shutdown should cause future prometheus Gather() calls to no longer
// include metrics from this loop's MeterProvider.
err = provider.Shutdown(ctx)
require.NoError(t, err)
}
// ensure we aren't unneccessarily logging errors from the shutdown MeterProvider
require.NoError(t, handledError)
}

0 comments on commit 6593aaf

Please sign in to comment.