@@ -100,7 +100,7 @@ func newMetrics(client crclient.Client, log logr.Logger) *hypershiftMetrics {
100
100
hostedClustersWithFailureCondition : prometheus .NewGaugeVec (prometheus.GaugeOpts {
101
101
Name : "hypershift_hostedclusters_failure_conditions" ,
102
102
Help : "Total number of HostedClusters by platform with conditions in undesired state" ,
103
- }, []string {"platform" , " condition" }),
103
+ }, []string {"condition" }),
104
104
hostedClustersNodePools : prometheus .NewGaugeVec (prometheus.GaugeOpts {
105
105
Name : "hypershift_hostedcluster_nodepools" ,
106
106
Help : "Number of NodePools associated with a given HostedCluster" ,
@@ -228,8 +228,27 @@ var expectedHCConditionStates = map[hyperv1.ConditionType]bool{
228
228
229
229
func (m * hypershiftMetrics ) observeHostedClusters (hostedClusters * hyperv1.HostedClusterList ) {
230
230
hcCount := newLabelCounter ()
231
- hcByConditions := newLabelCounter ()
231
+ // We init to 0, this is needed so the metrics reports 0 in cases there's no HostedClusters.
232
+ // Otherwise the time series would show the last reported value by the counter.
233
+ hcCount .Init (string (hyperv1 .AWSPlatform ))
234
+ hcCount .Init (string (hyperv1 .NonePlatform ))
235
+ hcCount .Init (string (hyperv1 .IBMCloudPlatform ))
236
+ hcCount .Init (string (hyperv1 .AgentPlatform ))
237
+ hcCount .Init (string (hyperv1 .KubevirtPlatform ))
238
+ hcCount .Init (string (hyperv1 .AzurePlatform ))
239
+ hcCount .Init (string (hyperv1 .PowerVSPlatform ))
240
+
241
+ // Init hcByConditions counter.
242
+ hcByConditions := make (map [string ]float64 )
243
+ for condition , expectedState := range expectedHCConditionStates {
244
+ if expectedState == true {
245
+ hcByConditions [string ("not_" + condition )] = 0
246
+ } else {
247
+ hcByConditions [string (condition )] = 0
248
+ }
249
+ }
232
250
251
+ // Init identityProvidersCounter counter.
233
252
identityProvidersCounter := map [configv1.IdentityProviderType ]float64 {
234
253
configv1 .IdentityProviderTypeBasicAuth : 0 ,
235
254
configv1 .IdentityProviderTypeGitHub : 0 ,
@@ -312,21 +331,21 @@ func (m *hypershiftMetrics) observeHostedClusters(hostedClusters *hyperv1.Hosted
312
331
if availableTime != nil {
313
332
m .clusterAvailableTime .WithLabelValues (hc .Namespace + "/" + hc .Name ).Set (* availableTime )
314
333
}
334
+
315
335
platform := string (hc .Spec .Platform .Type )
316
336
hcCount .Add (platform )
337
+
317
338
for _ , cond := range hc .Status .Conditions {
318
339
expectedState , known := expectedHCConditionStates [hyperv1 .ConditionType (cond .Type )]
319
340
if ! known {
320
341
continue
321
342
}
322
- if expectedState {
343
+ if expectedState == true {
323
344
if cond .Status == metav1 .ConditionFalse {
324
- hcByConditions . Add ( platform , "not_" + cond .Type )
345
+ hcByConditions [ "not_" + cond . Type ] = hcByConditions [ "not_" + cond .Type ] + 1
325
346
}
326
347
} else {
327
- if cond .Status == metav1 .ConditionTrue {
328
- hcByConditions .Add (platform , cond .Type )
329
- }
348
+ hcByConditions [cond .Type ] = hcByConditions [cond .Type ] + 1
330
349
}
331
350
}
332
351
@@ -371,9 +390,9 @@ func (m *hypershiftMetrics) observeHostedClusters(hostedClusters *hyperv1.Hosted
371
390
m .hostedClusters .WithLabelValues (labels ... ).Set (float64 (count ))
372
391
}
373
392
374
- for key , count := range hcByConditions . Counts () {
375
- labels := counterKeyToLabels ( key )
376
- m .hostedClustersWithFailureCondition .WithLabelValues (labels ... ).Set (float64 ( count ) )
393
+ // Collect hcByConditions metric.
394
+ for condition , count := range hcByConditions {
395
+ m .hostedClustersWithFailureCondition .WithLabelValues (condition ).Set (count )
377
396
}
378
397
}
379
398
@@ -495,6 +514,11 @@ func newLabelCounter() *labelCounter {
495
514
}
496
515
}
497
516
517
+ func (c * labelCounter ) Init (values ... string ) {
518
+ key := strings .Join (values , "|" )
519
+ c .counts [key ] = 0
520
+ }
521
+
498
522
func (c * labelCounter ) Add (values ... string ) {
499
523
key := strings .Join (values , "|" )
500
524
c .counts [key ]++
0 commit comments