diff --git a/exporters/prometheus/exporter.go b/exporters/prometheus/exporter.go index c0ade197503..16df309be44 100644 --- a/exporters/prometheus/exporter.go +++ b/exporters/prometheus/exporter.go @@ -190,8 +190,8 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) { ch <- c.targetInfo } - if c.resourceAttributesFilter != nil { - c.resourceAttributes(metrics.Resource) + if c.resourceAttributesFilter != nil && len(c.resourceKeyVals.keys) == 0 { + c.createResourceAttributes(metrics.Resource) } for _, scopeMetrics := range metrics.ScopeMetrics { @@ -492,14 +492,10 @@ func (c *collector) metricType(m metricdata.Metrics) *dto.MetricType { return nil } -func (c *collector) resourceAttributes(res *resource.Resource) { +func (c *collector) createResourceAttributes(res *resource.Resource) { c.mu.Lock() defer c.mu.Unlock() - if len(c.resourceKeyVals.keys) > 0 { - return - } - resourceAttrs, _ := res.Set().Filter(c.resourceAttributesFilter) resourceKeys, resourceValues := getAttrs(resourceAttrs, [2]string{}, [2]string{}, keyVals{}) c.resourceKeyVals = keyVals{keys: resourceKeys, vals: resourceValues}