forked from SkySoft-ATM/gorillaz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheus.go
66 lines (56 loc) · 1.83 KB
/
prometheus.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
package gorillaz
import (
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// InitPrometheus registers Prometheus handler to path to expose metrics via HTTP
func (g *Gaz) InitPrometheus(path string) {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
Sugar.Infof("Setup Prometheus handler at %s", path)
handler := promhttp.InstrumentMetricHandler(
g.prometheusRegistry, promhttp.HandlerFor(g.prometheusRegistry, promhttp.HandlerOpts{}),
)
g.Router.Handle(path, handler).Methods("GET")
// export uptime as a prometheus counter
upCounter := prometheus.NewCounter(prometheus.CounterOpts{
Name: "uptime_sec",
Help: "uptime in seconds",
})
// register the application build version so we can collect it in Prometheus
buildVersion := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "app_info",
Help: "application build information",
ConstLabels: prometheus.Labels{"version": ApplicationVersion, "name": ApplicationName, "description": ApplicationDescription},
})
g.prometheusRegistry.MustRegister(upCounter)
g.prometheusRegistry.MustRegister(buildVersion)
// the actual value is set in "version" label
buildVersion.Set(1)
go func() {
t := time.NewTicker(time.Second)
for {
<-t.C
upCounter.Inc()
}
}()
}
// return true if collector was successfully registered
func (g *Gaz) RegisterCollector(c prometheus.Collector) error {
err := g.prometheusRegistry.Register(c)
if err != nil {
return errors.Wrap(err, "Could not register prometheus collector")
}
return nil
}
// register the collector successfully or panic
func (g *Gaz) MustRegisterCollector(c prometheus.Collector) {
err := g.prometheusRegistry.Register(c)
if err != nil {
panic("could not register collector, " + err.Error())
}
}