pangolin_exporter
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
14package collector
15
16import (
17"context"
18"database/sql"
19
20"github.com/prometheus/client_golang/prometheus"
21)
22
23const bgWriterSubsystem = "stat_bgwriter"
24
25func init() {
26registerCollector(bgWriterSubsystem, defaultEnabled, NewPGStatBGWriterCollector)
27}
28
29type PGStatBGWriterCollector struct {
30}
31
32func NewPGStatBGWriterCollector(collectorConfig) (Collector, error) {
33return &PGStatBGWriterCollector{}, nil
34}
35
36var (
37statBGWriterCheckpointsTimedDesc = prometheus.NewDesc(
38prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_timed_total"),
39"Number of scheduled checkpoints that have been performed",
40[]string{},
41prometheus.Labels{},
42)
43statBGWriterCheckpointsReqDesc = prometheus.NewDesc(
44prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_req_total"),
45"Number of requested checkpoints that have been performed",
46[]string{},
47prometheus.Labels{},
48)
49statBGWriterCheckpointsReqTimeDesc = prometheus.NewDesc(
50prometheus.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{},
53prometheus.Labels{},
54)
55statBGWriterCheckpointsSyncTimeDesc = prometheus.NewDesc(
56prometheus.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{},
59prometheus.Labels{},
60)
61statBGWriterBuffersCheckpointDesc = prometheus.NewDesc(
62prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_checkpoint_total"),
63"Number of buffers written during checkpoints",
64[]string{},
65prometheus.Labels{},
66)
67statBGWriterBuffersCleanDesc = prometheus.NewDesc(
68prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_clean_total"),
69"Number of buffers written by the background writer",
70[]string{},
71prometheus.Labels{},
72)
73statBGWriterMaxwrittenCleanDesc = prometheus.NewDesc(
74prometheus.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{},
77prometheus.Labels{},
78)
79statBGWriterBuffersBackendDesc = prometheus.NewDesc(
80prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_backend_total"),
81"Number of buffers written directly by a backend",
82[]string{},
83prometheus.Labels{},
84)
85statBGWriterBuffersBackendFsyncDesc = prometheus.NewDesc(
86prometheus.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{},
89prometheus.Labels{},
90)
91statBGWriterBuffersAllocDesc = prometheus.NewDesc(
92prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_alloc_total"),
93"Number of buffers allocated",
94[]string{},
95prometheus.Labels{},
96)
97statBGWriterStatsResetDesc = prometheus.NewDesc(
98prometheus.BuildFQName(namespace, bgWriterSubsystem, "stats_reset_total"),
99"Time at which these statistics were last reset",
100[]string{},
101prometheus.Labels{},
102)
103
104statBGWriterQuery = `SELECT
105checkpoints_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
116FROM pg_stat_bgwriter;`
117)
118
119func (PGStatBGWriterCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
120db := instance.getDB()
121row := db.QueryRowContext(ctx,
122statBGWriterQuery)
123
124var cpt, cpr, bcp, bc, mwc, bb, bbf, ba sql.NullInt64
125var cpwt, cpst sql.NullFloat64
126var sr sql.NullTime
127
128err := row.Scan(&cpt, &cpr, &cpwt, &cpst, &bcp, &bc, &mwc, &bb, &bbf, &ba, &sr)
129if err != nil {
130return err
131}
132
133cptMetric := 0.0
134if cpt.Valid {
135cptMetric = float64(cpt.Int64)
136}
137ch <- prometheus.MustNewConstMetric(
138statBGWriterCheckpointsTimedDesc,
139prometheus.CounterValue,
140cptMetric,
141)
142cprMetric := 0.0
143if cpr.Valid {
144cprMetric = float64(cpr.Int64)
145}
146ch <- prometheus.MustNewConstMetric(
147statBGWriterCheckpointsReqDesc,
148prometheus.CounterValue,
149cprMetric,
150)
151cpwtMetric := 0.0
152if cpwt.Valid {
153cpwtMetric = float64(cpwt.Float64)
154}
155ch <- prometheus.MustNewConstMetric(
156statBGWriterCheckpointsReqTimeDesc,
157prometheus.CounterValue,
158cpwtMetric,
159)
160cpstMetric := 0.0
161if cpst.Valid {
162cpstMetric = float64(cpst.Float64)
163}
164ch <- prometheus.MustNewConstMetric(
165statBGWriterCheckpointsSyncTimeDesc,
166prometheus.CounterValue,
167cpstMetric,
168)
169bcpMetric := 0.0
170if bcp.Valid {
171bcpMetric = float64(bcp.Int64)
172}
173ch <- prometheus.MustNewConstMetric(
174statBGWriterBuffersCheckpointDesc,
175prometheus.CounterValue,
176bcpMetric,
177)
178bcMetric := 0.0
179if bc.Valid {
180bcMetric = float64(bc.Int64)
181}
182ch <- prometheus.MustNewConstMetric(
183statBGWriterBuffersCleanDesc,
184prometheus.CounterValue,
185bcMetric,
186)
187mwcMetric := 0.0
188if mwc.Valid {
189mwcMetric = float64(mwc.Int64)
190}
191ch <- prometheus.MustNewConstMetric(
192statBGWriterMaxwrittenCleanDesc,
193prometheus.CounterValue,
194mwcMetric,
195)
196bbMetric := 0.0
197if bb.Valid {
198bbMetric = float64(bb.Int64)
199}
200ch <- prometheus.MustNewConstMetric(
201statBGWriterBuffersBackendDesc,
202prometheus.CounterValue,
203bbMetric,
204)
205bbfMetric := 0.0
206if bbf.Valid {
207bbfMetric = float64(bbf.Int64)
208}
209ch <- prometheus.MustNewConstMetric(
210statBGWriterBuffersBackendFsyncDesc,
211prometheus.CounterValue,
212bbfMetric,
213)
214baMetric := 0.0
215if ba.Valid {
216baMetric = float64(ba.Int64)
217}
218ch <- prometheus.MustNewConstMetric(
219statBGWriterBuffersAllocDesc,
220prometheus.CounterValue,
221baMetric,
222)
223srMetric := 0.0
224if sr.Valid {
225srMetric = float64(sr.Time.Unix())
226}
227ch <- prometheus.MustNewConstMetric(
228statBGWriterStatsResetDesc,
229prometheus.CounterValue,
230srMetric,
231)
232
233return nil
234}
235