-
Notifications
You must be signed in to change notification settings - Fork 0
/
hznuoj_exporter.go
122 lines (101 loc) · 3.21 KB
/
hznuoj_exporter.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
package main
import (
"net/http"
"os"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
_ "github.com/go-sql-driver/mysql"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"hznuoj_exporter/collector"
)
var (
Namespace = ""
)
var (
webConfig = webflag.AddFlags(kingpin.CommandLine)
namespace = kingpin.Flag(
"metrics.namespace",
"Namespace of metrics",
).Default("hznuoj").String()
listenAddress = kingpin.Flag(
"web.listen-address",
"Address to listen on for web interface and telemetry.",
).Default(":9800").String()
metricPath = kingpin.Flag(
"web.telemetry-path",
"Path under which to expose metrics.",
).Default("/metrics").String()
dbType = kingpin.Flag(
"db.type",
"DB Type",
).Default("mysql").String()
dbConnectString = kingpin.Flag(
"db.connect.string",
"DB Connect String",
).Default("root:root@tcp(127.0.0.1:3306)/jol?charset=utf8&parseTime=True").String()
)
func init() {
prometheus.MustRegister(version.NewCollector(Namespace))
}
func newHandler(metrics collector.Metrics, scrapers []collector.Scraper, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
dsn := *dbConnectString
dbType := *dbType
registry := prometheus.NewRegistry()
registry.MustRegister(collector.New(ctx, dbType, dsn, metrics, scrapers, logger))
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry,
}
h := promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
}
func main() {
// Parse flags.
promlogConfig := &promlog.Config{}
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.Version(version.Print(Namespace))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(promlogConfig)
collector.Namespace = *namespace
Namespace = collector.Namespace + "_exporter"
level.Info(logger).Log("msg", "Starting "+Namespace, "version", version.Info())
level.Info(logger).Log("msg", "Build context", version.BuildContext())
var landingPage = []byte(`<html>
<head><title>` + Namespace + `</title></head>
<body>
<h1>` + Namespace + `</h1>
<p><a href='` + *metricPath + `'>Metrics</a></p>
</body>
</html>
`)
scrapers := []collector.Scraper{
collector.ProblemInfo{},
collector.UserInfo{},
collector.HitInfo{},
collector.LoginInfo{},
collector.ContestInfo{},
collector.SubmissionInfo{},
}
handlerFunc := newHandler(collector.NewMetrics(), scrapers, logger)
http.Handle(*metricPath, promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, handlerFunc))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(landingPage)
})
level.Info(logger).Log("msg", "Listening on address", "address", *listenAddress)
srv := &http.Server{Addr: *listenAddress}
if err := web.ListenAndServe(srv, *webConfig, logger); err != nil {
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
os.Exit(1)
}
}