Skip to content

Commit

Permalink
added metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mfaizanse committed Apr 23, 2024
1 parent b8f811a commit c7de08a
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main //nolint:cyclop // main function needs to initialize many objects

import (
"flag"
"github.com/kyma-project/nats-manager/internal/metrics"
"os"

nmapiv1alpha1 "github.com/kyma-project/nats-manager/api/v1alpha1"
Expand Down Expand Up @@ -150,6 +151,9 @@ func main() { //nolint:funlen // main function needs to initialize many objects

natsManager := nmmgr.NewNATSManger(kubeClient, helmRenderer, sugaredLogger)

collector := metrics.NewPrometheusCollector()
collector.RegisterMetrics()

// create NATS reconciler instance
natsReconciler := nmctrl.NewReconciler(
mgr.GetClient(),
Expand All @@ -165,6 +169,7 @@ func main() { //nolint:funlen // main function needs to initialize many objects
Namespace: envConfigs.NATSCRNamespace,
},
},
collector,
)

if err = (natsReconciler).SetupWithManager(mgr); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/nats/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package nats
import (
"context"
"fmt"
"github.com/kyma-project/nats-manager/internal/metrics"

nmapiv1alpha1 "github.com/kyma-project/nats-manager/api/v1alpha1"
"github.com/kyma-project/nats-manager/pkg/events"
Expand Down Expand Up @@ -70,6 +71,7 @@ type Reconciler struct {
ctrlManager kcontrollerruntime.Manager
destinationRuleWatchStarted bool
allowedNATSCR *nmapiv1alpha1.NATS
collector metrics.Collector
}

func NewReconciler(
Expand All @@ -81,6 +83,7 @@ func NewReconciler(
recorder record.EventRecorder,
natsManager nmmgr.Manager,
allowedNATSCR *nmapiv1alpha1.NATS,
collector metrics.Collector,
) *Reconciler {
return &Reconciler{
Client: client,
Expand All @@ -93,6 +96,7 @@ func NewReconciler(
natsManager: natsManager,
destinationRuleWatchStarted: false,
allowedNATSCR: allowedNATSCR,
collector: collector,
controller: nil,
}
}
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/nats/deprovisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const (
func (r *Reconciler) handleNATSDeletion(ctx context.Context, nats *nmapiv1alpha1.NATS,
log *zap.SugaredLogger,
) (kcontrollerruntime.Result, error) {
// reset metrics.
r.collector.ResetAvailabilityZonesUsedMetric()
r.collector.ResetClusterSizeMetric()

// skip reconciliation for deletion if the finalizer is not set.
if !r.containsFinalizer(nats) {
log.Debugf("skipped reconciliation for deletion as finalizer is not set.")
Expand Down
6 changes: 6 additions & 0 deletions internal/controller/nats/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func (r *Reconciler) handleNATSReconcile(ctx context.Context,
) (kcontrollerruntime.Result, error) {
log.Info("handling NATS reconciliation...")

// record metric.
r.collector.RecordClusterSizeMetric(nats.Spec.Cluster.Size)

// set status to processing
nats.Status.Initialize()
events.Normal(r.recorder, nats, nmapiv1alpha1.ConditionReasonProcessing, "Initializing NATS resource.")
Expand Down Expand Up @@ -101,6 +104,9 @@ func (r *Reconciler) handleNATSState(ctx context.Context, nats *nmapiv1alpha1.NA
nats.Status.AvailabilityZonesUsed, err = r.kubeClient.GetNumberOfAvailabilityZonesUsedByPods(ctx,
nats.GetNamespace(), getNATSPodsMatchLabels())

// record metric.
r.collector.RecordAvailabilityZonesUsedMetric(nats.Status.AvailabilityZonesUsed)

switch {
case err != nil:
nats.Status.UpdateConditionAvailabilityZones(kmetav1.ConditionFalse,
Expand Down
5 changes: 5 additions & 0 deletions internal/controller/nats/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nats

import (
"context"
"github.com/kyma-project/nats-manager/internal/metrics"
"testing"

nmapiv1alpha1 "github.com/kyma-project/nats-manager/api/v1alpha1"
Expand Down Expand Up @@ -60,6 +61,9 @@ func NewMockedUnitTestEnvironment(t *testing.T, objs ...client.Object) *MockedUn
mockController := new(nmctrlmocks.Controller)
mockManager := new(nmctrlmocks.Manager)

// setup mocks.
collector := metrics.NewPrometheusCollector()

// setup reconciler
reconciler := NewReconciler(
fakeClient,
Expand All @@ -70,6 +74,7 @@ func NewMockedUnitTestEnvironment(t *testing.T, objs ...client.Object) *MockedUn
recorder,
natsManager,
nil,
collector,
)
reconciler.controller = mockController
reconciler.ctrlManager = mockManager
Expand Down
90 changes: 90 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package metrics

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

const (
// availabilityZonesUsedMetricKey name of the availability zones used metric.
availabilityZonesUsedMetricKey = "nats_manager_availability_zones_used"
// availabilityZonesUsedHelp help text for the availability zones used metric.
availabilityZonesUsedHelp = "The number of availability zones used used by NATS Pods."

// clusterSizeMetricKey name of the cluster size metric.
clusterSizeMetricKey = "nats_manager_cr_cluster_size"
// clusterSizeMetricHelp help text for the cluster size metric.
clusterSizeMetricHelp = "The cluster size configured in the NATS CR."
)

// Perform a compile time check.
var _ Collector = &PrometheusCollector{}

//go:generate go run github.com/vektra/mockery/v2 --name=Collector --outpkg=mocks --case=underscore
type Collector interface {
RegisterMetrics()
RecordAvailabilityZonesUsedMetric(int)
RecordClusterSizeMetric(int)
ResetAvailabilityZonesUsedMetric()
ResetClusterSizeMetric()
}

// PrometheusCollector implements the prometheus.Collector interface.
type PrometheusCollector struct {
availabilityZonesUsed *prometheus.GaugeVec
clusterSize *prometheus.GaugeVec
}

// NewPrometheusCollector a new instance of Collector.
func NewPrometheusCollector() Collector {
return &PrometheusCollector{
availabilityZonesUsed: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: availabilityZonesUsedMetricKey,
Help: availabilityZonesUsedHelp,
},
nil,
),
clusterSize: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: clusterSizeMetricKey,
Help: clusterSizeMetricHelp,
},
nil,
),
}
}

// Describe implements the prometheus.Collector interface Describe method.
func (p *PrometheusCollector) Describe(ch chan<- *prometheus.Desc) {
p.availabilityZonesUsed.Describe(ch)
p.clusterSize.Describe(ch)
}

// Collect implements the prometheus.Collector interface Collect method.
func (p *PrometheusCollector) Collect(ch chan<- prometheus.Metric) {
p.availabilityZonesUsed.Collect(ch)
p.clusterSize.Collect(ch)
}

// RegisterMetrics registers the metrics.
func (p *PrometheusCollector) RegisterMetrics() {
metrics.Registry.MustRegister(p.availabilityZonesUsed)
metrics.Registry.MustRegister(p.clusterSize)
}

func (p *PrometheusCollector) RecordAvailabilityZonesUsedMetric(availabilityZonesUsed int) {
p.availabilityZonesUsed.WithLabelValues().Set(float64(availabilityZonesUsed))
}

func (p *PrometheusCollector) RecordClusterSizeMetric(clusterSize int) {
p.clusterSize.WithLabelValues().Set(float64(clusterSize))
}

func (p *PrometheusCollector) ResetAvailabilityZonesUsedMetric() {
p.availabilityZonesUsed.Reset()
}

func (p *PrometheusCollector) ResetClusterSizeMetric() {
p.clusterSize.Reset()
}
Loading

0 comments on commit c7de08a

Please sign in to comment.