-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcollector.go
174 lines (164 loc) · 4.57 KB
/
collector.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
170
171
172
173
174
package prometheus
import (
"fmt"
"github.com/alexfalkowski/go-service/os"
"github.com/alexfalkowski/go-service/version"
"github.com/jmoiron/sqlx"
"github.com/linxGnu/mssqlx"
"github.com/prometheus/client_golang/prometheus"
)
// Collector implements the prometheus.Collector interface.
type Collector struct {
db *mssqlx.DBs
maxOpen *prometheus.Desc
open *prometheus.Desc
inUse *prometheus.Desc
idle *prometheus.Desc
waitedFor *prometheus.Desc
blockedSeconds *prometheus.Desc
closedMaxIdle *prometheus.Desc
closedMaxLifetime *prometheus.Desc
closedMaxIdleTime *prometheus.Desc
}
// NewCollector for prometheus.
func NewCollector(name string, db *mssqlx.DBs, version version.Version) *Collector {
labels := prometheus.Labels{"name": os.ExecutableName(), "version": string(version)}
return &Collector{
db: db,
maxOpen: prometheus.NewDesc(
fmt.Sprintf("%s_sql_max_open_total", name),
fmt.Sprintf("Maximum number of open connections to %s.", name),
[]string{"db_name"},
labels,
),
open: prometheus.NewDesc(
fmt.Sprintf("%s_sql_open_total", name),
fmt.Sprintf("The number of established connections both in use and idle for %s.", name),
[]string{"db_name"},
labels,
),
inUse: prometheus.NewDesc(
fmt.Sprintf("%s_sql_in_use_total", name),
fmt.Sprintf("The number of connections currently in use for %s.", name),
[]string{"db_name"},
labels,
),
idle: prometheus.NewDesc(
fmt.Sprintf("%s_sql_idle_total", name),
fmt.Sprintf("The number of idle connections for %s.", name),
[]string{"db_name"},
labels,
),
waitedFor: prometheus.NewDesc(
fmt.Sprintf("%s_sql_waited_for_total", name),
fmt.Sprintf("The total number of connections waited for in %s.", name),
[]string{"db_name"},
labels,
),
blockedSeconds: prometheus.NewDesc(
fmt.Sprintf("%s_sql_blocked_seconds_total", name),
fmt.Sprintf("The total time blocked waiting for a new connection for %s.", name),
[]string{"db_name"},
labels,
),
closedMaxIdle: prometheus.NewDesc(
fmt.Sprintf("%s_sql_closed_max_idle_total", name),
fmt.Sprintf("The total number of connections closed due to SetMaxIdleConns for %s.", name),
[]string{"db_name"},
labels,
),
closedMaxLifetime: prometheus.NewDesc(
fmt.Sprintf("%s_sql_closed_max_lifetime_total", name),
fmt.Sprintf("The total number of connections closed due to SetConnMaxLifetime for %s.", name),
[]string{"db_name"},
labels,
),
closedMaxIdleTime: prometheus.NewDesc(
fmt.Sprintf("%s_sql_closed_max_idle_time_total", name),
fmt.Sprintf("The total number of connections closed due to SetConnMaxIdleTime for %s.", name),
[]string{"db_name"},
labels,
),
}
}
// Describe implements the prometheus.Collector interface.
func (c Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.maxOpen
ch <- c.open
ch <- c.inUse
ch <- c.idle
ch <- c.waitedFor
ch <- c.blockedSeconds
ch <- c.closedMaxIdle
ch <- c.closedMaxLifetime
ch <- c.closedMaxIdleTime
}
// Collect implements the prometheus.Collector interface.
func (c Collector) Collect(ch chan<- prometheus.Metric) {
ms, _ := c.db.GetAllMasters()
for i, m := range ms {
c.collect(fmt.Sprintf("master_%d", i), m, ch)
}
ss, _ := c.db.GetAllSlaves()
for i, s := range ss {
c.collect(fmt.Sprintf("slave_%d", i), s, ch)
}
}
// Collect implements the prometheus.Collector interface.
func (c Collector) collect(name string, db *sqlx.DB, ch chan<- prometheus.Metric) {
stats := db.Stats()
ch <- prometheus.MustNewConstMetric(
c.maxOpen,
prometheus.GaugeValue,
float64(stats.MaxOpenConnections),
name,
)
ch <- prometheus.MustNewConstMetric(
c.open,
prometheus.GaugeValue,
float64(stats.OpenConnections),
name,
)
ch <- prometheus.MustNewConstMetric(
c.inUse,
prometheus.GaugeValue,
float64(stats.InUse),
name,
)
ch <- prometheus.MustNewConstMetric(
c.idle,
prometheus.GaugeValue,
float64(stats.Idle),
name,
)
ch <- prometheus.MustNewConstMetric(
c.waitedFor,
prometheus.CounterValue,
float64(stats.WaitCount),
name,
)
ch <- prometheus.MustNewConstMetric(
c.blockedSeconds,
prometheus.CounterValue,
stats.WaitDuration.Seconds(),
name,
)
ch <- prometheus.MustNewConstMetric(
c.closedMaxIdle,
prometheus.CounterValue,
float64(stats.MaxIdleClosed),
name,
)
ch <- prometheus.MustNewConstMetric(
c.closedMaxLifetime,
prometheus.CounterValue,
float64(stats.MaxLifetimeClosed),
name,
)
ch <- prometheus.MustNewConstMetric(
c.closedMaxIdleTime,
prometheus.CounterValue,
float64(stats.MaxIdleTimeClosed),
name,
)
}