From 657cadae83f7f26edda9a26bbf22a1a4a709062c Mon Sep 17 00:00:00 2001 From: Kensei Nakada Date: Thu, 26 Oct 2023 01:27:27 +0900 Subject: [PATCH] support tortoise_number metric --- controllers/tortoise_controller.go | 5 +++++ pkg/metrics/metrics.go | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/controllers/tortoise_controller.go b/controllers/tortoise_controller.go index 3b0c7f9a..17081c75 100644 --- a/controllers/tortoise_controller.go +++ b/controllers/tortoise_controller.go @@ -43,6 +43,7 @@ import ( "github.com/mercari/tortoise/pkg/annotation" "github.com/mercari/tortoise/pkg/deployment" "github.com/mercari/tortoise/pkg/hpa" + "github.com/mercari/tortoise/pkg/metrics" "github.com/mercari/tortoise/pkg/recommender" "github.com/mercari/tortoise/pkg/tortoise" "github.com/mercari/tortoise/pkg/vpa" @@ -87,6 +88,7 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ if apierrors.IsNotFound(err) { // Probably deleted already and finalizer is already removed. logger.V(4).Info("tortoise is not found", "tortoise", req.NamespacedName) + metrics.RecordTortoise(tortoise, true) return ctrl.Result{}, nil } @@ -103,9 +105,12 @@ func (r *TortoiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ if err := r.TortoiseService.RemoveFinalizer(ctx, tortoise); err != nil { return ctrl.Result{}, fmt.Errorf("remove finalizer: %w", err) } + metrics.RecordTortoise(tortoise, true) return ctrl.Result{RequeueAfter: r.Interval}, nil } + metrics.RecordTortoise(tortoise, false) + defer func() { if tortoise == nil { logger.Error(reterr, "get error during the reconciliation, but cannot record the event because tortoise object is nil", "tortoise", req.NamespacedName) diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 9c134921..a26f7716 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -1,6 +1,7 @@ package metrics import ( + "github.com/mercari/tortoise/api/v1beta2" "github.com/prometheus/client_golang/prometheus" "sigs.k8s.io/controller-runtime/pkg/metrics" ) @@ -55,6 +56,11 @@ var ( Name: "proposed_memory_request", Help: "recommended memory request (byte) that tortoises propose", }, []string{"tortoise_name", "namespace", "container_name", "controller_name", "controller_kind"}) + + TortoiseNumber = prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Name: "tortoise_number", + Help: "the number of tortoise", + }, []string{"tortoise_name", "namespace", "controller_name", "controller_kind", "update_mode", "tortoise_phase"}) ) func init() { @@ -72,3 +78,18 @@ func init() { ProposedMemoryRequest, ) } + +func RecordTortoise(t *v1beta2.Tortoise, deleted bool) { + value := 1.0 + if deleted { + value = 0 + } + TortoiseNumber.WithLabelValues( + t.Name, + t.Namespace, + t.Spec.TargetRefs.ScaleTargetRef.Name, + t.Spec.TargetRefs.ScaleTargetRef.Kind, + string(t.Spec.UpdateMode), + string(t.Status.TortoisePhase), + ).Set(value) +}