-
Notifications
You must be signed in to change notification settings - Fork 24
/
metrics.go
44 lines (39 loc) · 1.03 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
package servicegetter
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/fluxninja/aperture/v2/pkg/metrics"
)
// Metrics is used for collecting metrics about servicegetter
//
// nil value of Metrics should be always usable.
type Metrics struct {
okTotal prometheus.Counter
errorsTotal prometheus.Counter
}
// NewMetrics creates new Metrics, registering counters in given registry.
func NewMetrics(registry *prometheus.Registry) (*Metrics, error) {
total := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: metrics.ServiceLookupsMetricName,
Help: "Number of IP to services lookups",
},
[]string{metrics.ServiceLookupsStatusLabel},
)
if err := registry.Register(total); err != nil {
return nil, err
}
return &Metrics{
okTotal: total.WithLabelValues(metrics.ServiceLookupsStatusOK),
errorsTotal: total.WithLabelValues(metrics.ServiceLookupsStatusError),
}, nil
}
func (m *Metrics) inc(ok bool) {
if m == nil {
return
}
if ok {
m.okTotal.Inc()
} else {
m.errorsTotal.Inc()
}
}