1
1
package metrics
2
2
3
3
import (
4
+ "sync"
4
5
"time"
5
6
6
7
"github.com/prometheus/client_golang/prometheus"
@@ -199,12 +200,37 @@ var (
199
200
},
200
201
)
201
202
202
- // subscriptionSyncCounters keeps a record of the Prometheus counters emitted by
203
- // Subscription objects. The key of a record is the Subscription name, while the value
204
- // is struct containing label values used in the counter
205
- subscriptionSyncCounters = make (map [string ]subscriptionSyncLabelValues )
203
+ subscriptionSyncCounters = newSubscriptionSyncCounter ()
206
204
)
207
205
206
+ // subscriptionSyncCounter keeps a record of the Prometheus counters emitted by
207
+ // Subscription objects. The key of a record is the Subscription name, while the value
208
+ // is struct containing label values used in the counter. Read and Write access are
209
+ // protected by mutex.
210
+ type subscriptionSyncCounter struct {
211
+ counters map [string ]subscriptionSyncLabelValues
212
+ countersLock sync.RWMutex
213
+ }
214
+
215
+ func newSubscriptionSyncCounter () subscriptionSyncCounter {
216
+ return subscriptionSyncCounter {
217
+ counters : make (map [string ]subscriptionSyncLabelValues ),
218
+ }
219
+ }
220
+
221
+ func (s * subscriptionSyncCounter ) setValues (key string , val subscriptionSyncLabelValues ) {
222
+ s .countersLock .Lock ()
223
+ defer s .countersLock .Unlock ()
224
+ s .counters [key ] = val
225
+ }
226
+
227
+ func (s * subscriptionSyncCounter ) readValues (key string ) (subscriptionSyncLabelValues , bool ) {
228
+ s .countersLock .RLock ()
229
+ defer s .countersLock .RUnlock ()
230
+ val , ok := s .counters [key ]
231
+ return val , ok
232
+ }
233
+
208
234
type subscriptionSyncLabelValues struct {
209
235
installedCSV string
210
236
pkg string
@@ -280,14 +306,15 @@ func EmitSubMetric(sub *operatorsv1alpha1.Subscription) {
280
306
if sub .Spec == nil {
281
307
return
282
308
}
309
+
283
310
SubscriptionSyncCount .WithLabelValues (sub .GetName (), sub .Status .InstalledCSV , sub .Spec .Channel , sub .Spec .Package , string (sub .Spec .InstallPlanApproval )).Inc ()
284
- if _ , present := subscriptionSyncCounters [ sub .GetName ()] ; ! present {
285
- subscriptionSyncCounters [ sub .GetName ()] = subscriptionSyncLabelValues {
311
+ if _ , present := subscriptionSyncCounters . readValues ( sub .GetName ()) ; ! present {
312
+ subscriptionSyncCounters . setValues ( sub .GetName (), subscriptionSyncLabelValues {
286
313
installedCSV : sub .Status .InstalledCSV ,
287
314
pkg : sub .Spec .Package ,
288
315
channel : sub .Spec .Channel ,
289
316
approvalStrategy : string (sub .Spec .InstallPlanApproval ),
290
- }
317
+ })
291
318
}
292
319
}
293
320
@@ -302,7 +329,7 @@ func UpdateSubsSyncCounterStorage(sub *operatorsv1alpha1.Subscription) {
302
329
if sub .Spec == nil {
303
330
return
304
331
}
305
- counterValues := subscriptionSyncCounters [ sub .GetName ()]
332
+ counterValues , _ := subscriptionSyncCounters . readValues ( sub .GetName ())
306
333
approvalStrategy := string (sub .Spec .InstallPlanApproval )
307
334
308
335
if sub .Spec .Channel != counterValues .channel ||
@@ -317,7 +344,7 @@ func UpdateSubsSyncCounterStorage(sub *operatorsv1alpha1.Subscription) {
317
344
counterValues .channel = sub .Spec .Channel
318
345
counterValues .approvalStrategy = approvalStrategy
319
346
320
- subscriptionSyncCounters [ sub .GetName ()] = counterValues
347
+ subscriptionSyncCounters . setValues ( sub .GetName (), counterValues )
321
348
}
322
349
}
323
350
0 commit comments