Skip to content

Commit

Permalink
restructure metrics implementation per review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Rajat Jindal <[email protected]>
  • Loading branch information
rajatjindal committed Feb 29, 2024
1 parent aa7d4c8 commit 3603ab4
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 57 deletions.
13 changes: 8 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

spinv1 "github.com/spinkube/spin-operator/api/v1"
Expand Down Expand Up @@ -102,16 +103,18 @@ func main() {
}

if err = (&controller.SpinAppReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("spinapp-reconciler"),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("spinapp-reconciler"),
MetricsRegistry: metrics.Registry,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "SpinApp")
os.Exit(1)
}
if err = (&controller.SpinAppExecutorReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
MetricsRegistry: metrics.Registry,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "SpinAppExecutor")
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.22.0

require (
github.com/go-logr/logr v1.4.1
github.com/prometheus/client_golang v1.18.0
github.com/prometheus/common v0.48.0
github.com/stretchr/testify v1.8.4
k8s.io/api v0.29.2
Expand Down Expand Up @@ -41,7 +42,6 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
42 changes: 0 additions & 42 deletions internal/controller/metrics.go

This file was deleted.

13 changes: 9 additions & 4 deletions internal/controller/spinapp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/metrics"

"github.com/prometheus/client_golang/prometheus"
spinv1 "github.com/spinkube/spin-operator/api/v1"
Expand All @@ -50,9 +51,11 @@ const (

// SpinAppReconciler reconciles a SpinApp object
type SpinAppReconciler struct {
Client client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
Client client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
MetricsRegistry metrics.RegistererGatherer
metrics *spinAppMetrics
}

//+kubebuilder:rbac:groups=core.spinoperator.dev,resources=spinapps,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -63,6 +66,8 @@ type SpinAppReconciler struct {

// SetupWithManager sets up the controller with the Manager.
func (r *SpinAppReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.setupMetrics()

return ctrl.NewControllerManagedBy(mgr).
For(&spinv1.SpinApp{}).
// Owns allows watching dependency resources for any changes
Expand Down Expand Up @@ -92,7 +97,7 @@ func (r *SpinAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}

// record spin_operator_spinapp_info metric
spinOperatorSpinAppInfo.With(prometheus.Labels{
r.metrics.infoGauge.With(prometheus.Labels{
"name": spinApp.Name,
"namespace": spinApp.Namespace,
"executor": spinApp.Spec.Executor,
Expand Down
8 changes: 5 additions & 3 deletions internal/controller/spinapp_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
)

Expand Down Expand Up @@ -103,9 +104,10 @@ func setupController(t *testing.T) (*envTestState, ctrl.Manager, *SpinAppReconci
require.NoError(t, err)

ctrlr := &SpinAppReconciler{
Client: envTest.k8sClient,
Scheme: envTest.scheme,
Recorder: mgr.GetEventRecorderFor("spinapp-reconciler"),
Client: envTest.k8sClient,
Scheme: envTest.scheme,
Recorder: mgr.GetEventRecorderFor("spinapp-reconciler"),
MetricsRegistry: metrics.Registry,
}

require.NoError(t, ctrlr.SetupWithManager(mgr))
Expand Down
36 changes: 36 additions & 0 deletions internal/controller/spinapp_executor_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package controller

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

type spinAppExecutorMetrics struct {
infoGauge *prometheus.GaugeVec
}

func newSpinAppExecutorMetrics() *spinAppExecutorMetrics {
return &spinAppExecutorMetrics{
infoGauge: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "spin_operator_spinapp_executor_info",
Help: "info about spinapp executor labeled by name, namespace, create_deployment and runtime_class_name",
},
[]string{
"name",
"namespace",
"create_deployment",
"runtime_class_name",
},
),
}
}

func (m *spinAppExecutorMetrics) Register(registry metrics.RegistererGatherer) {
registry.MustRegister(m.infoGauge)
}

func (r *SpinAppExecutorReconciler) setupMetrics() {
r.metrics = newSpinAppExecutorMetrics()
r.metrics.Register(r.MetricsRegistry)
}
35 changes: 35 additions & 0 deletions internal/controller/spinapp_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package controller

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

type spinAppMetrics struct {
infoGauge *prometheus.GaugeVec
}

func newSpinAppMetrics() *spinAppMetrics {
return &spinAppMetrics{
infoGauge: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "spin_operator_spinapp_info",
Help: "info about spinapp labeled by name, namespace, executor",
},
[]string{
"name",
"namespace",
"executor",
},
),
}
}

func (m *spinAppMetrics) Register(registry metrics.RegistererGatherer) {
registry.MustRegister(m.infoGauge)
}

func (r *SpinAppReconciler) setupMetrics() {
r.metrics = newSpinAppMetrics()
r.metrics.Register(r.MetricsRegistry)
}
9 changes: 7 additions & 2 deletions internal/controller/spinappexecutor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/metrics"

"github.com/prometheus/client_golang/prometheus"
spinv1 "github.com/spinkube/spin-operator/api/v1"
Expand All @@ -34,7 +35,9 @@ import (
// SpinAppExecutorReconciler reconciles a SpinAppExecutor object
type SpinAppExecutorReconciler struct {
client.Client
Scheme *runtime.Scheme
Scheme *runtime.Scheme
MetricsRegistry metrics.RegistererGatherer
metrics *spinAppExecutorMetrics
}

//+kubebuilder:rbac:groups=core.spinoperator.dev,resources=spinappexecutors,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -43,6 +46,8 @@ type SpinAppExecutorReconciler struct {

// SetupWithManager sets up the controller with the Manager.
func (r *SpinAppExecutorReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.setupMetrics()

return ctrl.NewControllerManagedBy(mgr).
For(&spinv1.SpinAppExecutor{}).
Complete(r)
Expand Down Expand Up @@ -84,7 +89,7 @@ func (r *SpinAppExecutorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}

// record spin_operator_spinapp_executor_info metric
spinOperatorSpinAppExecutorInfo.With(prometheus.Labels{
r.metrics.infoGauge.With(prometheus.Labels{
"name": executor.Name,
"namespace": executor.Namespace,
"create_deployment": fmt.Sprintf("%t", executor.Spec.CreateDeployment),
Expand Down

0 comments on commit 3603ab4

Please sign in to comment.