Skip to content

Commit

Permalink
Improve metrics (#81)
Browse files Browse the repository at this point in the history
<!-- Thank you for your contribution. Before you submit the pull
request:
1. Follow contributing guidelines, templates, the recommended Git
workflow, and any related documentation.
2. Read and submit the required Contributor Licence Agreements
(https://github.com/kyma-project/community/blob/main/CONTRIBUTING.md#agreements-and-licenses).
3. Test your changes and attach their results to the pull request.
4. Update the relevant documentation.

If the pull request requires a decision, follow the [decision-making
process](https://github.com/kyma-project/community/blob/main/governance.md)
and replace the PR template with the [decision record
template](https://github.com/kyma-project/community/blob/main/.github/ISSUE_TEMPLATE/decision-record.md).
-->

**Description**

Changes proposed in this pull request:

- Add statuses
- Refactor

**Related issue(s)**
<!-- If you refer to a particular issue, provide its number. For
example, `Resolves #123`, `Fixes #43`, or `See also #33`. -->
#28
  • Loading branch information
kyma-bot authored Jan 2, 2024
2 parents 5524db5 + 092c00c commit 000a224
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 49 deletions.
45 changes: 26 additions & 19 deletions controllers/compassmanager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/kyma-project/compass-manager/api/v1beta1"
"github.com/kyma-project/compass-manager/controllers/metrics"
s "github.com/kyma-project/compass-manager/controllers/status"
kyma "github.com/kyma-project/lifecycle-manager/api/v1beta2"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -177,15 +178,15 @@ func (cm *CompassManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
if err != nil {
return ctrl.Result{}, errors.Wrap(err, "failed to obtain Compass Manager Mapping for status checks")
}
status := statusNumber(mapping.Status)
status := s.Number(mapping.Status)

if status == Empty {
return cm.setStatusAndRequeue(req.NamespacedName, Processing)
if status == s.Empty {
return cm.setStatusAndRequeue(req.NamespacedName, s.Processing)
}

if status&(Failed) != 0 {
status &= ^Failed
return cm.setStatusAndRequeue(req.NamespacedName, status|Processing)
if status&(s.Failed) != 0 {
status &= ^s.Failed
return cm.setStatusAndRequeue(req.NamespacedName, status|s.Processing)
}

// From this point we will always deal with Compass Manager Mapping for KymaCR
Expand All @@ -194,8 +195,9 @@ func (cm *CompassManagerReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return cm.registerRuntimeInCompassAndRequeue(req.NamespacedName, kymaCR.Labels)
}

if status&(Registered|Processing) != Registered|Processing {
return cm.setStatusAndRequeue(req.NamespacedName, Registered|Processing)
if status&(s.Registered|s.Processing) != s.Registered|s.Processing {
cm.metrics.UpdateState(req.Name, s.Registered|s.Processing)
return cm.setStatusAndRequeue(req.NamespacedName, s.Registered|s.Processing)
}

// From that moment we will always deal with Compass Manager Mapping with ID of registered Runtime, or feature flag is disabled
Expand Down Expand Up @@ -230,7 +232,9 @@ func (cm *CompassManagerReconciler) handleKymaDeletion(name types.NamespacedName
cm.Log.Warnf("Failed to deregister Runtime from Compass for Kyma Resource %s: %v", name.Name, err)
return errors.Wrap(&DirectorError{message: err}, "failed to deregister Runtime from Compass")
}
cm.metrics.IncUnregister()
cm.metrics.IncUnregister(name.Name)
cm.metrics.UpdateState(name.Name, s.Empty)

cm.Log.Infof("Runtime %s deregistered from Compass", name.Name)
} else {
cm.Log.Infof("Runtime was not connected in Compass, deleting without deregistering")
Expand Down Expand Up @@ -271,7 +275,7 @@ func (cm *CompassManagerReconciler) registerRuntimeInCompassAndRequeue(kymaName

if regError != nil {
cm.Log.Errorf("Failed attempt to register runtime for Kyma resource: %s: %v", kymaName.Name, regError)
statErr := cm.cluster.SetCompassMappingStatus(kymaName, Failed)
statErr := cm.cluster.SetCompassMappingStatus(kymaName, s.Failed)

if statErr != nil {
return ctrl.Result{Requeue: true}, errors.Wrap(statErr, "failed to set Compass Manager Status after failed attempt to register runtime")
Expand All @@ -280,7 +284,9 @@ func (cm *CompassManagerReconciler) registerRuntimeInCompassAndRequeue(kymaName
return ctrl.Result{Requeue: true}, errors.Wrapf(regError, "failed attempt to register runtime for Kyma resource: %s", kymaName.Name)
}

cm.metrics.IncRegister()
cm.metrics.IncRegister(kymaName.Name)
cm.metrics.UpdateState(kymaName.Name, s.Registered|s.Processing)

cm.Log.Infof("Runtime %s registered in Compass", newCompassRuntimeID)
cmerr := cm.cluster.UpsertCompassMapping(kymaName, newCompassRuntimeID)
if cmerr != nil {
Expand All @@ -297,26 +303,27 @@ func (cm *CompassManagerReconciler) configureRuntimeAndSetMappingStatus(kymaName
if cfgError != nil {
cm.Log.Errorf("Failed attempt to configure Compass Runtime Agent for Kyma resource %s", kymaName.Name)

statErr := cm.cluster.SetCompassMappingStatus(kymaName, Registered|Failed)
statErr := cm.cluster.SetCompassMappingStatus(kymaName, s.Registered|s.Failed)
if statErr != nil {
return ctrl.Result{Requeue: true}, errors.Wrap(statErr, "failed to set Compass Manager Status after failed attempt configuration Compass Runtime Agent ")
}

return ctrl.Result{Requeue: true}, errors.Wrapf(cfgError, "failed attempt to configure Compass Runtime Agent for Kyma resource %s", kymaName.Name)
}

cm.metrics.IncConfigure()
cm.metrics.IncConfigure(kymaName.Name)
cm.metrics.UpdateState(kymaName.Name, s.Registered|s.Processing)
cm.Log.Infof("Compass Runtime Agent for Runtime %s configured.", compassRuntimeID)

statErr := cm.cluster.SetCompassMappingStatus(kymaName, Registered|Configured)
statErr := cm.cluster.SetCompassMappingStatus(kymaName, s.Registered|s.Configured)
if statErr != nil {
return ctrl.Result{Requeue: true}, errors.Wrap(statErr, "failed to set Compass Manager Status after successful configuration Compass Runtime Agent ")
}

return ctrl.Result{}, nil
}

func (cm *CompassManagerReconciler) setStatusAndRequeue(kymaName types.NamespacedName, status Status) (ctrl.Result, error) {
func (cm *CompassManagerReconciler) setStatusAndRequeue(kymaName types.NamespacedName, status s.Status) (ctrl.Result, error) {
err := cm.cluster.SetCompassMappingStatus(kymaName, status)
if err != nil {
return ctrl.Result{Requeue: true}, errors.Wrap(err, "failed to update Compass Manager Mapping status")
Expand Down Expand Up @@ -607,15 +614,15 @@ func (c *ControlPlaneInterface) GetCompassRuntimeID(name types.NamespacedName) (

// SetCompassMappingStatus sets the registered and configured on an existing CompassManagerMapping
// If error occurs - logs it and returns
func (c *ControlPlaneInterface) SetCompassMappingStatus(name types.NamespacedName, status Status) error {
func (c *ControlPlaneInterface) SetCompassMappingStatus(name types.NamespacedName, status s.Status) error {
mapping, err := c.GetCompassMapping(name)
if err != nil {
return err
}

registered := status&Registered != 0
configured := status&Configured != 0
state := stateText(status)
registered := status&s.Registered != 0
configured := status&s.Configured != 0
state := s.StateText(status)

mapping.Status = v1beta1.CompassManagerMappingStatus{
Registered: registered,
Expand Down
85 changes: 63 additions & 22 deletions controllers/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,84 @@
package metrics

import (
s "github.com/kyma-project/compass-manager/controllers/status"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

const (
MetricState = "cm_states"
MetricActions = "cm_actions"

LabelState = "state"
LabelName = "kyma_name"
LabelAction = "action"

ActionRegister = "register"
ActionConfigure = "configure"
ActionUnregister = "unregister"
)

type Metrics struct {
configured prometheus.Counter
registered prometheus.Counter
unregistered prometheus.Counter
states *prometheus.GaugeVec
actions *prometheus.CounterVec
}

func NewMetrics() Metrics {
m := Metrics{
configured: prometheus.NewCounter(prometheus.CounterOpts{
Name: "configure_counter",
Help: "Number of cluster configured",
}),
registered: prometheus.NewCounter(prometheus.CounterOpts{
Name: "register_counter",
Help: "Number of cluster registered",
}),
unregistered: prometheus.NewCounter(prometheus.CounterOpts{
Name: "unregister_counter",
Help: "Number of cluster unregistered",
}),
states: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: MetricState,
Help: "Indicates the Status.state for Compass Mappings",
}, []string{LabelName, LabelState}),

actions: prometheus.NewCounterVec(prometheus.CounterOpts{
Name: MetricActions,
Help: "Number of <action> performed on Kymas",
}, []string{LabelName, LabelAction}),
}
metrics.Registry.MustRegister(m.configured, m.registered, m.unregistered)
metrics.Registry.MustRegister(m.states, m.actions)
return m
}

func (m Metrics) IncConfigure() {
m.configured.Inc()
func (m Metrics) IncConfigure(kymaName string) {
m.actions.With(prometheus.Labels{
LabelName: kymaName,
LabelAction: ActionConfigure,
}).Inc()
}

func (m Metrics) IncRegister() {
m.registered.Inc()
func (m Metrics) IncRegister(kymaName string) {
m.actions.With(prometheus.Labels{
LabelName: kymaName,
LabelAction: ActionRegister,
}).Inc()
}

func (m Metrics) IncUnregister() {
m.unregistered.Inc()
func (m Metrics) IncUnregister(kymaName string) {
m.actions.With(prometheus.Labels{
LabelName: kymaName,
LabelAction: ActionUnregister,
}).Inc()
}

func (m Metrics) UpdateState(kymaName string, status s.Status) {
if status == s.Empty {
m.setModuleStateGauge(kymaName, "")
return
}
state := s.StateText(status)
m.setModuleStateGauge(kymaName, state)
}

func (m Metrics) setModuleStateGauge(kymaName, state string) {
for _, s := range []string{s.ReadyState, s.FailedState, s.ProcessingState} {
val := 0.0
if s == state {
val = 1
}
m.states.With(prometheus.Labels{
LabelName: kymaName,
LabelState: s,
}).Set(val)
}
}
6 changes: 3 additions & 3 deletions controllers/status.go → controllers/status/status.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers
package status

import (
"github.com/kyma-project/compass-manager/api/v1beta1"
Expand All @@ -20,7 +20,7 @@ const (
FailedState = "Failed"
)

func stateText(status Status) string {
func StateText(status Status) string {
if status&Failed != 0 {
return FailedState
}
Expand All @@ -35,7 +35,7 @@ func stateText(status Status) string {
return FailedState
}

func statusNumber(status v1beta1.CompassManagerMappingStatus) Status {
func Number(status v1beta1.CompassManagerMappingStatus) Status {
out := Status(0)

if status.State == ProcessingState {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers
package status

import (
"testing"
Expand Down Expand Up @@ -73,7 +73,7 @@ func Test_stateText(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := stateText(tt.args.status); got != tt.want {
if got := StateText(tt.args.status); got != tt.want {
t.Errorf("stateText() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -137,7 +137,7 @@ func Test_statusNumber(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := statusNumber(tt.args.status); got != tt.want {
if got := Number(tt.args.status); got != tt.want {
t.Errorf("statusNumber() = %v, want %v", got, tt.want)
}
})
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,6 @@ golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down

0 comments on commit 000a224

Please sign in to comment.