Skip to content

Commit

Permalink
Refactor monitoring metrics
Browse files Browse the repository at this point in the history
Signed-off-by: João Vilaça <[email protected]>
  • Loading branch information
machadovilaca committed Dec 19, 2023
1 parent 5428531 commit 6e6cb5b
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 476 deletions.
7 changes: 4 additions & 3 deletions controllers/ssp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"kubevirt.io/ssp-operator/internal/controller/predicates"
crd_watch "kubevirt.io/ssp-operator/internal/crd-watch"
"kubevirt.io/ssp-operator/internal/operands"
"kubevirt.io/ssp-operator/pkg/monitoring/metrics/ssp-operator"
)

const (
Expand Down Expand Up @@ -128,7 +129,7 @@ func (r *sspReconciler) setupController(mgr ctrl.Manager) error {
func (r *sspReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ctrl.Result, err error) {
defer func() {
if err != nil {
common.SSPOperatorReconcileSucceeded.Set(0)
metrics.SetSspOperatorReconcileSucceeded(false)
}
}()
reqLogger := r.log.WithValues("ssp", req.NamespacedName)
Expand Down Expand Up @@ -221,9 +222,9 @@ func (r *sspReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res ct
sspRequest.Logger.Info("CR status updated")

if sspRequest.Instance.Status.Phase == lifecycleapi.PhaseDeployed {
common.SSPOperatorReconcileSucceeded.Set(1)
metrics.SetSspOperatorReconcileSucceeded(true)
} else {
common.SSPOperatorReconcileSucceeded.Set(0)
metrics.SetSspOperatorReconcileSucceeded(false)
}

return ctrl.Result{}, nil
Expand Down
2 changes: 1 addition & 1 deletion controllers/vm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"kubevirt.io/ssp-operator/pkg/monitoring/metrics"
"kubevirt.io/ssp-operator/pkg/monitoring/metrics/ssp-operator"
)

// +kubebuilder:rbac:groups=kubevirt.io,resources=virtualmachines,verbs=get;list;watch
Expand Down
8 changes: 0 additions & 8 deletions internal/common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
routev1 "github.com/openshift/api/route/v1"
libhandler "github.com/operator-framework/operator-lib/handler"
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/prometheus/client_golang/prometheus"
tekton "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
Expand Down Expand Up @@ -114,13 +113,6 @@ type reconcileBuilder struct {

var _ ReconcileBuilder = &reconcileBuilder{}

var (
SSPOperatorReconcileSucceeded = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "kubevirt_ssp_operator_reconcile_succeeded",
Help: "Set to 1 if the reconcile process of all operands completes with no errors, and to 0 otherwise",
})
)

func (r *reconcileBuilder) NamespacedResource(resource client.Object) ReconcileBuilder {
r.resource = resource
r.isClusterResource = false
Expand Down
11 changes: 2 additions & 9 deletions internal/operands/common-templates/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,10 @@ import (

"github.com/blang/semver/v4"
"github.com/go-logr/logr"
"github.com/prometheus/client_golang/prometheus"

"kubevirt.io/ssp-operator/internal/common"
"kubevirt.io/ssp-operator/internal/operands"
)

var (
CommonTemplatesRestored = prometheus.NewCounter(prometheus.CounterOpts{
Name: "kubevirt_ssp_common_templates_restored_total",
Help: "The total number of common templates restored by the operator back to their original state",
})
metrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics/template-validator"
)

// Define RBAC rules needed by this operand:
Expand Down Expand Up @@ -109,7 +102,7 @@ func incrementTemplatesRestoredMetric(reconcileResults []common.ReconcileResult,

if reconcileResult.OperationResult == common.OperationResultUpdated && oldVersion == newVersion {
logger.Info(fmt.Sprintf("Changes reverted in common template: %s", reconcileResult.Resource.GetName()))
CommonTemplatesRestored.Inc()
metrics.IncCommonTemplatesRestored()
}
}
}
Expand Down
35 changes: 11 additions & 24 deletions internal/operands/common-templates/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

templatev1 "github.com/openshift/api/template/v1"
libhandler "github.com/operator-framework/operator-lib/handler"
"github.com/prometheus/client_golang/prometheus"
io_prometheus_client "github.com/prometheus/client_model/go"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
lifecycleapi "kubevirt.io/controller-lifecycle-operator-sdk/api"
Expand All @@ -24,6 +22,7 @@ import (
"kubevirt.io/ssp-operator/internal/common"
"kubevirt.io/ssp-operator/internal/operands"
. "kubevirt.io/ssp-operator/internal/test-utils"
metrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics/template-validator"
)

var log = logf.Log.WithName("common-templates-operand")
Expand Down Expand Up @@ -98,8 +97,8 @@ var _ = Describe("Common-Templates operand", func() {
ExpectResourceExists(&template, request)
}

desc, value := getCommonTemplatesRestoredMetric()
Expect(desc).To(ContainSubstring("kubevirt_ssp_common_templates_restored_total"))
value, err := metrics.GetCommonTemplatesRestored()
Expect(err).ToNot(HaveOccurred())
Expect(value).To(BeZero())
})

Expand Down Expand Up @@ -131,8 +130,8 @@ var _ = Describe("Common-Templates operand", func() {
Expect(template.Labels).To(HaveKey(testLabel))
}

desc, value := getCommonTemplatesRestoredMetric()
Expect(desc).To(ContainSubstring("kubevirt_ssp_common_templates_restored_total"))
value, err := metrics.GetCommonTemplatesRestored()
Expect(err).ToNot(HaveOccurred())
Expect(value).To(Equal(float64(len(testTemplates))))
})

