Skip to content

Commit

Permalink
Merge pull request #27 from gravitational/fred/add-event-labels
Browse files Browse the repository at this point in the history
Added event and ref labels
  • Loading branch information
fheinecke authored Mar 16, 2024
2 parents 55917a3 + 04c705d commit edf5a9f
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@ var (
prometheus.CounterOpts{
Name: "gha_workflow_run_time_seconds",
},
[]string{"repo", "ref", "workflow"},
[]string{"repo", "ref", "event_type", "workflow"},
)
jobRunTimeVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gha_job_run_time_seconds",
},
[]string{"repo", "ref", "workflow", "job"},
[]string{"repo", "ref", "event_type", "workflow", "job"},
)
stepRunTimeVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gha_step_run_time_seconds",
},
[]string{"repo", "ref", "workflow", "job", "step"},
[]string{"repo", "ref", "event_type", "workflow", "job", "step"},
)
workflowRunCountVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gha_workflow_run_count",
},
[]string{"repo", "ref", "workflow", "conclusion"},
[]string{"repo", "ref", "event_type", "workflow", "conclusion"},
)
jobRunCountVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gha_job_run_count",
},
[]string{"repo", "ref", "workflow", "job", "conclusion"},
[]string{"repo", "ref", "event_type", "workflow", "job", "conclusion"},
)
stepRunCountVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gha_step_run_count",
},
[]string{"repo", "ref", "workflow", "job", "step", "conclusion"},
[]string{"repo", "ref", "event_type", "workflow", "job", "step", "conclusion"},
)
)

Expand Down Expand Up @@ -203,13 +203,14 @@ func countJobs(run *github.WorkflowRun, jobs []*github.WorkflowJob) {
workflowName = strings.TrimSuffix(workflowName, path.Ext(workflowName))
repo := run.GetRepository().GetName()
ref := makeRef(run)
eventType := run.GetEvent()

var workflowRunTime time.Duration
for _, job := range jobs {
var jobRunTime time.Duration
for _, step := range job.Steps {
stepRunCountVec.WithLabelValues(
repo, ref, workflowName, job.GetName(), step.GetName(), step.GetConclusion(),
repo, ref, eventType, workflowName, job.GetName(), step.GetName(), step.GetConclusion(),
).Add(1)

if step.GetConclusion() != "success" {
Expand All @@ -220,34 +221,72 @@ func countJobs(run *github.WorkflowRun, jobs []*github.WorkflowJob) {
}
stepRunTime := step.CompletedAt.Time.Sub(step.StartedAt.Time)
stepRunTimeVec.WithLabelValues(
repo, ref, workflowName, job.GetName(), step.GetName(),
repo, ref, eventType, workflowName, job.GetName(), step.GetName(),
).Add(stepRunTime.Seconds())
jobRunTime += stepRunTime
}
jobRunCountVec.WithLabelValues(
repo, ref, workflowName, job.GetName(), job.GetConclusion(),
repo, ref, eventType, workflowName, job.GetName(), job.GetConclusion(),
).Add(1)
if job.GetConclusion() != "success" {
continue
}
jobRunTimeVec.WithLabelValues(
repo, ref, workflowName, job.GetName(),
repo, ref, eventType, workflowName, job.GetName(),
).Add(jobRunTime.Seconds())

workflowRunTime += jobRunTime
}

workflowRunCountVec.WithLabelValues(repo, ref, workflowName, run.GetConclusion()).Add(1)
workflowRunCountVec.WithLabelValues(repo, ref, eventType, workflowName, run.GetConclusion()).Add(1)

if run.GetConclusion() != "success" {
return
}

workflowRunTimeVec.WithLabelValues(repo, ref, workflowName).Add(workflowRunTime.Seconds())
workflowRunTimeVec.WithLabelValues(repo, ref, eventType, workflowName).Add(workflowRunTime.Seconds())
}

func makeRef(run *github.WorkflowRun) string {
return "todo"
eventName := run.GetEvent()
if strings.HasPrefix(eventName, "pull_request") {
// Attempt to tie the workflow to a PR
headSha := run.GetHeadSHA()
headBranch := run.GetHeadBranch()

if headSha == "" && headBranch == "" {
return "error-missing-head-ref"
}

for _, pr := range run.PullRequests {
prBaseBranch := pr.GetBase()

if prBaseBranch.GetSHA() == headSha ||
prBaseBranch.GetRef() == headBranch {
return headBranch
}
}

return headBranch
}

if strings.HasPrefix(eventName, "merge_group") {
headBranch := run.GetHeadBranch()
if headBranch == "" {
return "error-head-branch-is-nil"
}

mergeBranch := strings.TrimPrefix(headBranch, "gh-readonly-queue/")
mergeBranch = strings.SplitN(mergeBranch, "/pr-", 2)[0]
return mergeBranch
}

headBranch := run.GetHeadBranch()
if headBranch == "" {
return "error-head-branch-is-nil"
}

return headBranch
}

func newGHClient(owner string, appID int64, appKey []byte) (*github.Client, error) {
Expand Down

0 comments on commit edf5a9f

Please sign in to comment.