Skip to content

Commit

Permalink
Merge pull request #405 from marcusramberg/marcus/workflow_queue_time
Browse files Browse the repository at this point in the history
  • Loading branch information
tboerger authored Oct 19, 2024
2 parents 365d4e0 + aba8b5d commit 41856bd
Show file tree
Hide file tree
Showing 12 changed files with 886 additions and 46 deletions.
60 changes: 47 additions & 13 deletions pkg/action/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func Server(cfg *config.Config, db store.Store, logger *slog.Logger) error {
)

client, err := getClient(cfg, logger)

if err != nil {
return err
}
Expand Down Expand Up @@ -188,6 +187,19 @@ func handler(cfg *config.Config, db store.Store, logger *slog.Logger, client *gi
))
}

if cfg.Collector.WorkflowJobs {
logger.Debug("WorkflowJob collector registered")

registry.MustRegister(exporter.NewWorkflowJobCollector(
logger,
client,
db,
requestFailures,
requestDuration,
cfg.Target,
))
}

reg := promhttp.HandlerFor(
registry,
promhttp.HandlerOpts{
Expand All @@ -202,10 +214,9 @@ func handler(cfg *config.Config, db store.Store, logger *slog.Logger, client *gi
mux.Route("/", func(root chi.Router) {
root.Handle(cfg.Server.Path, reg)

if cfg.Collector.Workflows {
if cfg.Collector.Workflows || cfg.Collector.WorkflowJobs {
root.HandleFunc(cfg.Webhook.Path, func(w http.ResponseWriter, r *http.Request) {
secret, err := config.Value(cfg.Webhook.Secret)

if err != nil {
logger.Error("Failed to read webhook secret",
"error", err,
Expand All @@ -221,7 +232,6 @@ func handler(cfg *config.Config, db store.Store, logger *slog.Logger, client *gi
r,
[]byte(secret),
)

if err != nil {
logger.Error("Failed to parse github webhook",
"error", err,
Expand All @@ -237,7 +247,6 @@ func handler(cfg *config.Config, db store.Store, logger *slog.Logger, client *gi
github.WebHookType(r),
payload,
)

if err != nil {
logger.Error("Failed to parse github event",
"error", err,
Expand Down Expand Up @@ -280,6 +289,39 @@ func handler(cfg *config.Config, db store.Store, logger *slog.Logger, client *gi
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusInternalServerError)

io.WriteString(w, http.StatusText(http.StatusInternalServerError))
}
case *github.WorkflowJobEvent:
wfJob := event.GetWorkflowJob()
logger.Debug("received webhook request",
"type", "workflow_job",
"owner", event.GetRepo().GetOwner().GetLogin(),
"repo", event.GetRepo().GetName(),
"id", wfJob.GetID(),
"name", wfJob.GetName(),
"attempt", wfJob.GetRunAttempt(),
"status", wfJob.GetStatus(),
"conclusion", wfJob.GetConclusion(),
"created_at", wfJob.GetCreatedAt().Time.Unix(),
"started_at", wfJob.GetStartedAt().Time.Unix(),
"completed_at", wfJob.GetCompletedAt().Time.Unix(),
"labels", strings.Join(wfJob.Labels, ", "),
)

if err := db.StoreWorkflowJobEvent(event); err != nil {
logger.Error(
"failed to store github event",
"type", "workflow_job",
"owner", event.GetRepo().GetOwner().GetLogin(),
"repo", event.GetRepo().GetName(),
"name", wfJob.GetName(),
"id", wfJob.GetID(),
"error", err,
)

w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusInternalServerError)

io.WriteString(w, http.StatusText(http.StatusInternalServerError))
}
}
Expand Down Expand Up @@ -324,7 +366,6 @@ func getClient(cfg *config.Config, logger *slog.Logger) (*github.Client, error)

if useApplication(cfg, logger) {
privateKey, err := config.Value(cfg.Target.PrivateKey)

if err != nil {
logger.Error("Failed to read GitHub key",
"err", err,
Expand All @@ -339,7 +380,6 @@ func getClient(cfg *config.Config, logger *slog.Logger) (*github.Client, error)
cfg.Target.InstallID,
[]byte(privateKey),
)

if err != nil {
logger.Error("Failed to create GitHub transport",
"err", err,
Expand All @@ -356,7 +396,6 @@ func getClient(cfg *config.Config, logger *slog.Logger) (*github.Client, error)
}

accessToken, err := config.Value(cfg.Target.Token)

if err != nil {
logger.Error("Failed to read token",
"err", err,
Expand All @@ -380,7 +419,6 @@ func getClient(cfg *config.Config, logger *slog.Logger) (*github.Client, error)
func getEnterprise(cfg *config.Config, logger *slog.Logger) (*github.Client, error) {
if useApplication(cfg, logger) {
privateKey, err := config.Value(cfg.Target.PrivateKey)

if err != nil {
logger.Error("Failed to read GitHub key",
"err", err,
Expand All @@ -395,7 +433,6 @@ func getEnterprise(cfg *config.Config, logger *slog.Logger) (*github.Client, err
cfg.Target.InstallID,
[]byte(privateKey),
)

if err != nil {
logger.Error("Failed to create GitHub transport",
"err", err,
Expand All @@ -419,7 +456,6 @@ func getEnterprise(cfg *config.Config, logger *slog.Logger) (*github.Client, err
cfg.Target.BaseURL,
cfg.Target.BaseURL,
)

if err != nil {
logger.Error("Failed to parse base URL",
"err", err,
Expand All @@ -432,7 +468,6 @@ func getEnterprise(cfg *config.Config, logger *slog.Logger) (*github.Client, err
}

accessToken, err := config.Value(cfg.Target.Token)

if err != nil {
logger.Error("Failed to read token",
"err", err,
Expand Down Expand Up @@ -464,7 +499,6 @@ func getEnterprise(cfg *config.Config, logger *slog.Logger) (*github.Client, err
cfg.Target.BaseURL,
cfg.Target.BaseURL,
)

if err != nil {
logger.Error("Failed to parse base URL",
"err", err,
Expand Down
21 changes: 21 additions & 0 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,27 @@ func RootFlags(cfg *config.Config) []cli.Flag {
EnvVars: []string{"GITHUB_EXPORTER_WORKFLOWS_LABELS"},
Destination: &cfg.Target.Workflows.Labels,
},
&cli.BoolFlag{
Name: "collector.workflow_jobs",
Value: false,
Usage: "Enable collector for workflow jobs",
EnvVars: []string{"GITHUB_EXPORTER_COLLECTOR_WORKFLOW_JOBS"},
Destination: &cfg.Collector.WorkflowJobs,
},
&cli.DurationFlag{
Name: "collector.workflow_jobs.window",
Value: 24 * time.Hour,
Usage: "History window for querying workflow jobs",
EnvVars: []string{"GITHUB_EXPORTER_WORKFLOW_JOBS_WINDOW"},
Destination: &cfg.Target.WorkflowJobs.Window,
},
&cli.StringSliceFlag{
Name: "collector.workflow_jobs.labels",
Value: config.JobLabels(),
Usage: "List of labels used for workflow jobs",
EnvVars: []string{"GITHUB_EXPORTER_WORKFLOWS_LABELS"},
Destination: &cfg.Target.WorkflowJobs.Labels,
},
&cli.BoolFlag{
Name: "collector.runners",
Value: false,
Expand Down
63 changes: 42 additions & 21 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,44 @@ type Workflows struct {
Labels cli.StringSlice
}

// WorkflowJobs defines the workflow job specific configuration.
type WorkflowJobs struct {
Window time.Duration
Labels cli.StringSlice
}

// Runners defines the runner specific configuration.
type Runners struct {
Labels cli.StringSlice
}

// 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
Workflows Workflows
Runners Runners
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
Workflows Workflows
WorkflowJobs WorkflowJobs
Runners Runners
}

// Collector defines the collector specific configuration.
type Collector struct {
Admin bool
Orgs bool
Repos bool
Billing bool
Workflows bool
Runners bool
Admin bool
Orgs bool
Repos bool
Billing bool
Workflows bool
WorkflowJobs bool
Runners bool
}

// Database defines the database specific configuration.
Expand Down Expand Up @@ -104,6 +112,21 @@ func Labels() *cli.StringSlice {
)
}

// JobLabels defines the default labels used by workflow_job collector.
func JobLabels() *cli.StringSlice {
return cli.NewStringSlice(
"owner",
"repo",
"name",
"title",
"branch",
"sha",
"run_id",
"run_attempt",
"labels",
)
}

// RunnerLabels defines the default labels used by runner collector.
func RunnerLabels() *cli.StringSlice {
return cli.NewStringSlice(
Expand All @@ -117,7 +140,6 @@ func Value(val string) (string, error) {
content, err := os.ReadFile(
strings.TrimPrefix(val, "file://"),
)

if err != nil {
return "", fmt.Errorf("failed to parse secret file: %w", err)
}
Expand All @@ -129,7 +151,6 @@ func Value(val string) (string, error) {
content, err := base64.StdEncoding.DecodeString(
strings.TrimPrefix(val, "base64://"),
)

if err != nil {
return "", fmt.Errorf("failed to parse base64 value: %w", err)
}
Expand Down
Loading

0 comments on commit 41856bd

Please sign in to comment.