Skip to content

Commit

Permalink
*: refactor meta metrics
Browse files Browse the repository at this point in the history
move meta metrics under the `metrics` package.
  • Loading branch information
rexagod committed Jul 5, 2023
1 parent ef04e2f commit 0dfc332
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
2 changes: 2 additions & 0 deletions pkg/manifests/config.go
Expand Up @@ -31,6 +31,8 @@ import (
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
auditv1 "k8s.io/apiserver/pkg/apis/audit/v1"
"k8s.io/klog/v2"

"github.com/openshift/cluster-monitoring-operator/pkg/metrics"
)

const (
Expand Down
27 changes: 27 additions & 0 deletions pkg/metrics/metrics.go
@@ -0,0 +1,27 @@
package metrics

import (
"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
)

// ReconcileAttempts is a counter that indicates the number of attempts to reconcile the operator configuration.
var ReconcileAttempts = metrics.NewCounter(&metrics.CounterOpts{
Name: "cluster_monitoring_operator_reconcile_attempts_total",
Help: "Number of attempts to reconcile the operator configuration.",
StabilityLevel: metrics.ALPHA,
})

// ReconcileStatus is a gauge that indicates the latest reconciliation state.
var ReconcileStatus = metrics.NewGauge(&metrics.GaugeOpts{
Name: "cluster_monitoring_operator_last_reconciliation_successful",
Help: "Latest reconciliation state. Set to 1 if last reconciliation succeeded, else 0.",
StabilityLevel: metrics.ALPHA,
})

func init() {
// The API (metrics) server is instrumented to work with component-base.
// Refer: https://github.com/kubernetes/kubernetes/blob/ec87834bae787ab6687921d65c3bcfde8a6e01b9/staging/src/k8s.io/apiserver/pkg/server/routes/metrics.go#L44.
legacyregistry.MustRegister(ReconcileAttempts)
legacyregistry.MustRegister(ReconcileStatus)
}
36 changes: 9 additions & 27 deletions pkg/operator/operator.go
Expand Up @@ -22,15 +22,15 @@ import (
"time"

"github.com/blang/semver/v4"
"github.com/openshift/cluster-monitoring-operator/pkg/alert"
"github.com/openshift/cluster-monitoring-operator/pkg/rebalancer"
cmostr "github.com/openshift/cluster-monitoring-operator/pkg/strings"
"github.com/pkg/errors"
certapiv1 "k8s.io/api/certificates/v1"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"

"github.com/openshift/cluster-monitoring-operator/pkg/alert"
"github.com/openshift/cluster-monitoring-operator/pkg/metrics"
"github.com/openshift/cluster-monitoring-operator/pkg/rebalancer"
cmostr "github.com/openshift/cluster-monitoring-operator/pkg/strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -183,9 +183,6 @@ type Operator struct {

queue workqueue.RateLimitingInterface

reconcileAttempts *metrics.Counter
reconcileStatus *metrics.Gauge

failedReconcileAttempts int

assets *manifests.Assets
Expand Down Expand Up @@ -254,22 +251,7 @@ func New(
rebalancer: rebalancer.NewRebalancer(ctx, c.KubernetesInterface()),
ruleController: ruleController,
relabelController: relabelController,
reconcileAttempts: metrics.NewCounter(&metrics.CounterOpts{
Name: "cluster_monitoring_operator_reconcile_attempts_total",
Help: "Number of attempts to reconcile the operator configuration",
StabilityLevel: metrics.ALPHA,
}),
reconcileStatus: metrics.NewGauge(&metrics.GaugeOpts{
Name: "cluster_monitoring_operator_last_reconciliation_successful",
Help: "Latest reconciliation state. Set to 1 if last reconciliation succeeded, else 0.",
StabilityLevel: metrics.ALPHA,
}),
}

legacyregistry.MustRegister(
o.reconcileAttempts,
o.reconcileStatus,
)
}

informer := cache.NewSharedIndexInformer(
o.client.SecretListWatchForNamespace(namespace), &v1.Secret{}, resyncPeriod, cache.Indexers{},
Expand Down Expand Up @@ -650,15 +632,15 @@ func (o *Operator) processNextWorkItem(ctx context.Context) bool {
}
defer o.queue.Done(key)

o.reconcileAttempts.Inc()
metrics.ReconcileAttempts.Inc()
err := o.sync(ctx, key.(string))
if err == nil {
o.reconcileStatus.Set(1)
metrics.ReconcileStatus.Set(1)
o.queue.Forget(key)
return true
}

o.reconcileStatus.Set(0)
metrics.ReconcileStatus.Set(0)
klog.Errorf("Syncing %q failed", key)
utilruntime.HandleError(errors.Wrapf(err, "sync %q failed", key))
o.queue.AddRateLimited(key)
Expand Down

0 comments on commit 0dfc332

Please sign in to comment.