File: dbstats.go

package info (click to toggle)
golang-gitlab-gitlab-org-labkit 1.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: sh: 210; javascript: 49; makefile: 4
file content (211 lines) | stat: -rw-r--r-- 6,126 bytes parent folder | download | duplicates (3)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package sqlmetrics

import (
	"database/sql"

	"github.com/prometheus/client_golang/prometheus"
)

const (
	namespace   = "go_sql_dbstats"
	subsystem   = "connections"
	dbNameLabel = "db_name"

	// Names for the recorded metrics.
	maxOpenConnectionsName = "max_open"
	openConnectionsName    = "open"
	inUseName              = "in_use"
	idleName               = "idle"
	waitCountName          = "waits_total"
	waitDurationName       = "wait_seconds_total"
	maxIdleClosedName      = "max_idle_closed_count_total"
	maxIdleTimeClosedName  = "max_idle_time_closed_count_total"
	maxLifetimeClosedName  = "max_lifetime_closed_count_total"

	// Descriptions for the recorded metrics.
	maxOpenConnectionsDesc = "The limit of open connections to the database."
	openConnectionsDesc    = "The number of established connections both in use and idle."
	inUseDesc              = "The number of connections currently in use."
	idleDesc               = "The number of idle connections."
	waitCountDesc          = "The total number of connections waited for."
	waitDurationDesc       = "The total time blocked waiting for a new connection."
	maxIdleClosedDesc      = "The total number of connections closed due to SetMaxIdleConns."
	maxIdleTimeClosedDesc  = "The total number of connections closed due to SetConnMaxIdleTime."
	maxLifetimeClosedDesc  = "The total number of connections closed due to SetConnMaxLifetime."
)

// DBStatsGetter is an interface for sql.DBStats. It's implemented by sql.DB.
type DBStatsGetter interface {
	Stats() sql.DBStats
}

// DBStatsCollector implements the prometheus.Collector interface.
type DBStatsCollector struct {
	sg DBStatsGetter

	maxOpenDesc           *prometheus.Desc
	openDesc              *prometheus.Desc
	inUseDesc             *prometheus.Desc
	idleDesc              *prometheus.Desc
	waitCountDesc         *prometheus.Desc
	waitDurationDesc      *prometheus.Desc
	maxIdleClosedDesc     *prometheus.Desc
	maxIdleTimeClosedDesc *prometheus.Desc
	maxLifetimeClosedDesc *prometheus.Desc
}

// Describe implements the prometheus.Collector interface.
func (c *DBStatsCollector) Describe(ch chan<- *prometheus.Desc) {
	ch <- c.maxOpenDesc
	ch <- c.openDesc
	ch <- c.inUseDesc
	ch <- c.idleDesc
	ch <- c.waitCountDesc
	ch <- c.waitDurationDesc
	ch <- c.maxIdleClosedDesc
	ch <- c.maxIdleTimeClosedDesc
	ch <- c.maxLifetimeClosedDesc
}

// Collect implements the prometheus.Collector interface.
func (c *DBStatsCollector) Collect(ch chan<- prometheus.Metric) {
	stats := c.sg.Stats()

	ch <- prometheus.MustNewConstMetric(
		c.maxOpenDesc,
		prometheus.GaugeValue,
		float64(stats.MaxOpenConnections),
	)
	ch <- prometheus.MustNewConstMetric(
		c.openDesc,
		prometheus.GaugeValue,
		float64(stats.OpenConnections),
	)
	ch <- prometheus.MustNewConstMetric(
		c.inUseDesc,
		prometheus.GaugeValue,
		float64(stats.InUse),
	)
	ch <- prometheus.MustNewConstMetric(
		c.idleDesc,
		prometheus.GaugeValue,
		float64(stats.Idle),
	)
	ch <- prometheus.MustNewConstMetric(
		c.waitCountDesc,
		prometheus.CounterValue,
		float64(stats.WaitCount),
	)
	ch <- prometheus.MustNewConstMetric(
		c.waitDurationDesc,
		prometheus.CounterValue,
		stats.WaitDuration.Seconds(),
	)
	ch <- prometheus.MustNewConstMetric(
		c.maxIdleClosedDesc,
		prometheus.CounterValue,
		float64(stats.MaxIdleClosed),
	)
	ch <- prometheus.MustNewConstMetric(
		c.maxIdleTimeClosedDesc,
		prometheus.CounterValue,
		float64(stats.MaxIdleTimeClosed),
	)
	ch <- prometheus.MustNewConstMetric(
		c.maxLifetimeClosedDesc,
		prometheus.CounterValue,
		float64(stats.MaxLifetimeClosed),
	)
}

