Skip to content

prometheus: use a sync.Pool instead of allocating metricdata.ResourceMetrics in Collect #6472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixes

- Stop percent encoding header environment variables in `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc` and `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#6392)
- Use a `sync.Pool` instead of allocating `metricdata.ResourceMetrics` in `go.opentelemetry.io/otel/exporters/prometheus`. (#6472)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down
13 changes: 10 additions & 3 deletions exporters/prometheus/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type collector struct {
scopeInfosInvalid map[instrumentation.Scope]struct{}
metricFamilies map[string]*dto.MetricFamily
resourceKeyVals keyVals
metricsPool sync.Pool
}

// prometheus counters MUST have a _total suffix by default:
Expand All @@ -118,6 +119,11 @@ func New(opts ...Option) (*Exporter, error) {
metricFamilies: make(map[string]*dto.MetricFamily),
namespace: cfg.namespace,
resourceAttributesFilter: cfg.resourceAttributesFilter,
metricsPool: sync.Pool{
New: func() interface{} {
return &metricdata.ResourceMetrics{}
},
},
}

if err := cfg.registerer.Register(collector); err != nil {
Expand All @@ -144,10 +150,10 @@ func (c *collector) Describe(ch chan<- *prometheus.Desc) {
//
// This method is safe to call concurrently.
func (c *collector) Collect(ch chan<- prometheus.Metric) {
// TODO (#3047): Use a sync.Pool instead of allocating metrics every Collect.
metrics := metricdata.ResourceMetrics{}
err := c.reader.Collect(context.TODO(), &metrics)
metrics := c.metricsPool.Get().(*metricdata.ResourceMetrics)
err := c.reader.Collect(context.TODO(), metrics)
if err != nil {
c.metricsPool.Put(metrics)
if errors.Is(err, metric.ErrReaderShutdown) {
return
}
Expand Down Expand Up @@ -244,6 +250,7 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
}
}
}
c.metricsPool.Put(metrics)
}

func addHistogramMetric[N int64 | float64](ch chan<- prometheus.Metric, histogram metricdata.Histogram[N], m metricdata.Metrics, name string, kv keyVals) {
Expand Down
Loading