Skip to content

Commit fa3d3c9

Browse files
committed
add more labels to identify workflow and run
Signed-off-by: Markus Blaschke <[email protected]>
1 parent 9539e95 commit fa3d3c9

File tree

1 file changed

+99
-49
lines changed

1 file changed

+99
-49
lines changed

metrics_github_workflows.go

Lines changed: 99 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
const (
1616
CUSTOMPROP_LABEL_FMT = "prop_%s"
17+
LABEL_VALUE_UNKNOWN = "<unknown>"
1718
)
1819

1920
var (
@@ -78,6 +79,7 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
7879
"repo",
7980
"workflowID",
8081
"workflow",
82+
"workflowUrl",
8183
"state",
8284
"path",
8385
},
@@ -98,7 +100,11 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
98100
"org",
99101
"repo",
100102
"workflowID",
103+
"workflowRunNumber",
101104
"workflow",
105+
"workflowUrl",
106+
"workflowRun",
107+
"workflowRunRul",
102108
"event",
103109
"branch",
104110
"status",
@@ -117,6 +123,7 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
117123
"org",
118124
"repo",
119125
"workflowID",
126+
"workflowRunNumber",
120127
},
121128
)
122129
m.Collector.RegisterMetricList("workflowRunRunningStartTime", m.prometheus.workflowRunRunningStartTime, true)
@@ -133,7 +140,11 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
133140
"org",
134141
"repo",
135142
"workflowID",
143+
"workflowRunNumber",
136144
"workflow",
145+
"workflowUrl",
146+
"workflowRun",
147+
"workflowRunRul",
137148
"event",
138149
"branch",
139150
"conclusion",
@@ -152,6 +163,7 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
152163
"org",
153164
"repo",
154165
"workflowID",
166+
"workflowRunNumber",
155167
},
156168
)
157169
m.Collector.RegisterMetricList("workflowLatestRunStartTime", m.prometheus.workflowLatestRunStartTime, true)
@@ -165,6 +177,7 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
165177
"org",
166178
"repo",
167179
"workflowID",
180+
"workflowRunNumber",
168181
},
169182
)
170183
m.Collector.RegisterMetricList("workflowLatestRunDuration", m.prometheus.workflowLatestRunDuration, true)
@@ -181,7 +194,11 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
181194
"org",
182195
"repo",
183196
"workflowID",
197+
"workflowRunNumber",
184198
"workflow",
199+
"workflowUrl",
200+
"workflowRun",
201+
"workflowRunRul",
185202
"branch",
186203
"actorLogin",
187204
"actorType",
@@ -248,8 +265,8 @@ func (m *MetricsCollectorGithubWorkflows) getRepoList(org string) ([]*github.Rep
248265
return repositories, nil
249266
}
250267