Expand Down Expand Up @@ -289,8 +288,8 @@ var _ = Describe("Common-Templates operand", func() {
template = getTemplate(request, &testTemplates[0])
template.Namespace = namespace

desc, value := getCommonTemplatesRestoredMetric()
Expect(desc).To(ContainSubstring("kubevirt_ssp_common_templates_restored_total"))
value, err := metrics.GetCommonTemplatesRestored()
Expect(err).ToNot(HaveOccurred())
initialMetricValue = value
})

Expand All @@ -305,8 +304,8 @@ var _ = Describe("Common-Templates operand", func() {
updatedTpl := getTemplate(request, template)
Expect(updatedTpl.Labels[TemplateTypeLabel]).To(Equal(testTemplates[0].Labels[TemplateTypeLabel]))

desc, value := getCommonTemplatesRestoredMetric()
Expect(desc).To(ContainSubstring("kubevirt_ssp_common_templates_restored_total"))
value, err := metrics.GetCommonTemplatesRestored()
Expect(err).ToNot(HaveOccurred())
Expect(value).To(Equal(initialMetricValue + 1))
})

Expand All @@ -322,8 +321,8 @@ var _ = Describe("Common-Templates operand", func() {
updatedTpl := getTemplate(request, template)
Expect(updatedTpl.Labels[TemplateTypeLabel]).To(Equal(testTemplates[0].Labels[TemplateTypeLabel]))

desc, value := getCommonTemplatesRestoredMetric()
Expect(desc).To(ContainSubstring("kubevirt_ssp_common_templates_restored_total"))
value, err := metrics.GetCommonTemplatesRestored()
Expect(err).ToNot(HaveOccurred())
Expect(value).To(Equal(initialMetricValue))
})
})
Expand Down Expand Up @@ -355,18 +354,6 @@ func getTestTemplates() []templatev1.Template {
}}
}

func getCommonTemplatesRestoredMetric() (string, float64) {
ch := make(chan prometheus.Metric, 1)
CommonTemplatesRestored.Collect(ch)
close(ch)
m := <-ch
metric := &io_prometheus_client.Metric{}
err := m.Write(metric)
Expect(err).ToNot(HaveOccurred())

return m.Desc().String(), metric.GetCounter().GetValue()
}

