Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record suspension metrics #164

Merged
merged 2 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions controllers/alert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
return ctrl.Result{}, client.IgnoreNotFound(err)
}

// record suspension metrics
r.recordSuspension(ctx, alert)

// record reconciliation duration
if r.MetricsRecorder != nil {
objRef, err := reference.GetReference(r.Scheme, &alert)
Expand Down Expand Up @@ -109,6 +112,25 @@ func (r *AlertReconciler) validate(ctx context.Context, alert v1beta1.Alert) err
return nil
}

func (r *AlertReconciler) recordSuspension(ctx context.Context, alert v1beta1.Alert) {
if r.MetricsRecorder == nil {
return
}
log := logr.FromContext(ctx)

objRef, err := reference.GetReference(r.Scheme, &alert)
if err != nil {
log.Error(err, "unable to record suspended metric")
return
}

if !alert.DeletionTimestamp.IsZero() {
r.MetricsRecorder.RecordSuspend(*objRef, false)
} else {
r.MetricsRecorder.RecordSuspend(*objRef, alert.Spec.Suspend)
}
}

func (r *AlertReconciler) recordReadiness(ctx context.Context, alert v1beta1.Alert) {
log := logr.FromContext(ctx)
if r.MetricsRecorder == nil {
Expand Down
23 changes: 23 additions & 0 deletions controllers/receiver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"crypto/sha256"
"fmt"
"k8s.io/client-go/tools/reference"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -62,6 +63,9 @@ func (r *ReceiverReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, client.IgnoreNotFound(err)
}

// record suspension metrics
defer r.recordSuspension(ctx, receiver)

token, err := r.token(ctx, receiver)
if err != nil {
receiver = v1beta1.ReceiverNotReady(receiver, v1beta1.TokenNotFoundReason, err.Error())
Expand Down Expand Up @@ -118,6 +122,25 @@ func (r *ReceiverReconciler) token(ctx context.Context, receiver v1beta1.Receive
return token, nil
}

func (r *ReceiverReconciler) recordSuspension(ctx context.Context, rcvr v1beta1.Receiver) {
if r.MetricsRecorder == nil {
return
}
log := logr.FromContext(ctx)

objRef, err := reference.GetReference(r.Scheme, &rcvr)
if err != nil {
log.Error(err, "unable to record suspended metric")
return
}

if !rcvr.DeletionTimestamp.IsZero() {
r.MetricsRecorder.RecordSuspend(*objRef, false)
} else {
r.MetricsRecorder.RecordSuspend(*objRef, rcvr.Spec.Suspend)
}
}

func sha256sum(val string) string {
digest := sha256.Sum256([]byte(val))
return fmt.Sprintf("%x", digest)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/fluxcd/notification-controller/api v0.10.0
github.com/fluxcd/pkg/apis/meta v0.8.0
github.com/fluxcd/pkg/recorder v0.0.6
github.com/fluxcd/pkg/runtime v0.8.4
github.com/fluxcd/pkg/runtime v0.8.5
github.com/go-logr/logr v0.3.0
github.com/google/go-github/v32 v32.1.0
github.com/hashicorp/go-retryablehttp v0.6.8
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ github.com/fluxcd/pkg/apis/meta v0.8.0 h1:wqWpUsxhKHB1ZztcvOz+vnyhdKW9cWmjFp8Vci
github.com/fluxcd/pkg/apis/meta v0.8.0/go.mod h1:yHuY8kyGHYz22I0jQzqMMGCcHViuzC/WPdo9Gisk8Po=
github.com/fluxcd/pkg/recorder v0.0.6 h1:me/n8syeeGXz50OXoPX3jgIj9AtinvhHdKT9Dy+MbHs=
github.com/fluxcd/pkg/recorder v0.0.6/go.mod h1:IfQxfVRSNsWs3B0Yp5B6ObEWwKHILlAx8N7XkoDdhFg=
github.com/fluxcd/pkg/runtime v0.8.4 h1:amuhfoHGCUfFCPXg3Zrcyy7f9J+fho+/+FbQDDyewko=
github.com/fluxcd/pkg/runtime v0.8.4/go.mod h1:JD0eZIn5xkTeHHQUWXSqJPIh/ecO0d0qrUKbSVHnpnw=
github.com/fluxcd/pkg/runtime v0.8.5 h1:ynh8fszbLQ3QSisQBNOABEUTnvt+/QfCdaL6gOJQcoQ=
github.com/fluxcd/pkg/runtime v0.8.5/go.mod h1:JD0eZIn5xkTeHHQUWXSqJPIh/ecO0d0qrUKbSVHnpnw=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
Expand Down