forked from cpanato/github_actions_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (76 loc) · 3.14 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
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/cpanato/github_actions_exporter/internal/server"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9101").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
ghWebHookPath = kingpin.Flag("web.gh-webhook-path", "Path that will be called by the GitHub webhook.").Default("/gh_event").String()
githubWebhookToken = kingpin.Flag("gh.github-webhook-token", "GitHub Webhook Token.").Envar("GITHUB_WEBHOOK_TOKEN").Default("").String()
gitHubAPIToken = kingpin.Flag("gh.github-api-token", "GitHub API Token.").Envar("GITHUB_API_TOKEN").Default("").String()
gitHubOrg = kingpin.Flag("gh.github-org", "GitHub Organization.").Default("").String()
gitHubUser = kingpin.Flag("gh.github-user", "GitHub User.").Default("").String()
gitHubBillingPollingSeconds = kingpin.Flag("gh.billing-poll-seconds", "Frequency at which to poll billing API.").Default("5").Int()
)
func init() {
prometheus.MustRegister(version.NewCollector("ghactions_exporter"))
}
func main() {
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.Version(version.Print("ghactions_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
_ = level.Info(logger).Log("msg", "Starting ghactions_exporter", "version", version.Info())
_ = level.Info(logger).Log("build_context", version.BuildContext())
if err := validateFlags(*githubWebhookToken); err != nil {
_ = level.Error(logger).Log("msg", "Missing configure flags", "err", err)
os.Exit(1)
}
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
srv := server.NewServer(logger, server.Opts{
WebhookPath: *ghWebHookPath,
ListenAddress: *listenAddress,
MetricsPath: *metricsPath,
GitHubToken: *githubWebhookToken,
GitHubAPIToken: *gitHubAPIToken,
GitHubUser: *gitHubUser,
GitHubOrg: *gitHubOrg,
BillingAPIPollSeconds: *gitHubBillingPollingSeconds,
})
go func() {
err := srv.Serve(context.Background())
if err != nil {
_ = level.Error(logger).Log("msg", "Server closed", "err", err)
} else {
_ = level.Info(logger).Log("msg", "Server closed")
}
}()
_ = level.Info(logger).Log("msg", fmt.Sprintf("Signal received: %v. Exiting...", <-signalChan))
err := srv.Shutdown(context.Background())
if err != nil {
_ = level.Error(logger).Log("msg", "Error occurred while closing the server", "err", err)
os.Exit(1)
}
os.Exit(0)
}
func validateFlags(token string) error {
if token == "" {
return errors.New("please configure the GitHub Webhook Token")
}
return nil
}