-
Notifications
You must be signed in to change notification settings - Fork 6
/
prom.go
93 lines (82 loc) · 3.06 KB
/
prom.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
// Package prometheusx provides a canonical way to expose Prometheus metrics.
package prometheusx
import (
"flag"
"log"
"net/http"
"net/http/pprof"
"strconv"
"github.com/m-lab/go/httpx"
"github.com/m-lab/go/rtx"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
// GitShortCommit holds the truncated commit id for a git commit. It should be
// equal to the first column of the output of `git log --oneline`. This string
// is interpreted by init() as a base-16 number and the Prometheus metric
// git_short_commit is set to the resulting numerical value. It is recommended
// that the string be set as part of the build/link process, as follows:
//
// go build -ldflags "-X prometheusx.GitShortCommit=$(git log -1 --format=%h)" ./...
//
// This metric should be useful when determining whether code on various
// systems is running the same version, which should, among other things, help
// detect failed rollouts, or extended periods in which test deployments occur
// but never a production deployment.
GitShortCommit = "No commit specified"
gitShortCommitMetric = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "git_short_commit",
Help: "The git commit interpreted as a number.",
},
[]string{"commit"})
)
var (
// ListenAddress is a package flag to specify the prometheus metric
// server listen address.
ListenAddress = flag.String("prometheusx.listen-address", ":9990", "")
)
func setCommitNumber(commit string) {
number, err := strconv.ParseInt(commit, 16, 64)
if err == nil {
gitShortCommitMetric.WithLabelValues(commit).Set(float64(number))
} else {
gitShortCommitMetric.WithLabelValues(commit).Set(0)
}
}
func init() {
setCommitNumber(GitShortCommit)
}
// MustServeMetrics starts the prometheus http metrics server with the
// package flag ListenAddress.
func MustServeMetrics() *http.Server {
return MustStartPrometheus(*ListenAddress)
}
// MustStartPrometheus starts an http server which exposes local metrics to
// Prometheus. If the passed-in address is ":0" then a random open port will
// be chosen and the .Addr element of the returned server will be udpated to
// reflect the actual port.
func MustStartPrometheus(addr string) *http.Server {
if addr != *ListenAddress {
log.Printf("WARNING: Starting metrics server with %q when flag is %s", addr, *ListenAddress)
log.Printf("WARNING: Update code to use `prometheusx.MustStartMetricsServer()`")
}
// Prometheus with some extras.
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.Handle("/metrics", promhttp.Handler())
// Start up the http server.
server := &http.Server{
Addr: addr,
Handler: mux,
}
rtx.Must(httpx.ListenAndServeAsync(server), "Could not start metric server")
// Return the server
return server
}