-
Notifications
You must be signed in to change notification settings - Fork 75
/
metrics.go
61 lines (51 loc) · 1.46 KB
/
metrics.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
package main
import (
"context"
"net/http"
"time"
"gollum/core"
promMetrics "github.com/CrowdStrike/go-metrics-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)
func startPrometheusMetricsService(address string) func() {
srv := &http.Server{Addr: address}
quit := make(chan struct{})
prometheusRegistry := prometheus.NewRegistry()
flushInterval := 3 * time.Second
promClient := promMetrics.NewPrometheusProvider(core.MetricsRegistry, "gollum", "", prometheusRegistry, flushInterval)
// Start updates
go func() {
for {
select {
case <-time.After(flushInterval):
if err := promClient.UpdatePrometheusMetricsOnce(); err != nil {
logrus.WithError(err).Warn("Error updating metrics")
}
case <-quit:
return
}
}
}()
// Start http
go func() {
opts := promhttp.HandlerOpts{
ErrorLog: logrus.StandardLogger(),
ErrorHandling: promhttp.ContinueOnError,
}
http.Handle("/prometheus", promhttp.HandlerFor(prometheusRegistry, opts))
err := srv.ListenAndServe()
if err != nil {
logrus.WithError(err).Error("Failed to start metrics http server")
}
}()
logrus.WithField("address", address).Info("Started metric service")
// Return stop function
return func() {
close(quit)
if err := srv.Shutdown(context.Background()); err != nil {
logrus.WithError(err).Error("Failed to shutdown metrics http server")
}
}
}