Skip to content

PrometheusWriter uses global async metric cache shared across instances #1176

@abhijeetsharma200

Description

@abhijeetsharma200

Is your feature request related to a problem? Please describe.

The promAsyncMetricCache in internal/sinks/prometheus.go is a package-level global variable shared across all PrometheusWriter instances:

var promAsyncMetricCache = make(PromMetricCache)
var promAsyncMetricCacheLock = sync.RWMutex{}

This means multiple PrometheusWriter instances (e.g., listening on different ports) would share the same cache, causing metrics to be mixed between scrapers. It also makes testing harder since global state must be manually reset between tests.

**Describe the solution you'd like

Move the cache and lock into the PrometheusWriter struct:

  type PrometheusWriter struct {
      // ... existing fields ...
      asyncMetricCache     PromMetricCache
      asyncMetricCacheLock sync.RWMutex
  }

This properly isolates each instance and follows Go best practices for encapsulation.

**Additional context

Identified during the data race fix in PR #1140. The atomic swap pattern works correctly, but the global cache was noted as technical debt.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions