This repository has been archived by the owner on Oct 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
117 lines (101 loc) · 2.86 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
package main
import (
"context"
"io/ioutil"
"net/http"
"os"
"runtime"
"strings"
"time"
arg "github.com/alexflint/go-arg"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/joho/godotenv"
"github.com/metalmatze/githubql_exporter/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/shurcooL/githubql"
"golang.org/x/oauth2"
)
var (
// Version of githubql_exporter.
Version string
// Revision or Commit this binary was built from.
Revision string
// BuildDate this binary was built.
BuildDate string
// GoVersion running this binary.
GoVersion = runtime.Version()
// StartTime has the time this was started.
StartTime = time.Now()
)
// Config gets its content from env and passes it on to different packages
type Config struct {
Debug bool `arg:"env:DEBUG"`
GitHubToken string `arg:"env:GITHUB_TOKEN"`
Orgs string `arg:"env:ORGS"`
WebAddr string `arg:"env:WEB_ADDR"`
WebPath string `arg:"env:WEB_PATH"`
MaxRepos int `arg:"env:MAX_REPOS"`
}
// Token returns a token or an error.
func (c Config) Token() oauth2.TokenSource {
return oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: c.GitHubToken},
)
}
func main() {
_ = godotenv.Load()
c := Config{
WebPath: "/metrics",
WebAddr: ":9276",
MaxRepos: 100,
}
arg.MustParse(&c)
if tokfile := os.Getenv("GITHUB_TOKEN_FILE"); c.GitHubToken == "" && tokfile != "" {
t, err := ioutil.ReadFile(tokfile)
if err != nil {
panic(err)
}
c.GitHubToken = strings.TrimSpace(string(t))
}
if c.GitHubToken == "" {
panic("GITHUB_TOKEN is required")
}
filterOption := level.AllowInfo()
if c.Debug {
filterOption = level.AllowDebug()
}
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = level.NewFilter(logger, filterOption)
logger = log.With(logger,
"ts", log.DefaultTimestampUTC,
"caller", log.DefaultCaller,
)
level.Info(logger).Log(
"msg", "starting githubql_exporter",
"version", Version,
"revision", Revision,
"buildDate", BuildDate,
"goVersion", GoVersion,
)
httpClient := oauth2.NewClient(context.Background(), c.Token())
client := githubql.NewClient(httpClient)
organizations := strings.Split(c.Orgs, ",")
prometheus.MustRegister(collector.NewOrganizationCollector(logger, client, organizations, c.MaxRepos))
http.Handle(c.WebPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>GitHubQL Exporter</title></head>
<body>
<h1>GitHubQL Exporter</h1>
<p><a href="` + c.WebPath + `">Metrics</a></p>
</body>
</html>`))
})
level.Info(logger).Log("msg", "listening", "addr", c.WebAddr)
if err := http.ListenAndServe(c.WebAddr, nil); err != nil {
level.Error(logger).Log("msg", "http listen and serve error", "err", err)
os.Exit(1)
}
}