diff --git a/changelog/unreleased/workflow-run-created-metric.md b/changelog/unreleased/workflow-run-created-metric.md new file mode 100644 index 0000000..1fcfa78 --- /dev/null +++ b/changelog/unreleased/workflow-run-created-metric.md @@ -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 diff --git a/docs/partials/metrics.md b/docs/partials/metrics.md index 6cb8c4d..e4c1a66 100644 --- a/docs/partials/metrics.md +++ b/docs/partials/metrics.md @@ -254,4 +254,4 @@ github_workflow_duration_ms{, , , , , , , , } -: Status of workflow runs +: Status of workflow runs \ No newline at end of file diff --git a/pkg/command/command.go b/pkg/command/command.go index d70a0dc..957fd32 100644 --- a/pkg/command/command.go +++ b/pkg/command/command.go @@ -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, diff --git a/pkg/config/config.go b/pkg/config/config.go index 4844476..4cb03f3 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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. diff --git a/pkg/exporter/workflow.go b/pkg/exporter/workflow.go index f819f87..a3591fb 100644 --- a/pkg/exporter/workflow.go +++ b/pkg/exporter/workflow.go @@ -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. @@ -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"), @@ -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, + ), } } @@ -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. @@ -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()), } @@ -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..., + ) } } @@ -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, },