From a55a8e6cde801e729f11c10ae1303a0c78ba324d Mon Sep 17 00:00:00 2001 From: Vincent Boutour Date: Sun, 21 Mar 2021 19:17:10 +0100 Subject: [PATCH] refactor: Changing metrics for notifier Signed-off-by: Vincent Boutour --- pkg/notifier/notifier.go | 17 +++++++++-------- pkg/notifier/notifier_test.go | 2 +- pkg/notifier/prometheus.go | 18 ++++++------------ 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/pkg/notifier/notifier.go b/pkg/notifier/notifier.go index 55717761..f9666819 100644 --- a/pkg/notifier/notifier.go +++ b/pkg/notifier/notifier.go @@ -70,7 +70,7 @@ func (a app) Notify() error { return fmt.Errorf("unable to clean repository before starting: %w", err) } - newReleases, err := a.getNewReleases(ctx) + newReleases, repoCount, err := a.getNewReleases(ctx) if err != nil { return fmt.Errorf("unable to get new releases: %w", err) } @@ -90,10 +90,11 @@ func (a app) Notify() error { } if len(a.pushURL) != 0 { - registry, releasesMetrics, notificationsMetrics := configurePrometheus() + registry, metrics := configurePrometheus() - releasesMetrics.Set(float64(len(newReleases))) - notificationsMetrics.Set(float64(len(ketchupsToNotify))) + metrics.WithLabelValues("repositories").Set(float64(repoCount)) + metrics.WithLabelValues("releases").Set(float64(len(newReleases))) + metrics.WithLabelValues("notifications").Set(float64(len(ketchupsToNotify))) if err := push.New(a.pushURL, "ketchup").Gatherer(registry).Push(); err != nil { logger.Error("unable to push metrics: %s", err) @@ -103,15 +104,15 @@ func (a app) Notify() error { return nil } -func (a app) getNewReleases(ctx context.Context) ([]model.Release, error) { +func (a app) getNewReleases(ctx context.Context) ([]model.Release, uint64, error) { var newReleases []model.Release - count := 0 + count := uint64(0) page := uint(1) for { repositories, totalCount, err := a.repositoryService.List(ctx, page, pageSize) if err != nil { - return nil, fmt.Errorf("unable to fetch page %d of repositories: %s", page, err) + return nil, count, fmt.Errorf("unable to fetch page %d of repositories: %s", page, err) } for _, repo := range repositories { @@ -123,7 +124,7 @@ func (a app) getNewReleases(ctx context.Context) ([]model.Release, error) { page++ } else { logger.Info("%d repositories checked, %d new releases", count, len(newReleases)) - return newReleases, nil + return newReleases, count, nil } } } diff --git a/pkg/notifier/notifier_test.go b/pkg/notifier/notifier_test.go index ffdcf0d3..93304d33 100644 --- a/pkg/notifier/notifier_test.go +++ b/pkg/notifier/notifier_test.go @@ -137,7 +137,7 @@ func TestGetNewReleases(t *testing.T) { for _, tc := range cases { t.Run(tc.intention, func(t *testing.T) { pageSize = 1 - got, gotErr := tc.instance.getNewReleases(tc.args.ctx) + got, _, gotErr := tc.instance.getNewReleases(tc.args.ctx) pageSize = 20 failed := false diff --git a/pkg/notifier/prometheus.go b/pkg/notifier/prometheus.go index 7c6cda01..eccb75dd 100644 --- a/pkg/notifier/prometheus.go +++ b/pkg/notifier/prometheus.go @@ -2,21 +2,15 @@ package notifier import "github.com/prometheus/client_golang/prometheus" -func configurePrometheus() (prometheus.Gatherer, prometheus.Gauge, prometheus.Gauge) { +func configurePrometheus() (prometheus.Gatherer, *prometheus.GaugeVec) { namespace := "ketchup" promRegistry := prometheus.NewRegistry() - releasesMetrics := prometheus.NewGauge(prometheus.GaugeOpts{ + metrics := prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: namespace, - Name: "releases", - }) - promRegistry.MustRegister(releasesMetrics) + Name: "metrics", + }, []string{"item"}) + promRegistry.MustRegister(metrics) - notificationMetrics := prometheus.NewGauge(prometheus.GaugeOpts{ - Namespace: namespace, - Name: "notification", - }) - promRegistry.MustRegister(notificationMetrics) - - return promRegistry, releasesMetrics, notificationMetrics + return promRegistry, metrics }