-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmetrics.go
153 lines (131 loc) · 3.83 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
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
package monitor
import "github.com/prometheus/client_golang/prometheus"
const (
// defaultNamespace is default metric namespace.
defaultNamespace = ""
// defaultSubsystem is default metric subsystem.
defaultSubsystem = "go_pg"
)
// Metrics represent a set of Prometheus metrics for database client stats.
type Metrics struct {
namespace string
subsystem string
constLabels prometheus.Labels
hits *prometheus.GaugeVec
misses *prometheus.GaugeVec
timeouts *prometheus.GaugeVec
totalConns *prometheus.GaugeVec
idleConns *prometheus.GaugeVec
staleConns *prometheus.GaugeVec
registerer prometheus.Registerer
}
// MetricsOption is an option for NewMetrics.
type MetricsOption func(metrics *Metrics)
// MetricsWithNamespace is an option that sets metric namespace.
func MetricsWithNamespace(namespace string) MetricsOption {
return func(metrics *Metrics) {
metrics.namespace = namespace
}
}
// MetricsWithSubsystem is an option that sets metric subsystem.
func MetricsWithSubsystem(subsystem string) MetricsOption {
return func(metrics *Metrics) {
metrics.subsystem = subsystem
}
}
// MetricsWithConstLabels is an option that sets metric constant labels.
func MetricsWithConstLabels(constLabels prometheus.Labels) MetricsOption {
return func(metrics *Metrics) {
metrics.constLabels = constLabels
}
}
// MetricsWithRegisterer is an option that sets custom registerer for metrics.
func MetricsWithRegisterer(registerer prometheus.Registerer) MetricsOption {
return func(metrics *Metrics) {
metrics.registerer = registerer
}
}
// NewMetrics returns a new configured Metrics.
func NewMetrics(opts ...MetricsOption) *Metrics {
m := &Metrics{
namespace: defaultNamespace,
subsystem: defaultSubsystem,
registerer: prometheus.DefaultRegisterer,
}
for _, opt := range opts {
opt(m)
}
hits := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: m.namespace,
Subsystem: m.subsystem,
ConstLabels: m.constLabels,
Name: "pool_hits",
Help: "Number of times free connection was found in the pool",
},
[]string{"pool_name"},
)
m.registerer.MustRegister(hits)
m.hits = hits
misses := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: m.namespace,
Subsystem: m.subsystem,
ConstLabels: m.constLabels,
Name: "pool_misses",
Help: "Number of times free connection was NOT found in the pool",
},
[]string{"pool_name"},
)
m.registerer.MustRegister(misses)
m.misses = misses
timeouts := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: m.namespace,
Subsystem: m.subsystem,
ConstLabels: m.constLabels,
Name: "pool_timeouts",
Help: "Number of times a wait timeout occurred",
},
[]string{"pool_name"},
)
m.registerer.MustRegister(timeouts)
m.timeouts = timeouts
totalConns := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: m.namespace,
Subsystem: m.subsystem,
ConstLabels: m.constLabels,
Name: "pool_total_connections",
Help: "Number of total connections in the pool",
},
[]string{"pool_name"},
)
m.registerer.MustRegister(totalConns)
m.totalConns = totalConns
idleConns := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: m.namespace,
Subsystem: m.subsystem,
ConstLabels: m.constLabels,
Name: "pool_idle_connections",
Help: "Number of idle connections in the pool",
},
[]string{"pool_name"},
)
m.registerer.MustRegister(idleConns)
m.idleConns = idleConns
staleConns := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: m.namespace,
Subsystem: m.subsystem,
ConstLabels: m.constLabels,
Name: "pool_stale_connections",
Help: "Number of stale connections removed from the pool",
},
[]string{"pool_name"},
)
m.registerer.MustRegister(staleConns)
m.staleConns = staleConns
return m
}