Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added event and ref labels #27

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
fheinecke marked this conversation as resolved.
Show resolved Hide resolved
if headBranch == "" {
return "error-head-branch-is-nil"
}

return headBranch
}

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