-
Notifications
You must be signed in to change notification settings - Fork 126
PrometheusWriter uses global async metric cache shared across instances #1176
Description
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.