-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
354 lines (308 loc) · 10.1 KB
/
collector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package main
import (
"context"
"log"
"net/http"
"path"
"strings"
"time"
"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/google/go-github/v60/github"
"github.com/gravitational/trace"
"github.com/hashicorp/go-retryablehttp"
"github.com/prometheus/client_golang/prometheus"
)
var (
workflowRunnerSecondsVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gha_workflow_runner_seconds",
},
[]string{"repo", "ref", "event_type", "workflow"},
)
workflowElapsedSecondsVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gha_workflow_elapsed_seconds",
},
[]string{"repo", "ref", "event_type", "workflow"},
)
jobRunnerSecondsVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gha_job_runner_seconds",
},
[]string{"repo", "ref", "event_type", "workflow", "job"},
)
stepRunnerSecondsVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gha_step_runner_seconds",
},
[]string{"repo", "ref", "event_type", "workflow", "job", "step"},
)
workflowRunCountVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gha_workflow_run_count",
},
[]string{"repo", "ref", "event_type", "workflow", "conclusion"},
)
jobRunCountVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gha_job_run_count",
},
[]string{"repo", "ref", "event_type", "workflow", "job", "conclusion"},
)
stepRunCountVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gha_step_run_count",
},
[]string{"repo", "ref", "event_type", "workflow", "job", "step", "conclusion"},
)
)
type Collector struct {
cfg *CLI
client *github.Client
// seenRuns tracks which completed workflows runs we've seen so we don't count
// them again.
seenRuns map[int64]bool
// oldestIncomplete tracks per-repo the earliest start time of a workflow run that we've
// seen that was not completed. This becomes the cutoff for querying github
// each collect loop.
oldestIncomplete map[string]time.Time
}
func NewCollector(cfg *CLI) *Collector {
prometheus.MustRegister(workflowRunnerSecondsVec)
prometheus.MustRegister(workflowElapsedSecondsVec)
prometheus.MustRegister(jobRunnerSecondsVec)
prometheus.MustRegister(stepRunnerSecondsVec)
prometheus.MustRegister(workflowRunCountVec)
prometheus.MustRegister(jobRunCountVec)
prometheus.MustRegister(stepRunCountVec)
return &Collector{
cfg: cfg,
seenRuns: make(map[int64]bool),
oldestIncomplete: make(map[string]time.Time),
}
}
func (c *Collector) Run(ctx context.Context) {
log.Print("Collector started")
defer func() { log.Print("Collector finished: ", ctx.Err()) }()
for ; ctx.Err() == nil; time.Sleep(c.cfg.Sleep) {
for _, repo := range c.cfg.Repos {
if err := c.collectRepo(ctx, repo); err != nil {
log.Print(err)
}
}
}
}
func (c *Collector) collectRepo(ctx context.Context, repo string) error {
log.Printf("Started collecting repo: %s", repo)
defer func() { log.Printf("Finished collecting repo: %s", repo) }()
// Recreate client on demand after errors and on startup
if c.client == nil {
client, err := newGHClient(c.cfg.Owner, c.cfg.AppID, []byte(c.cfg.AppKey))
if err != nil {
return trace.Wrap(err, "collection failed for repo: %q", repo)
}
c.client = client
}
ignoreCompleted := false
cutoff, ok := c.oldestIncomplete[repo]
if !ok {
cutoff = time.Now().UTC().Add(-c.cfg.InitialWindow)
// This is the first time collecting from this repo. We normally ignore
// completed runs and only collect run time from runs that complete
// after we have started, but if the `--backfill` flag is given, then
// we collect run times from all completed runs in the initial window.
ignoreCompleted = !c.cfg.Backfill
}
created := cutoff.Format("created:>=2006-01-02T15:04:05+00:00")
opts := &github.ListWorkflowRunsOptions{
Created: created,
ListOptions: github.ListOptions{PerPage: 100},
}
actions := c.client.Actions
newCutoff := time.Now().UTC()
for {
runs, response, err := actions.ListRepositoryWorkflowRuns(ctx, c.cfg.Owner, repo, opts)
if err != nil {
return trace.Wrap(err, "failed to list workflow runs")
}
for _, run := range runs.WorkflowRuns {
newCutoff, err = c.collectRun(ctx, repo, run, newCutoff, ignoreCompleted)
if err != nil {
return trace.Wrap(err, "failed to collect workflow runs")
}
}
opts.Page = response.NextPage
if opts.Page == 0 {
break
}
}
c.oldestIncomplete[repo] = newCutoff
return nil
}
func (c *Collector) collectRun(ctx context.Context, repo string, run *github.WorkflowRun, cutoff time.Time, ignoreCompleted bool) (time.Time, error) {
var err error
switch {
case run.GetStatus() != "completed":
switch {
case run.CreatedAt == nil:
// Ignore runs without a created timestamp
case run.CreatedAt.Before(cutoff):
cutoff = run.CreatedAt.Time
log.Printf("open: %s/%d: %s (new cutoff: %v)",
repo, run.GetID(), run.GetName(), cutoff)
default:
log.Printf("open: %s/%d: %s", repo, run.GetID(), run.GetName())
}
case c.seenRuns[run.GetID()]:
log.Printf("seen: %s/%d: %s", repo, run.GetID(), run.GetName())
default:
if !ignoreCompleted {
err = c.collectJobs(ctx, repo, run)
}
if err == nil {
c.seenRuns[run.GetID()] = true
log.Printf("done: %s/%d: %s\n", repo, run.GetID(), run.GetName())
}
}
return cutoff, err
}
func (c *Collector) collectJobs(ctx context.Context, repo string, run *github.WorkflowRun) error {
opts := &github.ListWorkflowJobsOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
actions := c.client.Actions
for {
jobs, response, err := actions.ListWorkflowJobs(ctx, c.cfg.Owner, repo, run.GetID(), opts)
if err != nil {
return trace.Wrap(err, "failed to list workflow run jobs")
}
countJobs(run, jobs.Jobs)
opts.Page = response.NextPage
if opts.Page == 0 {
break
}
}
return nil
}
func countJobs(run *github.WorkflowRun, jobs []*github.WorkflowJob) {
workflowName := path.Base(run.GetPath())
workflowName = strings.TrimSuffix(workflowName, path.Ext(workflowName))
repo := run.GetRepository().GetName()
ref := makeRef(run)
eventType := run.GetEvent()
var workflowRunTime time.Duration
for _, job := range jobs {
var jobRunTime time.Duration
for _, step := range job.Steps {
stepRunCountVec.WithLabelValues(
repo, ref, eventType, workflowName, job.GetName(), step.GetName(), step.GetConclusion(),
).Add(1)
if step.GetConclusion() != "success" {
continue
}
if step.StartedAt == nil || step.CompletedAt == nil {
continue
}
stepRunTime := step.CompletedAt.Time.Sub(step.StartedAt.Time)
stepRunnerSecondsVec.WithLabelValues(
repo, ref, eventType, workflowName, job.GetName(), step.GetName(),
).Add(stepRunTime.Seconds())
jobRunTime += stepRunTime
}
jobRunCountVec.WithLabelValues(
repo, ref, eventType, workflowName, job.GetName(), job.GetConclusion(),
).Add(1)
if job.GetConclusion() != "success" {
continue
}
jobRunnerSecondsVec.WithLabelValues(
repo, ref, eventType, workflowName, job.GetName(),
).Add(jobRunTime.Seconds())
workflowRunTime += jobRunTime
}
// The tool doesn't currently account for more than one run attempt.
// This brings a multitude of issues that are near-impossible to
// account for due to GH's API design.
if run.GetRunAttempt() > 1 {
return
}
workflowRunCountVec.WithLabelValues(repo, ref, eventType, workflowName, run.GetConclusion()).Add(1)
if run.GetConclusion() != "success" {
return
}
workflowRunnerSecondsVec.WithLabelValues(repo, ref, eventType, workflowName).Add(workflowRunTime.Seconds())
workflowElapsedSecondsVec.WithLabelValues(repo, ref, eventType, workflowName).Add(run.GetUpdatedAt().Sub(run.GetCreatedAt().Time).Seconds())
}
func makeRef(run *github.WorkflowRun) string {
eventName := run.GetEvent()
if strings.HasPrefix(eventName, "pull_request") {
// Attempt to tie the workflow to a PR
headSha := run.GetHeadSHA()
headBranch := run.GetHeadBranch()
if headSha == "" && headBranch == "" {
return "error-missing-head-ref"
}
for _, pr := range run.PullRequests {
prHeadBranch := pr.GetHead()
if prHeadBranch.GetSHA() == headSha ||
prHeadBranch.GetRef() == headBranch {
return pr.GetBase().GetRef()
}
}
return headBranch
}
if strings.HasPrefix(eventName, "merge_group") {
headBranch := run.GetHeadBranch()
if headBranch == "" {
return "error-head-branch-is-nil"
}
mergeBranch := strings.TrimPrefix(headBranch, "gh-readonly-queue/")
mergeBranch = strings.SplitN(mergeBranch, "/pr-", 2)[0]
return mergeBranch
}
headBranch := run.GetHeadBranch()
if headBranch == "" {
return "error-head-branch-is-nil"
}
return headBranch
}
func newGHClient(owner string, appID int64, appKey []byte) (*github.Client, error) {
// Create a retryable http client / roundtripper, but turn off logging
// for now as it is a bit noisy. We should use a logger that implements
// retryablehttp.LevelledLogger so we can turn on/off debug logging as needed.
rtclient := retryablehttp.NewClient()
rtclient.Logger = nil // too noisy right now
rt := &retryablehttp.RoundTripper{Client: rtclient}
// Create a temporary github client that can list the app installations
// so we can get the installation ID for the proper github client.
tmpTransport, err := ghinstallation.NewAppsTransport(rt, appID, appKey)
if err != nil {
return nil, err
}
gh := github.NewClient(&http.Client{Transport: tmpTransport})
var instID int64
listOptions := github.ListOptions{PerPage: 100}
findInst:
for {
installations, response, err := gh.Apps.ListInstallations(context.Background(), &listOptions)
if err != nil {
return nil, trace.Wrap(err, "Failed to list installations")
}
for _, inst := range installations {
if inst.GetAccount().GetLogin() == owner {
instID = inst.GetID()
break findInst
}
}
if response.NextPage == 0 {
return nil, trace.NotFound("No such installation found")
}
listOptions.Page = response.NextPage
}
transport, err := ghinstallation.New(rt, appID, instID, appKey)
if err != nil {
return nil, trace.Wrap(err, "Failed creating authenticated transport")
}
gh = github.NewClient(&http.Client{Transport: transport})
return gh, nil
}