/
client.go
169 lines (147 loc) · 4.59 KB
/
client.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2021 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package prometrics
import (
"flag"
"fmt"
"net/http"
"github.com/grailbio/infra"
"github.com/grailbio/reflow/log"
"github.com/grailbio/reflow/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func init() {
infra.Register("prometrics", new(Client))
}
type Client struct {
// Namespace is given as a prefix to all prometheus metrics.
Namespace string
// Port is the port to serve Prometheus metrics. If zero is passed,
// the metrics HTTP server isn't started.
Port int
// NodeExporterPort is the port to serve node_exporter metrics on the
// reflowlet. If zero is passed, the node_exporter daemon isn't
// enabled.
NodeExporterPort int
reg prometheus.Registerer
gath prometheus.Gatherer
gauges map[string]*prometheus.GaugeVec
counters map[string]*prometheus.CounterVec
histograms map[string]*prometheus.HistogramVec
}
// String implements infra.Provider.
func (r *Client) String() string {
return fmt.Sprintf("%T,Port=%d", r, r.Port)
}
// Help implements infra.Provider.
func (r *Client) Help() string {
return "configure a prometheus metrics Client hosted on the given port"
}
// Init implements infra.Provider.
func (r *Client) Init() error {
if r.Port == r.NodeExporterPort {
return fmt.Errorf("port (%d) and nodemetricsport (%d) must be different", r.Port, r.NodeExporterPort)
}
reg := prometheus.NewRegistry()
r.reg = reg
r.gath = reg
r.init()
if r.Port != 0 {
go func() {
log.Printf("hosting prometheus at %d", r.Port)
handler := promhttp.HandlerFor(r.gath, promhttp.HandlerOpts{})
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", r.Port), handler))
}()
}
return nil
}
// Flags implements infra.Provider.
func (r *Client) Flags(flags *flag.FlagSet) {
flags.IntVar(&r.Port, "port", 0, "port to serve metrics (http server disabled if zero passed)")
flags.IntVar(&r.NodeExporterPort, "nodeexporterport", 9101, "port to serve node_exporter metrics "+
"(node_exporter disabled if zero passed)")
flags.StringVar(&r.Namespace, "namespace", "reflow", "namespace to prepend to metrics")
}
// init inspects the counters/gauges/histograms defined on the root metrics implementation
// and initializes their backing stores in the prometheus registry on the new Client. It should only
// be called once.
func (r *Client) init() {
r.gauges = make(map[string]*prometheus.GaugeVec)
r.counters = make(map[string]*prometheus.CounterVec)
r.histograms = make(map[string]*prometheus.HistogramVec)
for name, opts := range metrics.Gauges {
gv := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: r.Namespace,
Name: name,
Help: opts.Help,
}, opts.Labels)
r.gauges[name] = gv
if err := r.reg.Register(gv); err != nil {
log.Fatal(err)
}
}
for name, opts := range metrics.Counters {
cv := prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: r.Namespace,
Name: name,
Help: opts.Help,
}, opts.Labels)
r.counters[name] = cv
if err := r.reg.Register(cv); err != nil {
log.Fatal(err)
}
}
for name, opts := range metrics.Histograms {
hv := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: r.Namespace,
Name: name,
Buckets: opts.Buckets,
Help: opts.Help,
}, opts.Labels)
r.histograms[name] = hv
if err := r.reg.Register(hv); err != nil {
log.Fatal(err)
}
}
r.reg.MustRegister(
prometheus.NewGoCollector(),
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{Namespace: r.Namespace}),
)
}
func (r *Client) GetGauge(name string, labels map[string]string) metrics.Gauge {
cv := r.gauges[name]
gauge, err := cv.GetMetricWith(labels)
if err != nil {
log.Fatal(err)
}
return gauge
}
func (r *Client) GetCounter(name string, labels map[string]string) metrics.Counter {
cv := r.counters[name]
counter, err := cv.GetMetricWith(labels)
if err != nil {
log.Fatal(err)
}
return counter
}
func (r *Client) GetHistogram(name string, labels map[string]string) metrics.Histogram {
hv := r.histograms[name]
histogram, err := hv.GetMetricWith(labels)
if err != nil {
log.Fatal(err)
}
return histogram
}
// NewClient returns a prometheus metrics Client that wraps the existing registry.
func NewClient(reg prometheus.Registerer, gath prometheus.Gatherer, nodeExporterPort int) metrics.Client {
r := &Client{
reg: reg,
gath: gath,
Namespace: "reflow",
NodeExporterPort: nodeExporterPort,
}
r.init()
return r
}