-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
client.go
151 lines (129 loc) · 3.54 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
package metric
import (
"time"
"github.com/influxdata/influxdb/kit/platform/errors"
"github.com/prometheus/client_golang/prometheus"
)
// REDClient is a metrics client for collection RED metrics.
type REDClient struct {
metrics []metricCollector
}
// New creates a new REDClient.
func New(reg prometheus.Registerer, service string, opts ...ClientOptFn) *REDClient {
opt := metricOpts{
namespace: "service",
service: service,
counterMetrics: map[string]VecOpts{
"call_total": {
Help: "Number of calls",
LabelNames: []string{"method"},
CounterFn: func(vec *prometheus.CounterVec, o CollectFnOpts) {
vec.With(prometheus.Labels{"method": o.Method}).Inc()
},
},
"error_total": {
Help: "Number of errors encountered",
LabelNames: []string{"method", "code"},
CounterFn: func(vec *prometheus.CounterVec, o CollectFnOpts) {
if o.Err != nil {
vec.With(prometheus.Labels{
"method": o.Method,
"code": errors.ErrorCode(o.Err),
}).Inc()
}
},
},
},
histogramMetrics: map[string]VecOpts{
"duration": {
Help: "Duration of calls",
LabelNames: []string{"method"},
HistogramFn: func(vec *prometheus.HistogramVec, o CollectFnOpts) {
vec.
With(prometheus.Labels{"method": o.Method}).
Observe(time.Since(o.Start).Seconds())
},
},
},
}
for _, o := range opts {
o(&opt)
}
client := new(REDClient)
for metricName, vecOpts := range opt.counterMetrics {
client.metrics = append(client.metrics, &counter{
fn: vecOpts.CounterFn,
CounterVec: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: opt.namespace,
Subsystem: opt.serviceName(),
Name: metricName,
Help: vecOpts.Help,
}, vecOpts.LabelNames),
})
}
for metricName, vecOpts := range opt.histogramMetrics {
client.metrics = append(client.metrics, &histogram{
fn: vecOpts.HistogramFn,
HistogramVec: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: opt.namespace,
Subsystem: opt.serviceName(),
Name: metricName,
Help: vecOpts.Help,
}, vecOpts.LabelNames),
})
}
reg.MustRegister(client.collectors()...)
return client
}
type RecordFn func(err error, opts ...func(opts *CollectFnOpts)) error
// RecordAdditional provides an extension to the base method, err data provided
// to the metrics.
func RecordAdditional(props map[string]interface{}) func(opts *CollectFnOpts) {
return func(opts *CollectFnOpts) {
opts.AdditionalProps = props
}
}
// Record returns a record fn that is called on any given return err. If an error is encountered
// it will register the err metric. The err is never altered.
func (c *REDClient) Record(method string) RecordFn {
start := time.Now()
return func(err error, opts ...func(opts *CollectFnOpts)) error {
opt := CollectFnOpts{
Method: method,
Start: start,
Err: err,
}
for _, o := range opts {
o(&opt)
}
for _, metric := range c.metrics {
metric.collect(opt)
}
return err
}
}
func (c *REDClient) collectors() []prometheus.Collector {
var collectors []prometheus.Collector
for _, metric := range c.metrics {
collectors = append(collectors, metric)
}
return collectors
}
type metricCollector interface {
prometheus.Collector
collect(o CollectFnOpts)
}
type counter struct {
*prometheus.CounterVec
fn CounterFn
}
func (c *counter) collect(o CollectFnOpts) {
c.fn(c.CounterVec, o)
}
type histogram struct {
*prometheus.HistogramVec
fn HistogramFn
}
func (h *histogram) collect(o CollectFnOpts) {
h.fn(h.HistogramVec, o)
}