Skip to content

Commit fbd8ab7

Browse files
committed
Add support for pg_stat_checkpointer
See: https://www.dbi-services.com/blog/postgresql-17-new-catalog-view-pg_stat_checkpointer/ Signed-off-by: Nicolas Rodriguez <[email protected]>
1 parent 1ffd4ac commit fbd8ab7

File tree

2 files changed

+362
-0
lines changed

2 files changed

+362
-0
lines changed

collector/pg_stat_checkpointer.go

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
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 statCheckpointerSubsystem = "stat_checkpointer"
24+
25+
func init() {
26+
registerCollector(statCheckpointerSubsystem, defaultEnabled, NewPGStatCheckpointerCollector)
27+
}
28+
29+
type PGStatCheckpointerCollector struct {
30+
}
31+
32+
func NewPGStatCheckpointerCollector(collectorConfig) (Collector, error) {
33+
return &PGStatCheckpointerCollector{}, nil
34+
}
35+
36+
var (
37+
statCheckpointerNumTimedDesc = prometheus.NewDesc(
38+
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "num_timed_total"),
39+
"Number of scheduled checkpoints due to timeout",
40+
[]string{},
41+
prometheus.Labels{},
42+
)
43+
statCheckpointerNumRequestedDesc = prometheus.NewDesc(
44+
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "num_requested_total"),
45+
"Number of requested checkpoints that have been performed",
46+
[]string{},
47+
prometheus.Labels{},
48+
)
49+
statCheckpointerRestartpointsTimedDesc = prometheus.NewDesc(
50+
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "restartpoints_timed_total"),
51+
"Number of scheduled restartpoints due to timeout or after a failed attempt to perform it",
52+
[]string{},
53+
prometheus.Labels{},
54+
)
55+
statCheckpointerRestartpointsReqDesc = prometheus.NewDesc(
56+
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "restartpoints_req_total"),
57+
"Number of requested restartpoints",
58+
[]string{},
59+
prometheus.Labels{},
60+
)
61+
statCheckpointerRestartpointsDoneDesc = prometheus.NewDesc(
62+
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "restartpoints_done_total"),
63+
"Number of restartpoints that have been performed",
64+
[]string{},
65+
prometheus.Labels{},
66+
)
67+
statCheckpointerWriteTimeDesc = prometheus.NewDesc(
68+
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "write_time_total"),
69+
"Total amount of time that has been spent in the portion of processing checkpoints and restartpoints where files are written to disk, in milliseconds",
70+
[]string{},
71+
prometheus.Labels{},
72+
)
73+
statCheckpointerSyncTimeDesc = prometheus.NewDesc(
74+
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "sync_time_total"),
75+
"Total amount of time that has been spent in the portion of processing checkpoints and restartpoints where files are synchronized to disk, in milliseconds",
76+
[]string{},
77+
prometheus.Labels{},
78+
)
79+
statCheckpointerBuffersWrittenDesc = prometheus.NewDesc(
80+
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "buffers_written_total"),
81+
"Number of buffers written during checkpoints and restartpoints",
82+
[]string{},
83+
prometheus.Labels{},
84+
)
85+
statCheckpointerStatsResetDesc = prometheus.NewDesc(
86+
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "stats_reset_total"),
87+
"Time at which these statistics were last reset",
88+
[]string{},
89+
prometheus.Labels{},
90+
)
91+
92+
statCheckpointerQuery = `SELECT
93+
num_timed
94+
,num_requested
95+
,restartpoints_timed
96+
,restartpoints_req
97+
,restartpoints_done
98+
,write_time
99+
,sync_time
100+
,buffers_written
101+
,stats_reset
102+
FROM pg_stat_checkpointer;`
103+
)
104+
105+
func (PGStatCheckpointerCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
106+
db := instance.getDB()
107+
row := db.QueryRowContext(ctx, statCheckpointerQuery)
108+
109+
// num_timed = nt = bigint
110+
// num_requested = nr = bigint
111+
// restartpoints_timed = rpt = bigint
112+
// restartpoints_req = rpr = bigint
113+
// restartpoints_done = rpd = bigint
114+
// write_time = wt = double precision
115+
// sync_time = st = double precision
116+
// buffers_written = bw = bigint
117+
// stats_reset = sr = timestamp
118+
119+
var nt, nr, rpt, rpr, rpd, bw sql.NullInt64
120+
var wt, st sql.NullFloat64
121+
var sr sql.NullTime
122+
123+
err := row.Scan(&nt, &nr, &rpt, &rpr, &rpd, &wt, &st, &bw, &sr)
124+
if err != nil {
125+
return err
126+
}
127+
128+
ntMetric := 0.0
129+
if nt.Valid {
130+
ntMetric = float64(nt.Int64)
131+
}
132+
ch <- prometheus.MustNewConstMetric(
133+
statCheckpointerNumTimedDesc,
134+
prometheus.CounterValue,
135+
ntMetric,
136+
)
137+
138+
nrMetric := 0.0
139+
if nr.Valid {
140+
nrMetric = float64(nr.Int64)
141+
}
142+
ch <- prometheus.MustNewConstMetric(
143+
statCheckpointerNumRequestedDesc,
144+
prometheus.CounterValue,
145+
nrMetric,
146+
)
147+
148+
rptMetric := 0.0
149+
if rpt.Valid {
150+
rptMetric = float64(rpt.Int64)
151+
}
152+
ch <- prometheus.MustNewConstMetric(
153+
statCheckpointerRestartpointsTimedDesc,
154+
prometheus.CounterValue,
155+
rptMetric,
156+
)
157+
158+
rprMetric := 0.0
159+
if rpr.Valid {
160+
rprMetric = float64(rpr.Int64)
161+
}
162+
ch <- prometheus.MustNewConstMetric(
163+
statCheckpointerRestartpointsReqDesc,
164+
prometheus.CounterValue,
165+
rprMetric,
166+
)
167+
168+
rpdMetric := 0.0
169+
if rpd.Valid {
170+
rpdMetric = float64(rpd.Int64)
171+
}
172+
ch <- prometheus.MustNewConstMetric(
173+
statCheckpointerRestartpointsDoneDesc,
174+
prometheus.CounterValue,
175+
rpdMetric,
176+
)
177+
178+
wtMetric := 0.0
179+
if wt.Valid {
180+
wtMetric = float64(wt.Float64)
181+
}
182+
ch <- prometheus.MustNewConstMetric(
183+
statCheckpointerWriteTimeDesc,
184+
prometheus.CounterValue,
185+
wtMetric,
186+
)
187+
188+
stMetric := 0.0
189+
if st.Valid {
190+
stMetric = float64(st.Float64)
191+
}
192+
ch <- prometheus.MustNewConstMetric(
193+
statCheckpointerSyncTimeDesc,
194+
prometheus.CounterValue,
195+
stMetric,
196+
)
197+
198+
bwMetric := 0.0
199+
if bw.Valid {
200+
bwMetric = float64(bw.Int64)
201+
}
202+
ch <- prometheus.MustNewConstMetric(
203+
statCheckpointerBuffersWrittenDesc,
204+
prometheus.CounterValue,
205+
bwMetric,
206+
)
207+
208+
srMetric := 0.0
209+
if sr.Valid {
210+
srMetric = float64(sr.Time.Unix())
211+
}
212+
ch <- prometheus.MustNewConstMetric(
213+
statCheckpointerStatsResetDesc,
214+
prometheus.CounterValue,
215+
srMetric,
216+
)
217+
218+
return nil
219+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Copyright 2023 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+
package collector
14+
15+
import (
16+
"context"
17+
"testing"
18+
"time"
19+
20+
"github.com/DATA-DOG/go-sqlmock"
21+
"github.com/prometheus/client_golang/prometheus"
22+
dto "github.com/prometheus/client_model/go"
23+
"github.com/smartystreets/goconvey/convey"
24+
)
25+
26+
func TestPGStatCheckpointerCollector(t *testing.T) {
27+
db, mock, err := sqlmock.New()
28+
if err != nil {
29+
t.Fatalf("Error opening a stub db connection: %s", err)
30+
}
31+
defer db.Close()
32+
33+
inst := &instance{db: db}
34+
35+
columns := []string{
36+
"num_timed",
37+
"num_requested",
38+
"restartpoints_timed",
39+
"restartpoints_req",
40+
"restartpoints_done",
41+
"write_time",
42+
"sync_time",
43+
"buffers_written",
44+
"stats_reset"}
45+
46+
srT, err := time.Parse("2006-01-02 15:04:05.00000-07", "2023-05-25 17:10:42.81132-07")
47+
if err != nil {
48+
t.Fatalf("Error parsing time: %s", err)
49+
}
50+
51+
rows := sqlmock.NewRows(columns).
52+
AddRow(354, 4945, 289097744, 1242257, 3275602074, 89320867, 450139, 2034563757, srT)
53+
mock.ExpectQuery(sanitizeQuery(statCheckpointerQuery)).WillReturnRows(rows)
54+
55+
ch := make(chan prometheus.Metric)
56+
go func() {
57+
defer close(ch)
58+
c := PGStatCheckpointerCollector{}
59+
60+
if err := c.Update(context.Background(), inst, ch); err != nil {
61+
t.Errorf("Error calling PGStatCheckpointerCollector.Update: %s", err)
62+
}
63+
}()
64+
65+
expected := []MetricResult{
66+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 354},
67+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 4945},
68+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 289097744},
69+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 1242257},
70+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 3275602074},
71+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 89320867},
72+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 450139},
73+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 2034563757},
74+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 1685059842},
75+
}
76+
77+
convey.Convey("Metrics comparison", t, func() {
78+
for _, expect := range expected {
79+
m := readMetric(<-ch)
80+
convey.So(expect, convey.ShouldResemble, m)
81+
}
82+
})
83+
if err := mock.ExpectationsWereMet(); err != nil {
84+
t.Errorf("there were unfulfilled exceptions: %s", err)
85+
}
86+
}
87+
88+
func TestPGStatCheckpointerCollectorNullValues(t *testing.T) {
89+
db, mock, err := sqlmock.New()
90+
if err != nil {
91+
t.Fatalf("Error opening a stub db connection: %s", err)
92+
}
93+
defer db.Close()
94+
95+
inst := &instance{db: db}
96+
97+
columns := []string{
98+
"num_timed",
99+
"num_requested",
100+
"restartpoints_timed",
101+
"restartpoints_req",
102+
"restartpoints_done",
103+
"write_time",
104+
"sync_time",
105+
"buffers_written",
106+
"stats_reset"}
107+
108+
rows := sqlmock.NewRows(columns).
109+
AddRow(nil, nil, nil, nil, nil, nil, nil, nil, nil)
110+
mock.ExpectQuery(sanitizeQuery(statCheckpointerQuery)).WillReturnRows(rows)
111+
112+
ch := make(chan prometheus.Metric)
113+
go func() {
114+
defer close(ch)
115+
c := PGStatCheckpointerCollector{}
116+
117+
if err := c.Update(context.Background(), inst, ch); err != nil {
118+
t.Errorf("Error calling PGStatCheckpointerCollector.Update: %s", err)
119+
}
120+
}()
121+
122+
expected := []MetricResult{
123+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
124+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
125+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
126+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
127+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
128+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
129+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
130+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
131+
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
132+
}
133+
134+
convey.Convey("Metrics comparison", t, func() {
135+
for _, expect := range expected {
136+
m := readMetric(<-ch)
137+
convey.So(expect, convey.ShouldResemble, m)
138+
}
139+
})
140+
if err := mock.ExpectationsWereMet(); err != nil {
141+
t.Errorf("there were unfulfilled exceptions: %s", err)
142+
}
143+
}

0 commit comments

Comments
 (0)