Skip to content

Commit 3cf038a

Browse files
committed
refactoring status filtering
Signed-off-by: Markus Blaschke <mail@markus-blaschke.de>
1 parent 34f06cc commit 3cf038a

4 files changed

Lines changed: 59 additions & 32 deletions

File tree

‎README.md‎

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,42 @@ Usage:
1414
github-workflow-exporter [OPTIONS]
1515
1616
Application Options:
17-
--log.level=[trace|debug|info|warning|error] Log level (default: info) [$LOG_LEVEL]
18-
--log.format=[logfmt|json] Log format (default: logfmt) [$LOG_FORMAT]
19-
--log.source=[|short|file|full] Show source for every log message (useful for debugging and bug reports) [$LOG_SOURCE]
20-
--log.color=[|auto|yes|no] Enable color for logs [$LOG_COLOR]
21-
--log.time Show log time [$LOG_TIME]
22-
--github.enterprise.url= GitHub enterprise url (self hosted) [$GITHUB_ENTERPRISE_URL]
23-
--github.organization= GitHub organization name [$GITHUB_ORGANIZATION]
24-
--github.token= GitHub token auth: PAT [$GITHUB_TOKEN]
25-
--github.app.id= GitHub app auth: App ID [$GITHUB_APP_ID]
26-
--github.app.installationid= GitHub app auth: App installation ID [$GITHUB_APP_INSTALLATION_ID]
27-
--github.app.keyfile= GitHub app auth: Private key (path to file) [$GITHUB_APP_PRIVATE_KEY]
28-
--github.repository.customprops= GitHub repository custom properties as labels for repos and workflows (space delimiter) [$GITHUB_REPOSITORY_CUSTOMPROPS]
29-
--github.workflows.timeframe= GitHub workflow timeframe for fetching (default: 168h) [$GITHUB_WORKFLOWS_TIMEFRAME]
30-
--scrape.time= Scrape time (default: 30m) [$SCRAPE_TIME]
31-
--cache.path= Cache path (to folder, file://path... or azblob://storageaccount.blob.core.windows.net/containername or k8scm://{namespace}/{configmap}}) [$CACHE_PATH]
32-
--server.bind= Server address (default: :8080) [$SERVER_BIND]
33-
--server.timeout.read= Server read timeout (default: 5s) [$SERVER_TIMEOUT_READ]
34-
--server.timeout.write= Server write timeout (default: 10s) [$SERVER_TIMEOUT_WRITE]
17+
--log.level=[trace|debug|info|warning|error] Log level (default: info) [$LOG_LEVEL]
18+
--log.format=[logfmt|json] Log format (default: logfmt) [$LOG_FORMAT]
19+
--log.source=[|short|file|full] Show source for every log message (useful for debugging and bug reports) [$LOG_SOURCE]
20+
--log.color=[|auto|yes|no] Enable color for logs [$LOG_COLOR]
21+
--log.time Show log time [$LOG_TIME]
22+
--github.enterprise.url= GitHub enterprise url (self hosted) [$GITHUB_ENTERPRISE_URL]
23+
--github.organization= GitHub organization name [$GITHUB_ORGANIZATION]
24+
--github.token= GitHub token auth: PAT [$GITHUB_TOKEN]
25+
--github.app.id= GitHub app auth: App ID [$GITHUB_APP_ID]
26+
--github.app.installationid= GitHub app auth: App installation ID [$GITHUB_APP_INSTALLATION_ID]
27+
--github.app.keyfile= GitHub app auth: Private key (path to file) [$GITHUB_APP_PRIVATE_KEY]
28+
--github.repository.customprops= GitHub repository custom properties as labels for repos and workflows (space delimiter)
29+
[$GITHUB_REPOSITORY_CUSTOMPROPS]
30+
--github.workflows.timeframe= GitHub workflow timeframe for fetching (default: 168h) [$GITHUB_WORKFLOWS_TIMEFRAME]
31+
--github.workflows.run_running.status= workflow statuses for github_workflow_run_running metric (others will be ignored, *
32+
allows all) (default: in_progress, queued, waiting, pending)
33+
[$GITHUB_WORKFLOWS_RUN_RUNNING_STATUS]
34+
--github.workflows.latest_run.status= workflow statuses for github_workflow_run_running metric (others will be ignored, *
35+
allows all) (default: completed, expected, failure, startup_failure)
36+
[$GITHUB_WORKFLOWS_LATEST_RUN_STATUS]
37+
--github.workflows.consecutive_failed.status= workflow statuses for github_workflow_consecutive_failed_runs metric (others will be
38+
ignored, * allows all) (default: completed, expected, failure, startup_failure)
39+
[$GITHUB_WORKFLOWS_CONSECUTIVE_FAILED_STATUS]
40+
--github.workflows.consecutive_runs.status= workflow statuses for github_workflow_consecutive_runs metric (others will be ignored,
41+
* allows all) (default: completed, expected, failure, startup_failure, queued, waiting,
42+
pending) [$GITHUB_WORKFLOWS_CONSECUTIVE_RUNS_STATUS]
43+
--scrape.time= Scrape time (default: 30m) [$SCRAPE_TIME]
44+
--cache.path= Cache path (to folder, file://path... or
45+
azblob://storageaccount.blob.core.windows.net/containername or
46+
k8scm://{namespace}/{configmap}}) [$CACHE_PATH]
47+
--server.bind= Server address (default: :8080) [$SERVER_BIND]
48+
--server.timeout.read= Server read timeout (default: 5s) [$SERVER_TIMEOUT_READ]
49+
--server.timeout.write= Server write timeout (default: 10s) [$SERVER_TIMEOUT_WRITE]
3550
3651
Help Options:
37-
-h, --help Show this help message
52+
-h, --help Show this help message
3853
```
3954

4055
### Authentication

‎config/opts.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ type (
3939
Workflows struct {
4040
Timeframe time.Duration `long:"github.workflows.timeframe" env:"GITHUB_WORKFLOWS_TIMEFRAME" description:"GitHub workflow timeframe for fetching" default:"168h"`
4141

42-
RunRunningIgnoreStatus []string `long:"github.workflows.run_running.ignorestatus" env:"GITHUB_WORKFLOWS_RUN_RUNNING_IGNORESTATUS" description:"Ignore workflow statuses for github_workflow_run_running metric" env-delim:" " default:"in_progress" default:"action_required" default:"queued" default:"waiting" default:"pending"` //nolint:staticcheck // ignore duplicate default tag
43-
RunLatestIgnoreStatus []string `long:"github.workflows.latest_run.ignorestatus" env:"GITHUB_WORKFLOWS_LATEST_RUN_IGNORESTATUS" description:"Ignore workflow statuses for github_workflow_run_running metric" env-delim:" " default:"in_progress" default:"action_required" default:"queued" default:"waiting" default:"pending"` //nolint:staticcheck // ignore duplicate default tag
44-
ConsecutiveFailuresIgnoreStatus []string `long:"github.workflows.consecutive_failed.ignorestatus" env:"GITHUB_WORKFLOWS_CONSECUTIVE_FAILED_IGNORESTATUS" description:"Ignore workflow statuses for github_workflow_consecutive_failed_runs metric" env-delim:" " default:"in_progress" default:"action_required" default:"queued" default:"waiting" default:"pending"` //nolint:staticcheck // ignore duplicate default tag
45-
ConsecutiveRunIgnoreStatus []string `long:"github.workflows.consecutive_runs.ignorestatus" env:"GITHUB_WORKFLOWS_CONSECUTIVE_RUNS_IGNORESTATUS" description:"Ignore workflow statuses for github_workflow_consecutive_runs metric" env-delim:" " default:"in_progress" default:"action_required"` //nolint:staticcheck // ignore duplicate default tag
42+
RunRunningStatus []string `long:"github.workflows.run_running.status" env:"GITHUB_WORKFLOWS_RUN_RUNNING_STATUS" description:"workflow statuses for github_workflow_run_running metric (others will be ignored, * allows all)" env-delim:" " default:"in_progress" default:"queued" default:"waiting" default:"pending"` //nolint:staticcheck // ignore duplicate default tag
43+
RunLatestStatus []string `long:"github.workflows.latest_run.status" env:"GITHUB_WORKFLOWS_LATEST_RUN_STATUS" description:"workflow statuses for github_workflow_run_running metric (others will be ignored, * allows all)" env-delim:" " default:"completed" default:"expected" default:"failure" default:"startup_failure"` //nolint:staticcheck // ignore duplicate default tag
44+
ConsecutiveFailuresStatus []string `long:"github.workflows.consecutive_failed.status" env:"GITHUB_WORKFLOWS_CONSECUTIVE_FAILED_STATUS" description:"workflow statuses for github_workflow_consecutive_failed_runs metric (others will be ignored, * allows all)" env-delim:" " default:"completed" default:"expected" default:"failure" default:"startup_failure"` //nolint:staticcheck // ignore duplicate default tag
45+
ConsecutiveRunStatus []string `long:"github.workflows.consecutive_runs.status" env:"GITHUB_WORKFLOWS_CONSECUTIVE_RUNS_STATUS" description:"workflow statuses for github_workflow_consecutive_runs metric (others will be ignored, * allows all)" env-delim:" " default:"completed" default:"expected" default:"failure" default:"startup_failure" default:"queued" default:"waiting" default:"pending"` //nolint:staticcheck // ignore duplicate default tag
4646
}
4747
}
4848

‎metrics_github_workflows.go‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/prometheus/client_golang/prometheus"
1212
"github.com/webdevops/go-common/prometheus/collector"
1313
"github.com/webdevops/go-common/utils/to"
14-
"golang.org/x/exp/slices"
1514
)
1615

1716
const (
@@ -431,8 +430,8 @@ func (m *MetricsCollectorGithubWorkflows) collectRunningRuns(org string, repo *g
431430
for _, row := range workflowRunList.List() {
432431
workflowRun := row
433432

434-
// ignore non-running workflows //nolint
435-
if !slices.Contains(Opts.GitHub.Workflows.RunRunningIgnoreStatus, strings.ToLower(workflowRun.GetStatus())) {
433+
// skip non-valid workflow statuses
434+
if !workflowRun.HasStatus(Opts.GitHub.Workflows.RunRunningStatus) {
436435
continue
437436
}
438437

@@ -492,8 +491,8 @@ func (m *MetricsCollectorGithubWorkflows) collectLatestRun(org string, repo *git
492491
continue
493492
}
494493

495-
// ignore non-running workflows //nolint
496-
if !slices.Contains(Opts.GitHub.Workflows.RunLatestIgnoreStatus, strings.ToLower(workflowRun.GetStatus())) {
494+
// skip non-valid workflow statuses
495+
if !workflowRun.HasStatus(Opts.GitHub.Workflows.RunLatestStatus) {
497496
continue
498497
}
499498

@@ -560,8 +559,8 @@ func (m *MetricsCollectorGithubWorkflows) collectConsecutiveFailures(org string,
560559
continue
561560
}
562561

563-
// ignore non-running workflows //nolint
564-
if !slices.Contains(Opts.GitHub.Workflows.ConsecutiveFailuresIgnoreStatus, strings.ToLower(workflowRun.GetStatus())) {
562+
// skip non-valid workflow statuses
563+
if !workflowRun.HasStatus(Opts.GitHub.Workflows.ConsecutiveFailuresStatus) {
565564
continue
566565
}
567566

@@ -631,8 +630,8 @@ func (m *MetricsCollectorGithubWorkflows) collectConsecutiveRuns(org string, rep
631630
continue
632631
}
633632

634-
// ignore non-running workflows //nolint
635-
if !slices.Contains(Opts.GitHub.Workflows.ConsecutiveRunIgnoreStatus, strings.ToLower(workflowRun.GetStatus())) {
633+
// skip non-valid workflow statuses
634+
if !workflowRun.HasStatus(Opts.GitHub.Workflows.ConsecutiveRunStatus) {
636635
continue
637636
}
638637

‎workflow_run.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"strings"
5+
46
"github.com/google/go-github/v92/github"
57
)
68

@@ -30,10 +32,21 @@ func (list *GithubWorkflowRunList) Add(workflowRun ...*github.WorkflowRun) {
3032
list.list = append(list.list, &GitHubWorkflowRun{item, list})
3133
}
3234
}
35+
3336
func (list *GithubWorkflowRunList) Count() int {
3437
return len(list.list)
3538
}
3639

3740
func (list *GithubWorkflowRunList) List() []*GitHubWorkflowRun {
3841
return list.list
3942
}
43+
44+
func (run *GitHubWorkflowRun) HasStatus(statuses []string) bool {
45+
for _, status := range statuses {
46+
if status == "*" || strings.EqualFold(status, run.GetStatus()) {
47+
return true
48+
}
49+
}
50+
51+
return false
52+
}

0 commit comments

Comments
 (0)