-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from appuio/feat/email-sending-metrics
Add metrics that indicate the status of email sending
- Loading branch information
Showing
5 changed files
with
227 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
apimeta "k8s.io/apimachinery/pkg/api/meta" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
userv1 "github.com/appuio/control-api/apis/user/v1" | ||
) | ||
|
||
//+kubebuilder:rbac:groups="rbac.appuio.io",resources=invitations,verbs=get;list;watch | ||
//+kubebuilder:rbac:groups="user.appuio.io",resources=invitations,verbs=get;list;watch | ||
//+kubebuilder:rbac:groups="rbac.appuio.io",resources=invitations/status,verbs=get | ||
//+kubebuilder:rbac:groups="user.appuio.io",resources=invitations/status,verbs=get | ||
|
||
var emailPendingDesc = prometheus.NewDesc( | ||
"control_api_invitation_emails_pending_current", | ||
"Amount of e-mails that have not been sent yet", | ||
nil, | ||
nil, | ||
) | ||
|
||
// EmailPendingMetric is a Prometheus collector that exposes the number of currently pending invitation e-mails | ||
type EmailPendingMetric struct { | ||
client.Client | ||
} | ||
|
||
var _ prometheus.Collector = &EmailPendingMetric{} | ||
|
||
// Describe implements prometheus.Collector. | ||
// Sends the static description of the metric to the provided channel. | ||
func (e *EmailPendingMetric) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- emailPendingDesc | ||
} | ||
|
||
// Collect implements prometheus.Collector. | ||
// Sends a metric to the provided channel. | ||
func (e *EmailPendingMetric) Collect(ch chan<- prometheus.Metric) { | ||
invs := &userv1.InvitationList{} | ||
|
||
if err := e.List(context.Background(), invs); err != nil { | ||
ch <- prometheus.NewInvalidMetric(emailPendingDesc, err) | ||
return | ||
} | ||
|
||
var count float64 = 0 | ||
for _, inv := range invs.Items { | ||
if !apimeta.IsStatusConditionTrue(inv.Status.Conditions, userv1.ConditionEmailSent) { | ||
count++ | ||
} | ||
} | ||
ch <- prometheus.MustNewConstMetric( | ||
emailPendingDesc, | ||
prometheus.GaugeValue, | ||
count, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package controllers_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
userv1 "github.com/appuio/control-api/apis/user/v1" | ||
"github.com/appuio/control-api/controllers" | ||
) | ||
|
||
func TestEmailPendingMetric(t *testing.T) { | ||
c := prepareTest(t, &userv1.Invitation{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-inv", | ||
}, | ||
Spec: userv1.InvitationSpec{}, | ||
Status: userv1.InvitationStatus{Conditions: []metav1.Condition{{Type: userv1.ConditionEmailSent, Status: metav1.ConditionTrue}}}, | ||
}, &userv1.Invitation{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-inv-2", | ||
}, | ||
Spec: userv1.InvitationSpec{}, | ||
}) | ||
|
||
require.NoError(t, | ||
testutil.CollectAndCompare(&controllers.EmailPendingMetric{c}, strings.NewReader(` | ||
# HELP control_api_invitation_emails_pending_current Amount of e-mails that have not been sent yet | ||
# TYPE control_api_invitation_emails_pending_current gauge | ||
control_api_invitation_emails_pending_current 1 | ||
`), | ||
), | ||
) | ||
} |