Skip to content

Commit

Permalink
support tortoise_number metric
Browse files Browse the repository at this point in the history
  • Loading branch information
sanposhiho committed Oct 25, 2023
1 parent 27013e7 commit 657cada
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions controllers/tortoise_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
}

0 comments on commit 657cada

Please sign in to comment.