-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
metrics.go
79 lines (65 loc) · 2.08 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package metrics
import (
"fmt"
"net/http"
"net/http/pprof"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
const defaultMetricsPath = "/metrics"
// Options for initializing metrics collection.
type Options struct {
// Common prefix for the keys of the different
// collected metrics.
Prefix string
// EnableProfile exposes profiling information on /pprof of the
// metrics listener.
EnableProfile bool
// enables go runtime metrics
EnableRuntimeMetrics bool
// A new registry is created if this option is nil.
PrometheusRegistry *prometheus.Registry
}
func DefaultOptions() Options {
return Options{
Prefix: "",
EnableProfile: false,
EnableRuntimeMetrics: true,
PrometheusRegistry: nil,
}
}
// Metrics is the generic interface that all the required backends
// should implement to be a promcheck metrics compatible backend.
type Metrics interface {
SetRuleGroupsTotal(value float64)
SetRulesTotal(value float64)
SetSelectorsTotal(file, group, rule, status string, value float64)
RegisterHandler(path string, handler *http.ServeMux)
}
// NewDefaultHandler returns a default metrics handler.
func NewDefaultHandler(o Options) http.Handler {
m := NewPrometheus(o)
return HandlerFor(m, o)
}
// HandlerFor returns a collection of metrics handlers.
func HandlerFor(m Metrics, o Options) http.Handler {
mux := http.NewServeMux()
if o.EnableProfile {
mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
}
// Root path should return 404.
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
// Fix trailing slashes and register routes.
mPath := defaultMetricsPath
mPath = strings.TrimRight(mPath, "/")
m.RegisterHandler(mPath, mux)
mPath = fmt.Sprintf("%s/", mPath)
m.RegisterHandler(mPath, mux)
return mux
}