forked from xrstf/github_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
306 lines (247 loc) · 10.9 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"time"
"go.xrstf.de/github_exporter/pkg/client"
"go.xrstf.de/github_exporter/pkg/fetcher"
"go.xrstf.de/github_exporter/pkg/github"
"go.xrstf.de/github_exporter/pkg/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/shurcooL/githubv4"
"github.com/sirupsen/logrus"
)
type options struct {
repositories repositoryList
owner string
realnames bool
repoRefreshInterval time.Duration
prRefreshInterval time.Duration
prResyncInterval time.Duration
prDepth int
issueRefreshInterval time.Duration
issueResyncInterval time.Duration
issueDepth int
milestoneRefreshInterval time.Duration
milestoneResyncInterval time.Duration
milestoneDepth int
listenAddr string
debugLog bool
}
type AppContext struct {
ctx context.Context
client *client.Client
fetcher *fetcher.Fetcher
options *options
}
func main() {
opt := options{
repoRefreshInterval: 5 * time.Minute,
prRefreshInterval: 5 * time.Minute,
prResyncInterval: 12 * time.Hour,
prDepth: -1,
issueRefreshInterval: 5 * time.Minute,
issueResyncInterval: 12 * time.Hour,
issueDepth: -1,
milestoneRefreshInterval: 5 * time.Minute,
milestoneResyncInterval: 12 * time.Hour,
milestoneDepth: -1,
listenAddr: ":9612",
}
flag.Var(&opt.repositories, "repo", "repository (owner/name format) to include, can be given multiple times")
flag.StringVar(&opt.owner, "owner", opt.owner, "github login (username or organization) of the owner of the repositories that will be included. Excludes forked and locked repo, includes 100 first private & public repos")
flag.BoolVar(&opt.realnames, "realnames", opt.realnames, "use usernames instead of internal IDs for author labels (this will make metrics contain personally identifiable information)")
flag.DurationVar(&opt.repoRefreshInterval, "repo-refresh-interval", opt.repoRefreshInterval, "time in between repository metadata refreshes")
flag.IntVar(&opt.prDepth, "pr-depth", opt.prDepth, "max number of pull requests to fetch per repository upon startup (-1 disables the limit, 0 disables PR fetching entirely)")
flag.DurationVar(&opt.prRefreshInterval, "pr-refresh-interval", opt.prRefreshInterval, "time in between PR refreshes")
flag.DurationVar(&opt.prResyncInterval, "pr-resync-interval", opt.prResyncInterval, "time in between full PR re-syncs")
flag.IntVar(&opt.issueDepth, "issue-depth", opt.issueDepth, "max number of issues to fetch per repository upon startup (-1 disables the limit, 0 disables issue fetching entirely)")
flag.DurationVar(&opt.issueRefreshInterval, "issue-refresh-interval", opt.issueRefreshInterval, "time in between issue refreshes")
flag.DurationVar(&opt.issueResyncInterval, "issue-resync-interval", opt.issueResyncInterval, "time in between full issue re-syncs")
flag.IntVar(&opt.milestoneDepth, "milestone-depth", opt.milestoneDepth, "max number of milestones to fetch per repository upon startup (-1 disables the limit, 0 disables milestone fetching entirely)")
flag.DurationVar(&opt.milestoneRefreshInterval, "milestone-refresh-interval", opt.milestoneRefreshInterval, "time in between milestone refreshes")
flag.DurationVar(&opt.milestoneResyncInterval, "milestone-resync-interval", opt.milestoneResyncInterval, "time in between full milestone re-syncs")
flag.StringVar(&opt.listenAddr, "listen", opt.listenAddr, "address and port to listen on")
flag.BoolVar(&opt.debugLog, "debug", opt.debugLog, "enable more verbose logging")
flag.Parse()
// setup logging
var log = logrus.New()
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.RFC1123,
})
if opt.debugLog {
log.SetLevel(logrus.DebugLevel)
}
// validate CLI flags
if opt.owner == "" && len(opt.repositories) == 0 {
log.Fatal("No -repo nor -owner defined.")
}
if opt.prRefreshInterval >= opt.prResyncInterval {
log.Fatal("-pr-refresh-interval must be < than -pr-resync-interval.")
}
if opt.issueRefreshInterval >= opt.issueResyncInterval {
log.Fatal("-issue-refresh-interval must be < than -issue-resync-interval.")
}
if opt.milestoneRefreshInterval >= opt.milestoneResyncInterval {
log.Fatal("-milestone-refresh-interval must be < than -milestone-resync-interval.")
}
token := os.Getenv("GITHUB_TOKEN")
if len(token) == 0 {
log.Fatal("No GITHUB_TOKEN environment variable defined.")
}
// setup API client
ctx := context.Background()
client, err := client.NewClient(ctx, log.WithField("component", "client"), token, opt.realnames)
if err != nil {
log.Fatalf("Failed to create API client: %v", err)
}
appCtx := AppContext{
ctx: ctx,
client: client,
options: &opt,
}
// start fetching data in the background, but start metrics
// server as soon as possible
go setup(appCtx, log)
log.Printf("Starting server on %s…", opt.listenAddr)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(opt.listenAddr, nil))
}
func setup(ctx AppContext, log logrus.FieldLogger) {
repositories := map[string]*github.Repository{}
if ctx.options.owner != "" {
log.Infof("Fetching all repositories for %s", ctx.options.owner)
repoNames, err := ctx.client.RepositoriesNames(ctx.options.owner)
if err != nil {
log.Fatalf("Failed to recover repositories: %v", err)
}
for _, repoName := range repoNames {
repositories[fmt.Sprintf("%s/%s", ctx.options.owner, repoName)] = github.NewRepository(ctx.options.owner, repoName)
}
}
// create a PR database for each repo
for _, repo := range ctx.options.repositories {
repositories[repo.String()] = github.NewRepository(repo.owner, repo.name)
}
// setup the single-threaded fetcher
ctx.fetcher = fetcher.NewFetcher(ctx.client, repositories, log.WithField("component", "fetcher"))
go ctx.fetcher.Worker()
prometheus.MustRegister(metrics.NewCollector(repositories, ctx.fetcher, ctx.client))
// perform the initial scan sequentially across all repositories, otherwise
// it's likely that we trigger GitHub's anti abuse system
log.Info("Initializing repositories…")
hasLabelledMetrics := ctx.options.prDepth != 0 || ctx.options.issueDepth != 0 || ctx.options.milestoneDepth != 0
for identifier, repo := range repositories {
repoLog := log.WithField("repo", identifier)
repoLog.Info("Scheduling initial scans…")
ctx.fetcher.EnqueueRepoUpdate(repo)
// keep repository metadata up-to-date
go refreshRepositoryInfoWorker(ctx, repoLog, repo)
if hasLabelledMetrics {
ctx.fetcher.EnqueueLabelUpdate(repo)
}
if ctx.options.prDepth != 0 {
ctx.fetcher.EnqueuePullRequestScan(repo, ctx.options.prDepth)
// keep refreshing open PRs
go refreshPullRequestsWorker(ctx, repoLog, repo)
// in a much larger interval, crawl all existing PRs to detect deletions and changes
// after a PR has been merged
go resyncPullRequestsWorker(ctx, repoLog, repo)
}
if ctx.options.issueDepth != 0 {
ctx.fetcher.EnqueueIssueScan(repo, ctx.options.issueDepth)
// keep refreshing open issues
go refreshIssuesWorker(ctx, repoLog, repo)
// in a much larger interval, crawl all existing issues to detect status changes
go resyncIssuesWorker(ctx, repoLog, repo)
}
if ctx.options.milestoneDepth != 0 {
ctx.fetcher.EnqueueMilestoneScan(repo, ctx.options.milestoneDepth)
// keep refreshing open milestones
go refreshMilestonesWorker(ctx, repoLog, repo)
// in a much larger interval, crawl all existing milestones to detect status changes
go resyncMilestonesWorker(ctx, repoLog, repo)
}
}
}
func refreshRepositoryInfoWorker(ctx AppContext, log logrus.FieldLogger, repo *github.Repository) {
for range time.NewTicker(ctx.options.repoRefreshInterval).C {
log.Debug("Refreshing repository metadata…")
ctx.fetcher.EnqueueRepoUpdate(repo)
}
}
// refreshRepositoriesWorker refreshes all OPEN pull requests, because changes
// to the build contexts do not change the updatedAt timestamp on GitHub and we
// want to closely track the mergability. It also fetches the last 50 updated
// PRs to find cases where a PR was merged and is not open anymore.
func refreshPullRequestsWorker(ctx AppContext, log logrus.FieldLogger, repo *github.Repository) {
for range time.NewTicker(ctx.options.prRefreshInterval).C {
log.Debug("Refreshing open pull requests…")
numbers := []int{}
for _, pr := range repo.GetPullRequests(githubv4.PullRequestStateOpen) {
numbers = append(numbers, pr.Number)
}
ctx.fetcher.EnqueuePriorityPullRequests(repo, numbers)
ctx.fetcher.EnqueueUpdatedPullRequests(repo)
}
}
func resyncPullRequestsWorker(ctx AppContext, log logrus.FieldLogger, repo *github.Repository) {
for range time.NewTicker(ctx.options.prResyncInterval).C {
log.Info("Synchronizing repository pull requests…")
numbers := []int{}
for _, pr := range repo.GetPullRequests(githubv4.PullRequestStateClosed, githubv4.PullRequestStateMerged) {
numbers = append(numbers, pr.Number)
}
ctx.fetcher.EnqueueRegularPullRequests(repo, numbers)
ctx.fetcher.EnqueueLabelUpdate(repo)
}
}
func refreshIssuesWorker(ctx AppContext, log logrus.FieldLogger, repo *github.Repository) {
for range time.NewTicker(ctx.options.issueRefreshInterval).C {
log.Debug("Refreshing open pull issues…")
numbers := []int{}
for _, issue := range repo.GetIssues(githubv4.IssueStateOpen) {
numbers = append(numbers, issue.Number)
}
ctx.fetcher.EnqueuePriorityIssues(repo, numbers)
ctx.fetcher.EnqueueUpdatedIssues(repo)
}
}
func resyncIssuesWorker(ctx AppContext, log logrus.FieldLogger, repo *github.Repository) {
for range time.NewTicker(ctx.options.issueResyncInterval).C {
log.Info("Synchronizing repository issues…")
numbers := []int{}
for _, issue := range repo.GetIssues(githubv4.IssueStateClosed) {
numbers = append(numbers, issue.Number)
}
ctx.fetcher.EnqueueRegularIssues(repo, numbers)
ctx.fetcher.EnqueueLabelUpdate(repo)
}
}
func refreshMilestonesWorker(ctx AppContext, log logrus.FieldLogger, repo *github.Repository) {
for range time.NewTicker(ctx.options.milestoneRefreshInterval).C {
log.Debug("Refreshing open pull milestones…")
numbers := []int{}
for _, milestone := range repo.GetMilestones(githubv4.MilestoneStateOpen) {
numbers = append(numbers, milestone.Number)
}
ctx.fetcher.EnqueuePriorityMilestones(repo, numbers)
ctx.fetcher.EnqueueUpdatedMilestones(repo)
}
}
func resyncMilestonesWorker(ctx AppContext, log logrus.FieldLogger, repo *github.Repository) {
for range time.NewTicker(ctx.options.milestoneResyncInterval).C {
log.Info("Synchronizing repository milestones…")
numbers := []int{}
for _, milestone := range repo.GetMilestones(githubv4.MilestoneStateClosed) {
numbers = append(numbers, milestone.Number)
}
ctx.fetcher.EnqueueRegularMilestones(repo, numbers)
ctx.fetcher.EnqueueLabelUpdate(repo)
}
}