pangolin_exporter

Форк
0
/
pg_stat_bgwriter.go 
234 строки · 6.5 Кб
1
// Copyright 2021 The Prometheus Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13

14
package collector
15

16
import (
17
	"context"
18
	"database/sql"
19

20
	"github.com/prometheus/client_golang/prometheus"
21
)
22

23
const bgWriterSubsystem = "stat_bgwriter"
24

25
func init() {
26
	registerCollector(bgWriterSubsystem, defaultEnabled, NewPGStatBGWriterCollector)
27
}
28

29
type PGStatBGWriterCollector struct {
30
}
31

32
func NewPGStatBGWriterCollector(collectorConfig) (Collector, error) {
33
	return &PGStatBGWriterCollector{}, nil
34
}
35

36
var (
37
	statBGWriterCheckpointsTimedDesc = prometheus.NewDesc(
38
		prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_timed_total"),
39
		"Number of scheduled checkpoints that have been performed",
40
		[]string{},
41
		prometheus.Labels{},
42
	)
43
	statBGWriterCheckpointsReqDesc = prometheus.NewDesc(
44
		prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_req_total"),
45
		"Number of requested checkpoints that have been performed",
46
		[]string{},
47
		prometheus.Labels{},
48
	)
49
	statBGWriterCheckpointsReqTimeDesc = prometheus.NewDesc(
50
		prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoint_write_time_total"),
51
		"Total amount of time that has been spent in the portion of checkpoint processing where files are written to disk, in milliseconds",
52
		[]string{},
53
		prometheus.Labels{},
54
	)
55
	statBGWriterCheckpointsSyncTimeDesc = prometheus.NewDesc(
56
		prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoint_sync_time_total"),
57
		"Total amount of time that has been spent in the portion of checkpoint processing where files are synchronized to disk, in milliseconds",
58
		[]string{},
59
		prometheus.Labels{},
60
	)
61
	statBGWriterBuffersCheckpointDesc = prometheus.NewDesc(
62
		prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_checkpoint_total"),
63
		"Number of buffers written during checkpoints",
64
		[]string{},
65
		prometheus.Labels{},
66
	)
67
	statBGWriterBuffersCleanDesc = prometheus.NewDesc(
68
		prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_clean_total"),
69
		"Number of buffers written by the background writer",
70
		[]string{},
71
		prometheus.Labels{},
72
	)
73
	statBGWriterMaxwrittenCleanDesc = prometheus.NewDesc(
74
		prometheus.BuildFQName(namespace, bgWriterSubsystem, "maxwritten_clean_total"),
75
		"Number of times the background writer stopped a cleaning scan because it had written too many buffers",
76
		[]string{},
77
		prometheus.Labels{},
78
	)
79
	statBGWriterBuffersBackendDesc = prometheus.NewDesc(
80
		prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_backend_total"),
81
		"Number of buffers written directly by a backend",
82
		[]string{},
83
		prometheus.Labels{},
84
	)
85
	statBGWriterBuffersBackendFsyncDesc = prometheus.NewDesc(
86
		prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_backend_fsync_total"),
87
		"Number of times a backend had to execute its own fsync call (normally the background writer handles those even when the backend does its own write)",
88
		[]string{},
89
		prometheus.Labels{},
90
	)
91
	statBGWriterBuffersAllocDesc = prometheus.NewDesc(
92
		prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_alloc_total"),
93
		"Number of buffers allocated",
94
		[]string{},
95
		prometheus.Labels{},
96
	)
97
	statBGWriterStatsResetDesc = prometheus.NewDesc(
98
		prometheus.BuildFQName(namespace, bgWriterSubsystem, "stats_reset_total"),
99
		"Time at which these statistics were last reset",
100
		[]string{},
101
		prometheus.Labels{},
102
	)
103

104
	statBGWriterQuery = `SELECT
105
		checkpoints_timed
106
		,checkpoints_req
107
		,checkpoint_write_time
108
		,checkpoint_sync_time
109
		,buffers_checkpoint
110
		,buffers_clean
111
		,maxwritten_clean
112
		,buffers_backend
113
		,buffers_backend_fsync
114
		,buffers_alloc
115
		,stats_reset
116
	FROM pg_stat_bgwriter;`
117
)
118