type dbStatsCollectorConfig struct {
	extraLabels prometheus.Labels
}

// DBStatsCollectorOption is used to pass options in NewDBStatsCollector.
type DBStatsCollectorOption func(*dbStatsCollectorConfig)

// WithExtraLabels will configure extra label values to apply to the DBStats metrics.
// A label named db_name will be ignored, as this is set internally.
func WithExtraLabels(labelValues map[string]string) DBStatsCollectorOption {
	return func(config *dbStatsCollectorConfig) {
		config.extraLabels = labelValues
	}
}

func applyDBStatsCollectorOptions(opts []DBStatsCollectorOption) dbStatsCollectorConfig {
	config := dbStatsCollectorConfig{}
	for _, v := range opts {
		v(&config)
	}

	return config
}

// NewDBStatsCollector creates a new DBStatsCollector.
func NewDBStatsCollector(dbName string, sg DBStatsGetter, opts ...DBStatsCollectorOption) *DBStatsCollector {
	config := applyDBStatsCollectorOptions(opts)

	if config.extraLabels == nil {
		config.extraLabels = make(prometheus.Labels)
	}
	config.extraLabels[dbNameLabel] = dbName

	return &DBStatsCollector{
		sg: sg,
		maxOpenDesc: prometheus.NewDesc(
			prometheus.BuildFQName(namespace, subsystem, maxOpenConnectionsName),
			maxOpenConnectionsDesc,
			nil,
			config.extraLabels,
		),
		openDesc: prometheus.NewDesc(
			prometheus.BuildFQName(namespace, subsystem, openConnectionsName),
			openConnectionsDesc,
			nil,
			config.extraLabels,
		),
		inUseDesc: prometheus.NewDesc(
			prometheus.BuildFQName(namespace, subsystem, inUseName),
			inUseDesc,
			nil,
			config.extraLabels,
		),
		idleDesc: prometheus.NewDesc(
			prometheus.BuildFQName(namespace, subsystem, idleName),
			idleDesc,
			nil,
			config.extraLabels,
		),
		waitCountDesc: prometheus.NewDesc(
			prometheus.BuildFQName(namespace, subsystem, waitCountName),
			waitCountDesc,
			nil,
			config.extraLabels,
		),
		waitDurationDesc: prometheus.NewDesc(
			prometheus.BuildFQName(namespace, subsystem, waitDurationName),
			waitDurationDesc,
			nil,
			config.extraLabels,
		),
		maxIdleClosedDesc: prometheus.NewDesc(
			prometheus.BuildFQName(namespace, subsystem, maxIdleClosedName),
			maxIdleClosedDesc,
			nil,
			config.extraLabels,
		),
		maxIdleTimeClosedDesc: prometheus.NewDesc(
			prometheus.BuildFQName(namespace, subsystem, maxIdleTimeClosedName),
			maxIdleTimeClosedDesc,
			nil,
			config.extraLabels,
		),
		maxLifetimeClosedDesc: prometheus.NewDesc(
			prometheus.BuildFQName(namespace, subsystem, maxLifetimeClosedName),
			maxLifetimeClosedDesc,
			nil,
			config.extraLabels,
		),
	}
}