@@ -108,8 +108,10 @@ func (promw *PrometheusWriter) Write(msg metrics.MeasurementEnvelope) error {
108108 return nil
109109}
110110
111+ type PromMetricCache = map [string ]map [string ]metrics.MeasurementEnvelope // [dbUnique][metric]lastly_fetched_data
112+
111113// Async Prom cache
112- var promAsyncMetricCache = make (map [ string ] map [ string ]metrics. MeasurementEnvelope ) // [dbUnique][metric]lastly_fetched_data
114+ var promAsyncMetricCache = make (PromMetricCache )
113115var promAsyncMetricCacheLock = sync.RWMutex {}
114116
115117func (promw * PrometheusWriter ) PromAsyncCacheAddMetricData (dbUnique , metric string , msgArr metrics.MeasurementEnvelope ) { // cache structure: [dbUnique][metric]lastly_fetched_data
@@ -124,8 +126,7 @@ func (promw *PrometheusWriter) PromAsyncCacheInitIfRequired(dbUnique, _ string)
124126 promAsyncMetricCacheLock .Lock ()
125127 defer promAsyncMetricCacheLock .Unlock ()
126128 if _ , ok := promAsyncMetricCache [dbUnique ]; ! ok {
127- metricMap := make (map [string ]metrics.MeasurementEnvelope )
128- promAsyncMetricCache [dbUnique ] = metricMap
129+ promAsyncMetricCache [dbUnique ] = make (map [string ]metrics.MeasurementEnvelope )
129130 }
130131}
131132
@@ -159,15 +160,21 @@ func (promw *PrometheusWriter) Collect(ch chan<- prometheus.Metric) {
159160 promw .totalScrapes .Add (1 )
160161 ch <- promw .totalScrapes
161162
163+ promAsyncMetricCacheLock .Lock ()
162164 if len (promAsyncMetricCache ) == 0 {
165+ promAsyncMetricCacheLock .Unlock ()
163166 promw .logger .Warning ("No dbs configured for monitoring. Check config" )
164167 ch <- promw .totalScrapeFailures
165168 promw .lastScrapeErrors .Set (0 )
166169 ch <- promw .lastScrapeErrors
167170 return
168171 }
172+ snapshot := promAsyncMetricCache
173+ promAsyncMetricCache = make (PromMetricCache , len (snapshot ))
174+ promAsyncMetricCacheLock .Unlock ()
175+
169176 t1 := time .Now ()
170- for dbname , metricsMessages := range promAsyncMetricCache {
177+ for _ , metricsMessages := range snapshot {
171178 for metric , metricMessages := range metricsMessages {
172179 if metric == "change_events" {
173180 continue // not supported
@@ -178,9 +185,6 @@ func (promw *PrometheusWriter) Collect(ch chan<- prometheus.Metric) {
178185 ch <- pm
179186 }
180187 }
181- promAsyncMetricCacheLock .Lock ()
182- promAsyncMetricCache [dbname ] = make (map [string ]metrics.MeasurementEnvelope ) // clear the cache for this db after metrics are collected
183- promAsyncMetricCacheLock .Unlock ()
184188 }
185189 promw .logger .WithField ("count" , rows ).WithField ("elapsed" , time .Since (t1 )).Info ("measurements written" )
186190 ch <- promw .totalScrapeFailures
0 commit comments