func getTemplate(req common.Request, template *templatev1.Template) *templatev1.Template {
key := client.ObjectKeyFromObject(template)
updatedTpl := &templatev1.Template{}
Expand Down
5 changes: 5 additions & 0 deletions internal/template-validator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import (

"kubevirt.io/ssp-operator/internal/template-validator/service"
"kubevirt.io/ssp-operator/internal/template-validator/validator"
validatorMetrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics/template-validator"
)

func Main() int {
app := &validator.App{}
service.Setup(app)

// setup monitoring
validatorMetrics.SetupMetrics()

app.Run()
return 0
}
Expand Down
12 changes: 2 additions & 10 deletions internal/template-validator/webhooks/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"net/http"
"strings"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
admissionv1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -34,13 +32,7 @@ import (
"kubevirt.io/ssp-operator/internal/template-validator/labels"
"kubevirt.io/ssp-operator/internal/template-validator/logger"
"kubevirt.io/ssp-operator/internal/template-validator/virtinformers"
)

var (
templateValidatorRejected = promauto.NewCounter(prometheus.CounterOpts{
Name: "kubevirt_ssp_template_validator_rejected_total",
Help: "The total number of rejected template validators",
})
"kubevirt.io/ssp-operator/pkg/monitoring/metrics/template-validator"
)

const (
Expand Down Expand Up @@ -102,7 +94,7 @@ func (w *webhooks) admitVm(ar *admissionv1.AdmissionReview) *admissionv1.Admissi

causes := ValidateVm(rules, vm)
if len(causes) > 0 {
templateValidatorRejected.Inc()
metrics.IncTemplateValidatorRejected()
return ToAdmissionResponse(causes)
}

Expand Down
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ import (
ssp "kubevirt.io/ssp-operator/api/v1beta2"
"kubevirt.io/ssp-operator/controllers"
"kubevirt.io/ssp-operator/internal/common"
common_templates "kubevirt.io/ssp-operator/internal/operands/common-templates"
sspMetrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics"
sspMetrics "kubevirt.io/ssp-operator/pkg/monitoring/metrics/ssp-operator"
"kubevirt.io/ssp-operator/webhooks"
// +kubebuilder:scaffold:imports
)
Expand Down Expand Up @@ -202,8 +201,8 @@ func main() {
opts := zap.Options{}
opts.BindFlags(flag.CommandLine)
flag.Parse()
metrics.Registry.MustRegister(common_templates.CommonTemplatesRestored)
metrics.Registry.MustRegister(common.SSPOperatorReconcileSucceeded)

// setup monitoring
sspMetrics.SetupMetrics()

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@ import (
runtimemetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
)

const metricPrefix = "kubevirt_ssp_"

var (
metrics = [][]operatormetrics.Metric{
rbdMetrics,
}
)

func SetupMetrics() {
operatormetrics.Register = runtimemetrics.Registry.Register

if err := operatormetrics.RegisterMetrics(metrics...); err != nil {
if err := operatormetrics.RegisterMetrics(
operatorMetrics,
rbdMetrics,
); err != nil {
panic(err)
}
}

func ListMetrics() []operatormetrics.Metric {
return operatormetrics.ListMetrics()
}
24 changes: 24 additions & 0 deletions pkg/monitoring/metrics/ssp-operator/operator_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package metrics

import "github.com/machadovilaca/operator-observability/pkg/operatormetrics"

var (
operatorMetrics = []operatormetrics.Metric{
sspOperatorReconcileSucceeded,
}

sspOperatorReconcileSucceeded = operatormetrics.NewGauge(
operatormetrics.MetricOpts{
Name: "kubevirt_ssp_operator_reconcile_succeeded",
Help: "Set to 1 if the reconcile process of all operands completes with no errors, and to 0 otherwise",
},
)
)

func SetSspOperatorReconcileSucceeded(isSucceeded bool) {
value := 0.0
if isSucceeded {
value = 1.0
}
sspOperatorReconcileSucceeded.Set(value)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (

vmRbdVolume = operatormetrics.NewGaugeVec(
operatormetrics.MetricOpts{
Name: metricPrefix + "vm_rbd_volume",
Name: "kubevirt_ssp_vm_rbd_volume",
Help: "VM with RBD mounted volume",
ExtraFields: map[string]string{
"StabilityLevel": "ALPHA",
Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions pkg/monitoring/metrics/template-validator/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package metrics

import (
"github.com/machadovilaca/operator-observability/pkg/operatormetrics"
runtimemetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
)

func SetupMetrics() {
operatormetrics.Register = runtimemetrics.Registry.Register

if err := operatormetrics.RegisterMetrics(
templateMetrics,
); err != nil {
panic(err)
}
}
44 changes: 44 additions & 0 deletions pkg/monitoring/metrics/template-validator/template_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package metrics

import (
"github.com/machadovilaca/operator-observability/pkg/operatormetrics"
ioprometheusclient "github.com/prometheus/client_model/go"
)

var (
templateMetrics = []operatormetrics.Metric{
commonTemplatesRestored,
templateValidatorRejected,
}

commonTemplatesRestored = operatormetrics.NewCounter(
operatormetrics.MetricOpts{
Name: "kubevirt_ssp_common_templates_restored_total",
Help: "The total number of common templates restored by the operator back to their original state",
},
)

templateValidatorRejected = operatormetrics.NewCounter(
operatormetrics.MetricOpts{
Name: "kubevirt_ssp_template_validator_rejected_total",
Help: "The total number of rejected template validators",
},
)
)

func IncCommonTemplatesRestored() {
commonTemplatesRestored.Inc()
}

func GetCommonTemplatesRestored() (float64, error) {
dto := &ioprometheusclient.Metric{}
err := commonTemplatesRestored.Write(dto)
if err != nil {
return 0, err
}
return dto.Counter.GetValue(), nil
}

func IncTemplateValidatorRejected() {
templateValidatorRejected.Inc()
}
Loading

0 comments on commit 6e6cb5b

Please sign in to comment.