Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -23,6 +23,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- 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)
- Ensure the `noopSpan.tracerProvider` method is not inlined in `go.opentelemetry.io/otel/trace` so the `go.opentelemetry.io/auto` instrumentation can instrument non-recording spans. (#6456)
- 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
12 changes: 9 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
Comment thread
MrAlias marked this conversation as resolved.
Outdated
}

// 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,9 +150,9 @@ 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)
defer c.metricsPool.Put(metrics)
err := c.reader.Collect(context.TODO(), metrics)
if err != nil {
if errors.Is(err, metric.ErrReaderShutdown) {
return
Expand Down