Skip to content

Commit

Permalink
Merge pull request #133 from YutaUra/main
Browse files Browse the repository at this point in the history
add lifetime_days label to prometheus metrics
  • Loading branch information
chaspy authored Apr 17, 2024
2 parents c3d5eb4 + 015b82d commit 19265fe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .envrc.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export GITHUB_TOKEN="secret"
export GITHUB_REPOSITORIES="chaspy/github-pr-prometheus-exporter,chaspy/favsearch"
# LIFETIME_STALE_DAYS is optional, default is 14 days
# export LIFETIME_STALE_DAYS=14
39 changes: 34 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type PR struct {
User string
RequestedReviewers []*github.User
Repo string
CreatedAt time.Time
}

var (
Expand Down Expand Up @@ -88,6 +89,12 @@ func snapshot() error {

prInfos := getPRInfos(prs)

lifetimeStaleDays, err := getLifetimeStaleDays()
if err != nil {
return fmt.Errorf("failed to get lifetime stale days: %w", err)
}
staleThresholdTime := time.Now().Add(-time.Hour * 24 * time.Duration(lifetimeStaleDays))

for _, prInfo := range prInfos {
labelsTag := make([]string, len(prInfo.Labels))
for i, label := range prInfo.Labels {
Expand All @@ -99,12 +106,18 @@ func snapshot() error {
reviewersTag[i] = *reviewer.Login
}

lifetimeStatus := "ok"
if prInfo.CreatedAt.Before(staleThresholdTime) {
lifetimeStatus = "stale"
}

labels := prometheus.Labels{
"number": strconv.Itoa(prInfo.Number),
"label": strings.Join(labelsTag, ","),
"author": prInfo.User,
"reviewer": strings.Join(reviewersTag, ","),
"repo": prInfo.Repo,
"number": strconv.Itoa(prInfo.Number),
"label": strings.Join(labelsTag, ","),
"author": prInfo.User,
"reviewer": strings.Join(reviewersTag, ","),
"repo": prInfo.Repo,
"lifetime_status": lifetimeStatus,
}

PullRequestCount.With(labels).Set(1)
Expand All @@ -113,6 +126,21 @@ func snapshot() error {
return nil
}

func getLifetimeStaleDays() (int, error) {
const defaultLifetimeStaleDays = 14
lifetimeStaleDays := os.Getenv("LIFETIME_STALE_DAYS")
if len(lifetimeStaleDays) == 0 {
return defaultLifetimeStaleDays, nil
}

integerLifetimeStaleDays, err := strconv.Atoi(lifetimeStaleDays)
if err != nil {
return 0, fmt.Errorf("failed to convert lifetimeStaleDays: %w", err)
}

return integerLifetimeStaleDays, nil
}

func getInterval() (int, error) {
const defaultGithubAPIIntervalSecond = 300
githubAPIInterval := os.Getenv("GITHUB_API_INTERVAL")
Expand Down Expand Up @@ -202,6 +230,7 @@ func getPRInfos(prs []*github.PullRequest) []PR {
User: pr.User.GetLogin(),
RequestedReviewers: pr.RequestedReviewers,
Repo: repos[4] + "/" + repos[5],
CreatedAt: *pr.CreatedAt.GetTime(),
}
}

Expand Down

0 comments on commit 19265fe

Please sign in to comment.