From 4cc9584c9952591bb163dc2a413570e32dc3ca84 Mon Sep 17 00:00:00 2001 From: Oded Ben Ozer Date: Thu, 12 Dec 2024 09:49:17 +0100 Subject: [PATCH] Use a struct to pass data between getRepoPrMetrics and PublishPrMetrics --- internal/pkg/githubapi/pr_metrics.go | 12 ++++++------ internal/pkg/prometheus/prometheus.go | 14 ++++++++++---- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/internal/pkg/githubapi/pr_metrics.go b/internal/pkg/githubapi/pr_metrics.go index 0e734a47..4b91952d 100644 --- a/internal/pkg/githubapi/pr_metrics.go +++ b/internal/pkg/githubapi/pr_metrics.go @@ -22,7 +22,7 @@ func MainGhMetricsLoop(mainGhClientCache *lru.Cache[string, GhClientPair]) { } } -func getRepoPrMetrics(ctx context.Context, ghClient GhClientPair, repo *github.Repository) (prWithStaleChecks int, openPRs int, openPromotionPrs int, err error) { +func getRepoPrMetrics(ctx context.Context, ghClient GhClientPair, repo *github.Repository) (pc prom.PrCounters, err error) { log.Debugf("Checking repo %s", repo.GetName()) ghOwner := repo.GetOwner().GetLogin() prListOpts := &github.PullRequestListOptions{ @@ -46,7 +46,7 @@ func getRepoPrMetrics(ctx context.Context, ghClient GhClientPair, repo *github.R for _, pr := range prs { if DoesPrHasLabel(pr.Labels, "promotion") { - openPromotionPrs++ + pc.OpenPromotionPrs++ } log.Debugf("Checking PR %d", pr.GetNumber()) @@ -57,10 +57,10 @@ func getRepoPrMetrics(ctx context.Context, ghClient GhClientPair, repo *github.R continue } if isPrStalePending(commitStatuses, timeToDefineStale) { - prWithStaleChecks++ + pc.PrWithStaleChecks++ } } - openPRs = len(prs) + pc.OpenPrs = len(prs) return } @@ -97,12 +97,12 @@ func getPrMetrics(mainGhClientCache *lru.Cache[string, GhClientPair]) { continue } for _, repo := range repos.Repositories { - stalePendingChecks, openPrs, promotionPrs, err := getRepoPrMetrics(ctx, ghClient, repo) + pc, err := getRepoPrMetrics(ctx, ghClient, repo) if err != nil { log.Errorf("error getting repos for %s: %v", ghOwner, err) continue } - prom.PublishPrMetrics(openPrs, promotionPrs, stalePendingChecks, repo.GetFullName()) + prom.PublishPrMetrics(pc, repo.GetFullName()) } } } diff --git a/internal/pkg/prometheus/prometheus.go b/internal/pkg/prometheus/prometheus.go index 549cd8fe..be3e4abf 100644 --- a/internal/pkg/prometheus/prometheus.go +++ b/internal/pkg/prometheus/prometheus.go @@ -10,6 +10,12 @@ import ( "github.com/prometheus/client_golang/prometheus/promauto" ) +type PrCounters struct { + OpenPrs int + OpenPromotionPrs int + PrWithStaleChecks int +} + var ( webhookHitsVec = promauto.NewCounterVec(prometheus.CounterOpts{ Name: "webhook_hits_total", @@ -82,13 +88,13 @@ func IncCommitStatusUpdateCounter(repoSlug string, status string) { }).Inc() } -func PublishPrMetrics(openPrs int, openPromPRs int, openPrsWithPendingChecks int, repoSlug string) { +func PublishPrMetrics(pc PrCounters, repoSlug string) { metricLables := prometheus.Labels{ "repo_slug": repoSlug, } - ghOpenPrsGauge.With(metricLables).Set(float64(openPrs)) - ghOpenPromotionPrsGauge.With(metricLables).Set(float64(openPromPRs)) - ghOpenPrsWithPendingCheckGauge.With(metricLables).Set(float64(openPrsWithPendingChecks)) + ghOpenPrsGauge.With(metricLables).Set(float64(pc.OpenPrs)) + ghOpenPromotionPrsGauge.With(metricLables).Set(float64(pc.OpenPromotionPrs)) + ghOpenPrsWithPendingCheckGauge.With(metricLables).Set(float64(pc.PrWithStaleChecks)) } // This function instrument Webhook hits and parsing of their content