-
Notifications
You must be signed in to change notification settings - Fork 770
/
Copy pathpg_stat_wal.go
240 lines (210 loc) · 6.52 KB
/
pg_stat_wal.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package collector
import (
"context"
"database/sql"
"fmt"
"log/slog"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
const statWALSubsystem = "stat_wal"
func init() {
registerCollector(statWALSubsystem, defaultDisabled, NewPGStatWALCollector)
}
type PGStatWALCollector struct {
log *slog.Logger
}
func NewPGStatWALCollector(config collectorConfig) (Collector, error) {
return &PGStatWALCollector{log: config.logger}, nil
}
var statsWALRecordsDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statWALSubsystem, "wal_records"),
"Total number of WAL records generated",
[]string{},
prometheus.Labels{},
)
var statsWALFPIDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statWALSubsystem, "wal_fpi"),
"Total number of WAL full page images generated",
[]string{},
prometheus.Labels{},
)
var statsWALBytesDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statWALSubsystem, "wal_bytes"),
"Total amount of WAL generated in bytes",
[]string{},
prometheus.Labels{},
)
var statsWALBuffersFullDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statWALSubsystem, "wal_buffers_full"),
"Number of times WAL data was written to disk because WAL buffers became full",
[]string{},
prometheus.Labels{},
)
var statsWALWriteDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statWALSubsystem, "wal_write"),
"Number of times WAL buffers were written out to disk via XLogWrite request. See Section 30.5 for more information about the internal WAL function XLogWrite.",
[]string{},
prometheus.Labels{},
)
var statsWALSyncDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statWALSubsystem, "wal_sync"),
"Number of times WAL files were synced to disk via issue_xlog_fsync request (if fsync is on and wal_sync_method is either fdatasync, fsync or fsync_writethrough, otherwise zero). See Section 30.5 for more information about the internal WAL function issue_xlog_fsync.",
[]string{},
prometheus.Labels{},
)
var statsWALWriteTimeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statWALSubsystem, "wal_write_time"),
"Total amount of time spent writing WAL buffers to disk via XLogWrite request, in milliseconds (if track_wal_io_timing is enabled, otherwise zero). This includes the sync time when wal_sync_method is either open_datasync or open_sync.",
[]string{},
prometheus.Labels{},
)
var statsWALSyncTimeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statWALSubsystem, "wal_sync_time"),
"Total amount of time spent syncing WAL files to disk via issue_xlog_fsync request, in milliseconds (if track_wal_io_timing is enabled, fsync is on, and wal_sync_method is either fdatasync, fsync or fsync_writethrough, otherwise zero).",
[]string{},
prometheus.Labels{},
)
var statsWALStatsResetDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statWALSubsystem, "stats_reset"),
"Time at which these statistics were last reset",
[]string{},
prometheus.Labels{},
)
func statWALQuery(columns []string) string {
return fmt.Sprintf("SELECT %s FROM pg_stat_wal;", strings.Join(columns, ","))
}
func (c *PGStatWALCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
db := instance.getDB()
columns := []string{
"wal_records", // bigint
"wal_fpi", // bigint
"wal_bytes", // numeric
"wal_buffers_full", // bigint
"wal_write", // bigint
"wal_sync", // bigint
"wal_write_time", // double precision
"wal_sync_time", // double precision
"stats_reset", // timestamp with time zone
}
rows, err := db.QueryContext(ctx,
statWALQuery(columns),
)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var walRecords, walFPI, walBytes, walBuffersFull, walWrite, walSync sql.NullInt64
var walWriteTime, walSyncTime sql.NullFloat64
var statsReset sql.NullTime
err := rows.Scan(
&walRecords,
&walFPI,
&walBytes,
&walBuffersFull,
&walWrite,
&walSync,
&walWriteTime,
&walSyncTime,
&statsReset,
)
if err != nil {
return err
}
walRecordsMetric := 0.0
if walRecords.Valid {
walRecordsMetric = float64(walRecords.Int64)
}
ch <- prometheus.MustNewConstMetric(
statsWALRecordsDesc,
prometheus.CounterValue,
walRecordsMetric,
)
walFPIMetric := 0.0
if walFPI.Valid {
walFPIMetric = float64(walFPI.Int64)
}
ch <- prometheus.MustNewConstMetric(
statsWALFPIDesc,
prometheus.CounterValue,
walFPIMetric,
)
walBytesMetric := 0.0
if walBytes.Valid {
walBytesMetric = float64(walBytes.Int64)
}
ch <- prometheus.MustNewConstMetric(
statsWALBytesDesc,
prometheus.CounterValue,
walBytesMetric,
)
walBuffersFullMetric := 0.0
if walBuffersFull.Valid {
walBuffersFullMetric = float64(walBuffersFull.Int64)
}
ch <- prometheus.MustNewConstMetric(
statsWALBuffersFullDesc,
prometheus.CounterValue,
walBuffersFullMetric,
)
walWriteMetric := 0.0
if walWrite.Valid {
walWriteMetric = float64(walWrite.Int64)
}
ch <- prometheus.MustNewConstMetric(
statsWALWriteDesc,
prometheus.CounterValue,
walWriteMetric,
)
walSyncMetric := 0.0
if walSync.Valid {
walSyncMetric = float64(walSync.Int64)
}
ch <- prometheus.MustNewConstMetric(
statsWALSyncDesc,
prometheus.CounterValue,
walSyncMetric,
)
walWriteTimeMetric := 0.0
if walWriteTime.Valid {
walWriteTimeMetric = float64(walWriteTime.Float64)
}
ch <- prometheus.MustNewConstMetric(
statsWALWriteTimeDesc,
prometheus.CounterValue,
walWriteTimeMetric,
)
walSyncTimeMetric := 0.0
if walSyncTime.Valid {
walSyncTimeMetric = float64(walSyncTime.Float64)
}
ch <- prometheus.MustNewConstMetric(
statsWALSyncTimeDesc,
prometheus.CounterValue,
walSyncTimeMetric,
)
resetMetric := 0.0
if statsReset.Valid {
resetMetric = float64(statsReset.Time.Unix())
}
ch <- prometheus.MustNewConstMetric(
statsWALStatsResetDesc,
prometheus.CounterValue,
resetMetric,
)
}
return nil
}