-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (70 loc) · 2.51 KB
/
Copy pathmain.go
File metadata and controls
83 lines (70 loc) · 2.51 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
76
77
78
79
80
81
82
83
package main
import (
"flag"
"net/http"
"os"
"time"
"github.com/peterbourgon/ff/v3"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
filterOption := level.AllowInfo()
logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = level.NewFilter(logger, filterOption)
logger = log.With(logger,
"ts", log.DefaultTimestampUTC,
"caller", log.DefaultCaller,
)
fs := flag.NewFlagSet("prometheus-browserless-exporter", flag.ExitOnError)
var (
listenAddr = fs.String("listen-addr", ":3001", "listen address")
timeoutSeconds = fs.Int("timeout", 5, "timeout in seconds")
debug = fs.Bool("debug", false, "log debug information")
browserlessEndpoint = fs.String("browserless-endpoint", "http://localhost:3000/metrics/total", "browserless metrics endpoint")
metricsPath = fs.String("metrics-endpoint", "/metrics", "path where prometheus metrics are going to be exposed")
)
if err := ff.Parse(fs, os.Args[1:],
ff.WithEnvVarNoPrefix(),
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
); err != nil {
level.Error(logger).Log("msg", "failed to parse flags", "err", err)
os.Exit(1)
}
if *debug {
filterOption = level.AllowDebug()
}
level.Info(logger).Log("msg", "starting prometheus-browserless-exporter")
timeout := time.Duration(*timeoutSeconds) * time.Millisecond
errors := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "browserless_exporter_errors_total",
Help: "The total number of errors per collector",
}, []string{"collector"})
// Client used to call metrics endpoints
httpClient := http.Client{
Timeout: time.Duration(*timeoutSeconds) * time.Second,
}
r := prometheus.NewRegistry()
r.MustRegister(errors)
r.MustRegister(NewBrowserlessTotalCollector(logger, httpClient, errors, timeout, *browserlessEndpoint))
http.Handle(*metricsPath,
promhttp.HandlerFor(r, promhttp.HandlerOpts{}),
)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>Browserless Exporter</title></head>
<body>
<h1>Browserless Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
</body>
</html>`))
})
level.Info(logger).Log("msg", "listening", "addr", *listenAddr)
if err := http.ListenAndServe(*listenAddr, nil); err != nil {
level.Error(logger).Log("msg", "http listenandserve error", "err", err)
os.Exit(1)
}
}