Skip to content

Commit

Permalink
Merge pull request #200 from ramir-savvy/workflows_status
Browse files Browse the repository at this point in the history
Added workflow run created duration metric and query configs
  • Loading branch information
tboerger committed Jun 5, 2023
2 parents 9ae676e + 418d0a8 commit 467ffb2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
10 changes: 10 additions & 0 deletions changelog/unreleased/workflow-run-created-metric.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Enhancement: New metrics and configurations for the workflow collector

1. Added a new metric for the duration, in minutes, of the time since the run was created:
github_workflow_duration_run_created_minutes
2. Added 2 optional configuration options for the workflows exporter:
1. Query workflows with a specific status (Default to any)
2. Set the window history (defaults to 12 hours)
3. Added run_id label

https://github.com/promhippie/github_exporter/pull/200
2 changes: 1 addition & 1 deletion docs/partials/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,4 @@ github_workflow_duration_ms{<prometheus.ConstrainedLabel Value>, <prometheus.Con
: Duration of workflow runs

github_workflow_status{<prometheus.ConstrainedLabel Value>, <prometheus.ConstrainedLabel Value>, <prometheus.ConstrainedLabel Value>, <prometheus.ConstrainedLabel Value>, <prometheus.ConstrainedLabel Value>, <prometheus.ConstrainedLabel Value>, <prometheus.ConstrainedLabel Value>, <prometheus.ConstrainedLabel Value>}
: Status of workflow runs
: Status of workflow runs
14 changes: 14 additions & 0 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ func RootFlags(cfg *config.Config) []cli.Flag {
EnvVars: []string{"GITHUB_EXPORTER_COLLECTOR_WORKFLOWS"},
Destination: &cfg.Collector.Workflows,
},
&cli.StringFlag{
Name: "collector.workflows.status",
Value: "",
Usage: "Query workflows with specific status",
EnvVars: []string{"GITHUB_EXPORTER_WORKFLOWS_STATUS"},
Destination: &cfg.Target.WorkflowsCfg.Status,
},
&cli.DurationFlag{
Name: "collector.workflows.history-window",
Value: 12 * time.Hour,
Usage: "Duration for querying workflows since the time they were created",
EnvVars: []string{"GITHUB_EXPORTER_WORKFLOWS_HISTORY_WINDOW"},
Destination: &cfg.Target.WorkflowsCfg.HistoryWindow,
},
&cli.BoolFlag{
Name: "collector.runners",
Value: false,
Expand Down
29 changes: 18 additions & 11 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@ type Logs struct {
Pretty bool
}

// WorkflowsExporterConfig defines the workflow exporter specific configuration.
type WorkflowsExporterConfig struct {
Status string
HistoryWindow time.Duration
}

// Target defines the target specific configuration.
type Target struct {
Token string
PrivateKey string
AppID int64
InstallID int64
BaseURL string
Insecure bool
Enterprises cli.StringSlice
Orgs cli.StringSlice
Repos cli.StringSlice
Timeout time.Duration
PerPage int
Token string
PrivateKey string
AppID int64
InstallID int64
BaseURL string
Insecure bool
Enterprises cli.StringSlice
Orgs cli.StringSlice
Repos cli.StringSlice
Timeout time.Duration
PerPage int
WorkflowsCfg WorkflowsExporterConfig
}

// Collector defines the collector specific configuration.
Expand Down
25 changes: 21 additions & 4 deletions pkg/exporter/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ type WorkflowCollector struct {
duration *prometheus.HistogramVec
config config.Target

Status *prometheus.Desc
Duration *prometheus.Desc
Status *prometheus.Desc
Duration *prometheus.Desc
RunCreation *prometheus.Desc
}

// NewWorkflowCollector returns a new WorkflowCollector.
Expand All @@ -33,7 +34,7 @@ func NewWorkflowCollector(logger log.Logger, client *github.Client, failures *pr
failures.WithLabelValues("action").Add(0)
}

labels := []string{"owner", "repo", "event", "name", "status", "head_branch", "run", "retry"}
labels := []string{"owner", "repo", "event", "name", "status", "head_branch", "run", "run_id", "retry"}
return &WorkflowCollector{
client: client,
logger: log.With(logger, "collector", "workflow"),
Expand All @@ -53,6 +54,12 @@ func NewWorkflowCollector(logger log.Logger, client *github.Client, failures *pr
labels,
nil,
),
RunCreation: prometheus.NewDesc(
"github_workflow_duration_run_created_minutes",
"Duration since the workflow run creation time in minutes",
labels,
nil,
),
}
}

Expand All @@ -68,6 +75,7 @@ func (c *WorkflowCollector) Metrics() []*prometheus.Desc {
func (c *WorkflowCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.Status
ch <- c.Duration
ch <- c.RunCreation
}

// Collect is called by the Prometheus registry when collecting metrics.
Expand Down Expand Up @@ -111,6 +119,7 @@ func (c *WorkflowCollector) Collect(ch chan<- prometheus.Metric) {
record.GetStatus(),
record.GetHeadBranch(),
strconv.Itoa(record.GetRunNumber()),
strconv.FormatInt(record.GetID(), 10),
strconv.Itoa(record.GetRunAttempt()),
}

Expand All @@ -127,6 +136,13 @@ func (c *WorkflowCollector) Collect(ch chan<- prometheus.Metric) {
float64((record.GetUpdatedAt().Time.Unix()-record.GetCreatedAt().Time.Unix())*1000),
labels...,
)

ch <- prometheus.MustNewConstMetric(
c.RunCreation,
prometheus.GaugeValue,
time.Since(record.GetRunStartedAt().Time).Minutes(),
labels...,
)
}
}

Expand Down Expand Up @@ -208,11 +224,12 @@ func (c *WorkflowCollector) repoWorkflows() []*github.WorkflowRun {

func (c *WorkflowCollector) pagedRepoWorkflows(ctx context.Context, owner, name string) ([]*github.WorkflowRun, error) {
startWindow := time.Now().Add(
time.Duration(-12) * time.Hour,
-c.config.WorkflowsCfg.HistoryWindow,
).Format(time.RFC3339)

opts := &github.ListWorkflowRunsOptions{
Created: fmt.Sprintf(">=%s", startWindow),
Status: c.config.WorkflowsCfg.Status,
ListOptions: github.ListOptions{
PerPage: c.config.PerPage,
},
Expand Down

0 comments on commit 467ffb2

Please sign in to comment.