119
func (PGStatBGWriterCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
120
	db := instance.getDB()
121
	row := db.QueryRowContext(ctx,
122
		statBGWriterQuery)
123

124
	var cpt, cpr, bcp, bc, mwc, bb, bbf, ba sql.NullInt64
125
	var cpwt, cpst sql.NullFloat64
126
	var sr sql.NullTime
127

128
	err := row.Scan(&cpt, &cpr, &cpwt, &cpst, &bcp, &bc, &mwc, &bb, &bbf, &ba, &sr)
129
	if err != nil {
130
		return err
131
	}
132

133
	cptMetric := 0.0
134
	if cpt.Valid {
135
		cptMetric = float64(cpt.Int64)
136
	}
137
	ch <- prometheus.MustNewConstMetric(
138
		statBGWriterCheckpointsTimedDesc,
139
		prometheus.CounterValue,
140
		cptMetric,
141
	)
142
	cprMetric := 0.0
143
	if cpr.Valid {
144
		cprMetric = float64(cpr.Int64)
145
	}
146
	ch <- prometheus.MustNewConstMetric(
147
		statBGWriterCheckpointsReqDesc,
148
		prometheus.CounterValue,
149
		cprMetric,
150
	)
151
	cpwtMetric := 0.0
152
	if cpwt.Valid {
153
		cpwtMetric = float64(cpwt.Float64)
154
	}
155
	ch <- prometheus.MustNewConstMetric(
156
		statBGWriterCheckpointsReqTimeDesc,
157
		prometheus.CounterValue,
158
		cpwtMetric,
159
	)
160
	cpstMetric := 0.0
161
	if cpst.Valid {
162
		cpstMetric = float64(cpst.Float64)
163
	}
164
	ch <- prometheus.MustNewConstMetric(
165
		statBGWriterCheckpointsSyncTimeDesc,
166
		prometheus.CounterValue,
167
		cpstMetric,
168
	)
169
	bcpMetric := 0.0
170
	if bcp.Valid {
171
		bcpMetric = float64(bcp.Int64)
172
	}
173
	ch <- prometheus.MustNewConstMetric(
174
		statBGWriterBuffersCheckpointDesc,
175
		prometheus.CounterValue,
176
		bcpMetric,
177
	)
178
	bcMetric := 0.0
179
	if bc.Valid {
180
		bcMetric = float64(bc.Int64)
181
	}
182
	ch <- prometheus.MustNewConstMetric(
183
		statBGWriterBuffersCleanDesc,
184
		prometheus.CounterValue,
185
		bcMetric,
186
	)
187
	mwcMetric := 0.0
188
	if mwc.Valid {
189
		mwcMetric = float64(mwc.Int64)
190
	}
191
	ch <- prometheus.MustNewConstMetric(
192
		statBGWriterMaxwrittenCleanDesc,
193
		prometheus.CounterValue,
194
		mwcMetric,
195
	)
196
	bbMetric := 0.0
197
	if bb.Valid {
198
		bbMetric = float64(bb.Int64)
199
	}
200
	ch <- prometheus.MustNewConstMetric(
201
		statBGWriterBuffersBackendDesc,
202
		prometheus.CounterValue,
203
		bbMetric,
204
	)
205
	bbfMetric := 0.0
206
	if bbf.Valid {
207
		bbfMetric = float64(bbf.Int64)
208
	}
209
	ch <- prometheus.MustNewConstMetric(
210
		statBGWriterBuffersBackendFsyncDesc,
211
		prometheus.CounterValue,
212
		bbfMetric,
213
	)
214
	baMetric := 0.0
215
	if ba.Valid {
216
		baMetric = float64(ba.Int64)
217
	}
218
	ch <- prometheus.MustNewConstMetric(
219
		statBGWriterBuffersAllocDesc,
220
		prometheus.CounterValue,
221
		baMetric,
222
	)
223
	srMetric := 0.0
224
	if sr.Valid {
225
		srMetric = float64(sr.Time.Unix())
226
	}
227
	ch <- prometheus.MustNewConstMetric(
228
		statBGWriterStatsResetDesc,
229
		prometheus.CounterValue,
230
		srMetric,
231
	)
232

233
	return nil
234
}
235

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.