251-
func (m *MetricsCollectorGithubWorkflows) getRepoWorkflows(org, repo string) ([]*github.Workflow, error) {
252-
var workflows []*github.Workflow
268+
func (m *MetricsCollectorGithubWorkflows) getRepoWorkflows(org, repo string) (map[int64]*github.Workflow, error) {
269+
workflows := map[int64]*github.Workflow{}
253270

254271
opts := github.ListOptions{PerPage: 100, Page: 1}
255272

@@ -266,7 +283,9 @@ func (m *MetricsCollectorGithubWorkflows) getRepoWorkflows(org, repo string) ([]
266283
return workflows, err
267284
}
268285

269-
workflows = append(workflows, result.Workflows...)
286+
for _, row := range result.Workflows {
287+
workflows[row.GetID()] = row
288+
}
270289

271290
// calc next page
272291
if response.NextPage == 0 {
@@ -369,12 +388,13 @@ func (m *MetricsCollectorGithubWorkflows) Collect(callback chan<- func()) {
369388
// workflow info metrics
370389
for _, workflow := range workflows {
371390
labels := prometheus.Labels{
372-
"org": org,
373-
"repo": repo.GetName(),
374-
"workflowID": fmt.Sprintf("%v", workflow.GetID()),
375-
"workflow": workflow.GetName(),
376-
"state": workflow.GetState(),
377-
"path": workflow.GetPath(),
391+
"org": org,
392+
"repo": repo.GetName(),
393+
"workflowID": fmt.Sprintf("%v", workflow.GetID()),
394+
"workflow": workflow.GetName(),
395+
"state": workflow.GetState(),
396+
"path": workflow.GetPath(),
397+
"workflowUrl": workflow.GetURL(),
378398
}
379399
for labelName, labelValue := range propLabels {
380400
labels[labelName] = labelValue
@@ -389,15 +409,15 @@ func (m *MetricsCollectorGithubWorkflows) Collect(callback chan<- func()) {
389409
}
390410

391411
if len(workflowRuns) >= 1 {
392-
m.collectRunningRuns(Opts.GitHub.Organization, repo, workflowRuns, callback)
393-
m.collectLatestRun(Opts.GitHub.Organization, repo, workflowRuns, callback)
394-
m.collectConsecutiveFailures(Opts.GitHub.Organization, repo, workflowRuns, callback)
412+
m.collectRunningRuns(Opts.GitHub.Organization, repo, workflows, workflowRuns, callback)
413+
m.collectLatestRun(Opts.GitHub.Organization, repo, workflows, workflowRuns, callback)
414+
m.collectConsecutiveFailures(Opts.GitHub.Organization, repo, workflows, workflowRuns, callback)
395415
}
396416
}
397417
}
398418
}
399419

400-
func (m *MetricsCollectorGithubWorkflows) collectRunningRuns(org string, repo *github.Repository, workflowRun []*github.WorkflowRun, callback chan<- func()) {
420+
func (m *MetricsCollectorGithubWorkflows) collectRunningRuns(org string, repo *github.Repository, workflows map[int64]*github.Workflow, workflowRun []*github.WorkflowRun, callback chan<- func()) {
401421
runMetric := m.Collector.GetMetricList("workflowRunRunning")
402422
runStartTimeMetric := m.Collector.GetMetricList("workflowRunRunningStartTime")
403423

@@ -415,29 +435,39 @@ func (m *MetricsCollectorGithubWorkflows) collectRunningRuns(org string, repo *g
415435
}
416436

417437
infoLabels := prometheus.Labels{
418-
"org": org,
419-
"repo": repo.GetName(),
420-
"workflowID": fmt.Sprintf("%v", workflowRun.GetWorkflowID()),
421-
"workflow": workflowRun.GetName(),
422-
"event": workflowRun.GetEvent(),
423-
"branch": workflowRun.GetHeadBranch(),
424-
"status": workflowRun.GetStatus(),
425-
"actorLogin": workflowRun.Actor.GetLogin(),
426-
"actorType": workflowRun.Actor.GetType(),
438+
"org": org,
439+
"repo": repo.GetName(),
440+
"workflowID": fmt.Sprintf("%v", workflowRun.GetWorkflowID()),
441+
"workflowRunNumber": fmt.Sprintf("%v", workflowRun.GetRunNumber()),
442+
"workflow": LABEL_VALUE_UNKNOWN,
443+
"workflowUrl": "",
444+
"workflowRun": workflowRun.GetName(),
445+
"workflowRunUrl": workflowRun.GetURL(),
446+
"event": workflowRun.GetEvent(),
447+
"branch": workflowRun.GetHeadBranch(),
448+
"status": workflowRun.GetStatus(),
449+
"actorLogin": workflowRun.Actor.GetLogin(),
450+
"actorType": workflowRun.Actor.GetType(),
451+
}
452+
453+
if workflow, ok := workflows[workflowRun.GetWorkflowID()]; ok {
454+
infoLabels["workflow"] = workflow.GetName()
455+
infoLabels["workflowUrl"] = workflow.GetURL()
427456
}
428457

429458
statLabels := prometheus.Labels{
430-
"org": org,
431-
"repo": repo.GetName(),
432-
"workflowID": fmt.Sprintf("%v", workflowRun.GetWorkflowID()),
459+
"org": org,
460+
"repo": repo.GetName(),
461+
"workflowID": fmt.Sprintf("%v", workflowRun.GetWorkflowID()),
462+
"workflowRunNumber": fmt.Sprintf("%v", workflowRun.GetRunNumber()),
433463
}
434464

435465
runMetric.AddInfo(infoLabels)
436466
runStartTimeMetric.AddTime(statLabels, workflowRun.GetRunStartedAt().Time)
437467
}
438468
}
439469

440-
func (m *MetricsCollectorGithubWorkflows) collectLatestRun(org string, repo *github.Repository, workflowRun []*github.WorkflowRun, callback chan<- func()) {
470+
func (m *MetricsCollectorGithubWorkflows) collectLatestRun(org string, repo *github.Repository, workflows map[int64]*github.Workflow, workflowRun []*github.WorkflowRun, callback chan<- func()) {
441471
runMetric := m.Collector.GetMetricList("workflowLatestRun")
442472
runTimestampMetric := m.Collector.GetMetricList("workflowLatestRunStartTime")
443473
runDurationMetric := m.Collector.GetMetricList("workflowLatestRunDuration")
@@ -466,21 +496,30 @@ func (m *MetricsCollectorGithubWorkflows) collectLatestRun(org string, repo *git
466496

467497
for _, workflowRun := range latestJobs {
468498
infoLabels := prometheus.Labels{
469-
"org": org,
470-
"repo": repo.GetName(),
471-
"workflowID": fmt.Sprintf("%v", workflowRun.GetWorkflowID()),
472-
"workflow": workflowRun.GetName(),
473-
"event": workflowRun.GetEvent(),
474-
"branch": workflowRun.GetHeadBranch(),
475-
"conclusion": workflowRun.GetConclusion(),
476-
"actorLogin": workflowRun.Actor.GetLogin(),
477-
"actorType": workflowRun.Actor.GetType(),
499+
"org": org,
500+
"repo": repo.GetName(),
501+
"workflowID": fmt.Sprintf("%v", workflowRun.GetWorkflowID()),
502+
"workflowRunNumber": fmt.Sprintf("%v", workflowRun.GetRunNumber()),
503+
"workflow": LABEL_VALUE_UNKNOWN,
504+
"workflowUrl": "",
505+
"workflowRun": workflowRun.GetName(),
506+
"workflowRunUrl": workflowRun.GetURL(),
507+
"event": workflowRun.GetEvent(),
508+
"branch": workflowRun.GetHeadBranch(),
509+
"conclusion": workflowRun.GetConclusion(),
510+
"actorLogin": workflowRun.Actor.GetLogin(),
511+
"actorType": workflowRun.Actor.GetType(),
512+
}
513+
if workflow, ok := workflows[workflowRun.GetWorkflowID()]; ok {
514+
infoLabels["workflow"] = workflow.GetName()
515+
infoLabels["workflowUrl"] = workflow.GetURL()
478516
}
479517

480518
statLabels := prometheus.Labels{
481-
"org": org,
482-
"repo": repo.GetName(),
483-
"workflowID": fmt.Sprintf("%v", workflowRun.GetWorkflowID()),
519+
"org": org,
520+
"repo": repo.GetName(),
521+
"workflowID": fmt.Sprintf("%v", workflowRun.GetWorkflowID()),
522+
"workflowRunNumber": fmt.Sprintf("%v", workflowRun.GetRunNumber()),
484523
}
485524

486525
runMetric.AddInfo(infoLabels)
@@ -489,7 +528,7 @@ func (m *MetricsCollectorGithubWorkflows) collectLatestRun(org string, repo *git
489528
}
490529
}
491530

492-
func (m *MetricsCollectorGithubWorkflows) collectConsecutiveFailures(org string, repo *github.Repository, workflowRun []*github.WorkflowRun, callback chan<- func()) {
531+
func (m *MetricsCollectorGithubWorkflows) collectConsecutiveFailures(org string, repo *github.Repository, workflows map[int64]*github.Workflow, workflowRun []*github.WorkflowRun, callback chan<- func()) {
493532
consecutiveFailuresMetric := m.Collector.GetMetricList("workflowConsecutiveFailures")
494533

495534
consecutiveFailMap := map[int64]*struct {
@@ -517,20 +556,31 @@ func (m *MetricsCollectorGithubWorkflows) collectConsecutiveFailures(org string,
517556
}
518557

519558
if _, exists := consecutiveFailMap[workflowId]; !exists {
559+
infoLabels := prometheus.Labels{
560+
"org": org,
561+
"repo": repo.GetName(),
562+
"workflowID": fmt.Sprintf("%v", workflowRun.GetWorkflowID()),
563+
"workflowRunNumber": fmt.Sprintf("%v", workflowRun.GetRunNumber()),
564+
"workflow": LABEL_VALUE_UNKNOWN,
565+
"workflowUrl": "",
566+
"workflowRun": workflowRun.GetName(),
567+
"workflowRunUrl": workflowRun.GetURL(),
568+
"branch": workflowRun.GetHeadBranch(),
569+
"actorLogin": workflowRun.Actor.GetLogin(),
570+
"actorType": workflowRun.Actor.GetType(),
571+
}
572+
573+
if workflow, ok := workflows[workflowRun.GetWorkflowID()]; ok {
574+
infoLabels["workflow"] = workflow.GetName()
575+
infoLabels["workflowUrl"] = workflow.GetURL()
576+
}
577+
520578
consecutiveFailMap[workflowId] = &struct {
521579
count int64
522580
labels prometheus.Labels
523581
}{
524-
count: 0,
525-
labels: prometheus.Labels{
526-
"org": org,
527-
"repo": repo.GetName(),
528-
"workflowID": fmt.Sprintf("%v", workflowRun.GetWorkflowID()),
529-
"workflow": workflowRun.GetName(),
530-
"branch": workflowRun.GetHeadBranch(),
531-
"actorLogin": workflowRun.Actor.GetLogin(),
532-
"actorType": workflowRun.Actor.GetType(),
533-
},
582+
count: 0,
583+
labels: infoLabels,
534584
}
535585
consecutiveFinishedMap[workflowId] = false
536586
}

0 commit comments

Comments
 (0)