Skip to content

Commit

Permalink
refactor: Changing metrics for notifier
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Mar 21, 2021
1 parent 9e89f9a commit a55a8e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
17 changes: 9 additions & 8 deletions pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 6 additions & 12 deletions pkg/notifier/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit a55a8e6

Please sign in to comment.