Skip to content

Commit

Permalink
Use a struct to pass data between getRepoPrMetrics and PublishPrMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Oded-B committed Dec 12, 2024
1 parent 78f43e7 commit 4cc9584
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
12 changes: 6 additions & 6 deletions internal/pkg/githubapi/pr_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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())
Expand All @@ -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
}
Expand Down Expand Up @@ -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())
}
}
}
14 changes: 10 additions & 4 deletions internal/pkg/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4cc9584

Please sign in to comment.