Skip to content

Commit 2ede584

Browse files
Merge pull request #2440 from enxebre/count-down-time-series
OCPBUGS-7841: Set metrics to 0 when needed to keep time series honest
2 parents cf74dad + 2601672 commit 2ede584

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

hypershift-operator/metrics.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func newMetrics(client crclient.Client, log logr.Logger) *hypershiftMetrics {
100100
hostedClustersWithFailureCondition: prometheus.NewGaugeVec(prometheus.GaugeOpts{
101101
Name: "hypershift_hostedclusters_failure_conditions",
102102
Help: "Total number of HostedClusters by platform with conditions in undesired state",
103-
}, []string{"platform", "condition"}),
103+
}, []string{"condition"}),
104104
hostedClustersNodePools: prometheus.NewGaugeVec(prometheus.GaugeOpts{
105105
Name: "hypershift_hostedcluster_nodepools",
106106
Help: "Number of NodePools associated with a given HostedCluster",
@@ -228,8 +228,27 @@ var expectedHCConditionStates = map[hyperv1.ConditionType]bool{
228228

229229
func (m *hypershiftMetrics) observeHostedClusters(hostedClusters *hyperv1.HostedClusterList) {
230230
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+
}
232250

251+
// Init identityProvidersCounter counter.
233252
identityProvidersCounter := map[configv1.IdentityProviderType]float64{
234253
configv1.IdentityProviderTypeBasicAuth: 0,
235254
configv1.IdentityProviderTypeGitHub: 0,
@@ -312,21 +331,21 @@ func (m *hypershiftMetrics) observeHostedClusters(hostedClusters *hyperv1.Hosted
312331
if availableTime != nil {
313332
m.clusterAvailableTime.WithLabelValues(hc.Namespace + "/" + hc.Name).Set(*availableTime)
314333
}
334+
315335
platform := string(hc.Spec.Platform.Type)
316336
hcCount.Add(platform)
337+
317338
for _, cond := range hc.Status.Conditions {
318339
expectedState, known := expectedHCConditionStates[hyperv1.ConditionType(cond.Type)]
319340
if !known {
320341
continue
321342
}
322-
if expectedState {
343+
if expectedState == true {
323344
if cond.Status == metav1.ConditionFalse {
324-
hcByConditions.Add(platform, "not_"+cond.Type)
345+
hcByConditions["not_"+cond.Type] = hcByConditions["not_"+cond.Type] + 1
325346
}
326347
} else {
327-
if cond.Status == metav1.ConditionTrue {
328-
hcByConditions.Add(platform, cond.Type)
329-
}
348+
hcByConditions[cond.Type] = hcByConditions[cond.Type] + 1
330349
}
331350
}
332351

@@ -371,9 +390,9 @@ func (m *hypershiftMetrics) observeHostedClusters(hostedClusters *hyperv1.Hosted
371390
m.hostedClusters.WithLabelValues(labels...).Set(float64(count))
372391
}
373392

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)
377396
}
378397
}
379398

@@ -495,6 +514,11 @@ func newLabelCounter() *labelCounter {
495514
}
496515
}
497516

517+
func (c *labelCounter) Init(values ...string) {
518+
key := strings.Join(values, "|")
519+
c.counts[key] = 0
520+
}
521+
498522
func (c *labelCounter) Add(values ...string) {
499523
key := strings.Join(values, "|")
500524
c.counts[key]++

0 commit comments

Comments
 (0)