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