Skip to content

Commit

Permalink
add basic info metrics to spin-operator
Browse files Browse the repository at this point in the history
Signed-off-by: Rajat Jindal <[email protected]>
  • Loading branch information
rajatjindal committed Feb 28, 2024
1 parent 6ea5fae commit 2a970a6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
42 changes: 42 additions & 0 deletions internal/controller/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package controller

import (
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

func init() {
registerPrometheusMetrics()
}

func registerPrometheusMetrics() {
metrics.Registry.MustRegister(spinOperatorSpinAppInfo)
metrics.Registry.MustRegister(spinOperatorSpinAppExecutorInfo)
}

var (
spinOperatorSpinAppInfo = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "spin_operator_spinapp_info",
Help: "info about spinapp labeled by name, namespace, executor",
},
[]string{
"name",
"namespace",
"executor",
},
)

spinOperatorSpinAppExecutorInfo = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "spin_operator_spinapp_executor_info",
Help: "info about spinapp executor labeled by name, namespace, create_deployment and runtimeclass",
},
[]string{
"name",
"namespace",
"create_deployment",
"runtimeclass",
},
)
)
8 changes: 8 additions & 0 deletions internal/controller/spinapp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/prometheus/client_golang/prometheus"
spinv1 "github.com/spinkube/spin-operator/api/v1"
"github.com/spinkube/spin-operator/internal/logging"
"github.com/spinkube/spin-operator/pkg/spinapp"
Expand Down Expand Up @@ -90,6 +91,13 @@ func (r *SpinAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, client.IgnoreNotFound(err)
}

// record spin_operator_spinapp_info metric
spinOperatorSpinAppInfo.With(prometheus.Labels{
"name": spinApp.Name,
"namespace": spinApp.Namespace,
"executor": spinApp.Spec.Executor,
}).Set(1)

var executor spinv1.SpinAppExecutor
if err := r.Client.Get(ctx, types.NamespacedName{
// Executors must currently be defined in the same namespace as the app.
Expand Down
15 changes: 15 additions & 0 deletions internal/controller/spinappexecutor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ package controller
import (
"context"
"errors"
"fmt"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/prometheus/client_golang/prometheus"
spinv1 "github.com/spinkube/spin-operator/api/v1"
"github.com/spinkube/spin-operator/internal/logging"
)
Expand Down Expand Up @@ -76,6 +78,19 @@ func (r *SpinAppExecutorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return ctrl.Result{}, client.IgnoreNotFound(err)
}

runtimeClass := ""
if executor.Spec.DeploymentConfig != nil {
runtimeClass = executor.Spec.DeploymentConfig.RuntimeClassName
}

// record spin_operator_spinapp_executor_info metric
spinOperatorSpinAppExecutorInfo.With(prometheus.Labels{
"name": executor.Name,
"namespace": executor.Namespace,
"create_deployment": fmt.Sprintf("%t", executor.Spec.CreateDeployment),
"runtimeclass": runtimeClass,
}).Set(1)

// Make sure the finalizer is present
err := r.ensureFinalizer(ctx, &executor)
return ctrl.Result{}, client.IgnoreNotFound(err)
Expand Down

0 comments on commit 2a970a6

Please sign in to comment.