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 1 commit
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
59 changes: 46 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,66 @@ 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"
if strings.HasPrefix(run.GetEvent(), "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 == nil {
fheinecke marked this conversation as resolved.
Show resolved Hide resolved
continue
}

if prBaseBranch.GetSHA() == headSha {
return headSha
fheinecke marked this conversation as resolved.
Show resolved Hide resolved
}

if prBaseBranch.GetRef() == headBranch {
return headBranch
}
}

return "error-no-matching-pr"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this is an error - the value should either be empty or "unknown". Not every workflow run with a pull request event will be able to be tied back to a PR - the description of the pull_requests field in the "Get a workflow run" docs says

Pull requests that are open with a head_sha or head_branch that matches the workflow run. The returned pull requests do not necessarily indicate pull requests that triggered the run.

So if the pull request has closed at the time we query the the workflow runs, we may not get a PR. That's not an error, just bad timing, so I don't think the value should indicate an error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think "error-no-matching-open-pr" would be better, or maybe the head branch? I'm attempting to prefix all the "this is not what you would expect and should be filtered out in queries" values with the same prefix.

Pull requests that are open

I really hate this API...

}

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