@@ -2,7 +2,6 @@ package main
22
33import (
44 "context"
5- "errors"
65 "fmt"
76 "log/slog"
87 "strings"
@@ -84,6 +83,7 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
8483 "workflowID" ,
8584 "workflow" ,
8685 "workflowUrl" ,
86+ "workflowDefinitionUrl" ,
8787 "state" ,
8888 "path" ,
8989 },
@@ -107,6 +107,7 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
107107 "workflowRunNumber" ,
108108 "workflow" ,
109109 "workflowUrl" ,
110+ "workflowDefinitionUrl" ,
110111 "workflowRun" ,
111112 "workflowRunUrl" ,
112113 "event" ,
@@ -147,6 +148,7 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
147148 "workflowRunNumber" ,
148149 "workflow" ,
149150 "workflowUrl" ,
151+ "workflowDefinitionUrl" ,
150152 "workflowRun" ,
151153 "workflowRunUrl" ,
152154 "event" ,
@@ -201,6 +203,7 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
201203 "workflowRunNumber" ,
202204 "workflow" ,
203205 "workflowUrl" ,
206+ "workflowDefinitionUrl" ,
204207 "workflowRun" ,
205208 "workflowRunUrl" ,
206209 "branch" ,
@@ -223,6 +226,7 @@ func (m *MetricsCollectorGithubWorkflows) Setup(collector *collector.Collector)
223226 "workflowRunNumber" ,
224227 "workflow" ,
225228 "workflowUrl" ,
229+ "workflowDefinitionUrl" ,
226230 "workflowRun" ,
227231 "workflowRunUrl" ,
228232 "branch" ,
@@ -265,12 +269,7 @@ func (m *MetricsCollectorGithubWorkflows) getRepoList(ctx context.Context, org s
265269 var repoCustomProperties []* github.CustomPropertyValue
266270 for {
267271 repoCustomProperties , _ , err = githubClient .Repositories .GetAllCustomPropertyValues (ctx , org , repository .GetName ())
268- var ghRateLimitError * github.RateLimitError
269- if ok := errors .As (err , & ghRateLimitError ); ok {
270- m .Logger ().Debug ("request GetAllCustomPropertyValues rate limited" , slog .Time ("waitingUntil" , ghRateLimitError .Rate .Reset .Time ))
271- time .Sleep (time .Until (ghRateLimitError .Rate .Reset .Time ))
272- continue
273- } else if err != nil {
272+ if err != nil {
274273 panic (err )
275274 }
276275 break
@@ -286,22 +285,20 @@ func (m *MetricsCollectorGithubWorkflows) getRepoList(ctx context.Context, org s
286285 return repositories , nil
287286}
288287
289- func (m * MetricsCollectorGithubWorkflows ) getRepoWorkflows (ctx context.Context , org , repo string ) (map [ int64 ] * github. Workflow , error ) {
290- workflows := map [ int64 ] * github. Workflow {}
288+ func (m * MetricsCollectorGithubWorkflows ) getRepoWorkflows (ctx context.Context , org string , repo * github. Repository ) (* GithubWorkflowList , error ) {
289+ workflows := NewGithubWorkflowList ( org , repo )
291290
292291 opts := github.ListOptions {PerPage : 100 , Page : 1 }
293292
294293 for {
295- m .Logger ().Debug (`fetching workflows list for repository` , slog .String ("repository" , repo ), slog .Int ("page" , opts .Page ))
294+ m .Logger ().Debug (`fetching workflows list for repository` , slog .String ("repository" , repo . GetName () ), slog .Int ("page" , opts .Page ))
296295
297- result , response , err := githubClient .Actions .ListWorkflows (ctx , org , repo , & opts )
296+ result , response , err := githubClient .Actions .ListWorkflows (ctx , org , repo . GetName () , & opts )
298297 if err != nil {
299298 return workflows , err
300299 }
301300
302- for _ , row := range result .Workflows {
303- workflows [row .GetID ()] = row
304- }
301+ workflows .Add (result .Workflows ... )
305302
306303 // calc next page
307304 if response .NextPage == 0 {
@@ -394,29 +391,28 @@ func (m *MetricsCollectorGithubWorkflows) Collect(callback chan<- func()) {
394391 repositoryMetric .AddInfo (labels )
395392
396393 // get workflows
397- workflows , err := m .getRepoWorkflows (ctx , org , repo . GetName () )
394+ workflows , err := m .getRepoWorkflows (ctx , org , repo )
398395 if err != nil {
399396 panic (err )
400397 }
401398
402399 // workflow info metrics
403- for _ , workflow := range workflows {
400+ for _ , workflow := range workflows . List () {
404401 labels := prometheus.Labels {
405- "org" : org ,
406- "repo" : repo .GetName (),
407- "workflowID" : fmt .Sprintf ("%v" , workflow .GetID ()),
408- "workflow" : workflow .GetName (),
409- "state" : workflow .GetState (),
410- "path" : workflow .GetPath (),
411- "workflowUrl" : workflow .GetHTMLURL (),
402+ "org" : org ,
403+ "repo" : repo .GetName (),
404+ "workflowID" : fmt .Sprintf ("%v" , workflow .GetID ()),
405+ "state" : workflow .GetState (),
406+ "path" : workflow .GetPath (),
412407 }
408+ workflow .AddDefaultLabels (labels )
413409 for labelName , labelValue := range propLabels {
414410 labels [labelName ] = labelValue
415411 }
416412 workflowMetric .AddInfo (labels )
417413 }
418414
419- if len ( workflows ) >= 1 {
415+ if workflows . Count ( ) >= 1 {
420416 workflowRuns , err := m .getRepoWorkflowRuns (ctx , repo )
421417 if err != nil {
422418 panic (err )
@@ -432,7 +428,7 @@ func (m *MetricsCollectorGithubWorkflows) Collect(callback chan<- func()) {
432428 }
433429}
434430
435- func (m * MetricsCollectorGithubWorkflows ) collectRunningRuns (org string , repo * github.Repository , workflows map [ int64 ] * github. Workflow , workflowRun []* github.WorkflowRun , callback chan <- func ()) {
431+ func (m * MetricsCollectorGithubWorkflows ) collectRunningRuns (org string , repo * github.Repository , workflows * GithubWorkflowList , workflowRun []* github.WorkflowRun , callback chan <- func ()) {
436432 runMetric := m .Collector .GetMetricList ("workflowRunRunning" )
437433 runStartTimeMetric := m .Collector .GetMetricList ("workflowRunRunningStartTime" )
438434
@@ -466,10 +462,7 @@ func (m *MetricsCollectorGithubWorkflows) collectRunningRuns(org string, repo *g
466462 }
467463
468464 // add workflow labels
469- if workflow , ok := workflows [workflowRun .GetWorkflowID ()]; ok {
470- infoLabels ["workflow" ] = workflow .GetName ()
471- infoLabels ["workflowUrl" ] = workflow .GetHTMLURL ()
472- }
465+ workflows .GetById (workflowRun .GetWorkflowID ()).AddDefaultLabels (infoLabels )
473466
474467 statLabels := prometheus.Labels {
475468 "org" : org ,
@@ -483,7 +476,7 @@ func (m *MetricsCollectorGithubWorkflows) collectRunningRuns(org string, repo *g
483476 }
484477}
485478
486- func (m * MetricsCollectorGithubWorkflows ) collectLatestRun (org string , repo * github.Repository , workflows map [ int64 ] * github. Workflow , workflowRun []* github.WorkflowRun , callback chan <- func ()) {
479+ func (m * MetricsCollectorGithubWorkflows ) collectLatestRun (org string , repo * github.Repository , workflows * GithubWorkflowList , workflowRun []* github.WorkflowRun , callback chan <- func ()) {
487480 runMetric := m .Collector .GetMetricList ("workflowLatestRun" )
488481 runTimestampMetric := m .Collector .GetMetricList ("workflowLatestRunStartTime" )
489482 runDurationMetric := m .Collector .GetMetricList ("workflowLatestRunDuration" )
@@ -493,6 +486,11 @@ func (m *MetricsCollectorGithubWorkflows) collectLatestRun(org string, repo *git
493486 workflowRun := row
494487 workflowId := workflowRun .GetWorkflowID ()
495488
489+ workflow := workflows .GetById (workflowRun .GetWorkflowID ())
490+ if ! workflow .IsActive () {
491+ continue
492+ }
493+
496494 // skip forks
497495 if workflowRun .GetHeadRepository ().Fork != nil && * workflowRun .GetHeadRepository ().Fork {
498496 continue
@@ -533,10 +531,7 @@ func (m *MetricsCollectorGithubWorkflows) collectLatestRun(org string, repo *git
533531 }
534532
535533 // add workflow labels
536- if workflow , ok := workflows [workflowRun .GetWorkflowID ()]; ok {
537- infoLabels ["workflow" ] = workflow .GetName ()
538- infoLabels ["workflowUrl" ] = workflow .GetHTMLURL ()
539- }
534+ workflows .GetById (workflowRun .GetWorkflowID ()).AddDefaultLabels (infoLabels )
540535
541536 statLabels := prometheus.Labels {
542537 "org" : org ,
@@ -551,7 +546,7 @@ func (m *MetricsCollectorGithubWorkflows) collectLatestRun(org string, repo *git
551546 }
552547}
553548
554- func (m * MetricsCollectorGithubWorkflows ) collectConsecutiveFailures (org string , repo * github.Repository , workflows map [ int64 ] * github. Workflow , workflowRun []* github.WorkflowRun , callback chan <- func ()) {
549+ func (m * MetricsCollectorGithubWorkflows ) collectConsecutiveFailures (org string , repo * github.Repository , workflows * GithubWorkflowList , workflowRun []* github.WorkflowRun , callback chan <- func ()) {
555550 consecutiveFailuresMetric := m .Collector .GetMetricList ("workflowConsecutiveFailures" )
556551
557552 consecutiveFailMap := map [int64 ]* struct {
@@ -564,6 +559,11 @@ func (m *MetricsCollectorGithubWorkflows) collectConsecutiveFailures(org string,
564559 workflowRun := row
565560 workflowId := workflowRun .GetWorkflowID ()
566561
562+ workflow := workflows .GetById (workflowRun .GetWorkflowID ())
563+ if ! workflow .IsActive () {
564+ continue
565+ }
566+
567567 // ignore running/not finished workflow runs
568568 switch workflowRun .GetStatus () {
569569 case "in_progress" :
@@ -592,10 +592,7 @@ func (m *MetricsCollectorGithubWorkflows) collectConsecutiveFailures(org string,
592592 }
593593
594594 // add workflow labels
595- if workflow , ok := workflows [workflowRun .GetWorkflowID ()]; ok {
596- infoLabels ["workflow" ] = workflow .GetName ()
597- infoLabels ["workflowUrl" ] = workflow .GetHTMLURL ()
598- }
595+ workflow .AddDefaultLabels (infoLabels )
599596
600597 consecutiveFailMap [workflowId ] = & struct {
601598 count int64
@@ -628,7 +625,7 @@ func (m *MetricsCollectorGithubWorkflows) collectConsecutiveFailures(org string,
628625 }
629626}
630627
631- func (m * MetricsCollectorGithubWorkflows ) collectConsecutiveRuns (org string , repo * github.Repository , workflows map [ int64 ] * github. Workflow , workflowRun []* github.WorkflowRun , callback chan <- func ()) {
628+ func (m * MetricsCollectorGithubWorkflows ) collectConsecutiveRuns (org string , repo * github.Repository , workflows * GithubWorkflowList , workflowRun []* github.WorkflowRun , callback chan <- func ()) {
632629 consecutiveRunsMetric := m .Collector .GetMetricList ("workflowConsecutiveRuns" )
633630
634631 consecutiveRunMap := map [int64 ]* struct {
@@ -642,6 +639,11 @@ func (m *MetricsCollectorGithubWorkflows) collectConsecutiveRuns(org string, rep
642639 workflowRun := row
643640 workflowId := workflowRun .GetWorkflowID ()
644641
642+ workflow := workflows .GetById (workflowRun .GetWorkflowID ())
643+ if ! workflow .IsActive () {
644+ continue
645+ }
646+
645647 if _ , exists := consecutiveRunMap [workflowId ]; ! exists {
646648 infoLabels := prometheus.Labels {
647649 "org" : org ,
@@ -657,10 +659,7 @@ func (m *MetricsCollectorGithubWorkflows) collectConsecutiveRuns(org string, rep
657659 }
658660
659661 // add workflow labels
660- if workflow , ok := workflows [workflowRun .GetWorkflowID ()]; ok {
661- infoLabels ["workflow" ] = workflow .GetName ()
662- infoLabels ["workflowUrl" ] = workflow .GetHTMLURL ()
663- }
662+ workflow .AddDefaultLabels (infoLabels )
664663
665664 consecutiveRunMap [workflowId ] = & struct {
666665 count int64
0 commit comments