-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (60 loc) · 2.64 KB
/
main.go
File metadata and controls
75 lines (60 loc) · 2.64 KB
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
package main
import (
"dev.volix.ops/thor/handler"
"dev.volix.ops/thor/pkg/slog"
"dev.volix.ops/thor/pkg/version"
"dev.volix.ops/thor/storage"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/route"
"gopkg.in/alecthomas/kingpin.v2"
"net/http"
"os"
)
func main() {
var (
app = kingpin.New("thor", "A Prometheus push and aggregation gateway.")
verbose = app.Flag("verbose", "Enable verbose/debug output.").Default("false").Bool()
listenAddress = app.Flag("web.listen-address", "Address and port to listen on.").Default(":9091").String()
metricsPath = app.Flag("web.metrics-path", "Path under which to expose metrics.").Default("/metrics").String()
skipConsistencyCheck = app.Flag("push.skip-consistency-check", "Skip consistency check, dangerous but faster.").Default("false").Bool()
)
kingpin.MustParse(app.Parse(os.Args[1:]))
if *verbose {
// we only support verbose or !verbose, as we don't need
// a more specific setting like debug, info, warn, ... level.
slog.SetVerbosity(1)
}
slog.Info("starting thor gateway version ", version.Version)
slog.Info("build context: ", version.BuildContext())
slog.Debug("listen address=", *listenAddress)
slog.Debug("metrics path=", *metricsPath)
ms := storage.NewMetricStorage()
r := route.New()
r.Get("/-/healthy", handler.Health(ms))
r.Get("/lore", handler.Lore())
// POST merges and adds to it and PUT replaces
for _, suffix := range []string{"", handler.Base64JobSuffix} {
isBase64 := suffix == handler.Base64JobSuffix
r.Post(*metricsPath+"/job"+suffix+"/:job/*labels", handler.Push(ms, isBase64, *skipConsistencyCheck, false))
r.Put(*metricsPath+"/job"+suffix+"/:job/*labels", handler.Push(ms, isBase64, *skipConsistencyCheck, true))
r.Del(*metricsPath+"/job"+suffix+"/:job/*labels", handler.Delete(ms, isBase64))
r.Post(*metricsPath+"/job"+suffix+"/:job", handler.Push(ms, isBase64, *skipConsistencyCheck,false))
r.Put(*metricsPath+"/job"+suffix+"/:job", handler.Push(ms, isBase64, *skipConsistencyCheck,true))
r.Del(*metricsPath+"/job"+suffix+"/:job", handler.Delete(ms, isBase64))
}
// create gatherer to serve /metrics page
g := prometheus.Gatherers{
prometheus.GathererFunc(func() ([]*dto.MetricFamily, error) { return ms.GetMetricFamilies(), nil }),
}
r.Get(*metricsPath, promhttp.HandlerFor(g, promhttp.HandlerOpts{}).ServeHTTP)
mux := http.NewServeMux()
mux.Handle("/", r)
server := &http.Server{
Addr: *listenAddress,
Handler: mux,
}
err := server.ListenAndServe()
slog.Error("http server stopped: ", err)
}