|
| 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 | +"time" |
| 19 | + |
| 20 | +"github.com/go-kit/log" |
| 21 | +"github.com/prometheus/client_golang/prometheus" |
| 22 | +) |
| 23 | + |
| 24 | +func init() { |
| 25 | +registerCollector("bgwriter", defaultEnabled, NewPGStatBGWriterCollector) |
| 26 | +} |
| 27 | + |
| 28 | +type PGStatBGWriterCollector struct { |
| 29 | +} |
| 30 | + |
| 31 | +func NewPGStatBGWriterCollector(logger log.Logger) (Collector, error) { |
| 32 | +return &PGStatBGWriterCollector{}, nil |
| 33 | +} |
| 34 | + |
| 35 | +const bgWriterSubsystem = "stat_bgwriter" |
| 36 | + |
| 37 | +var statBGWriter = map[string]*prometheus.Desc{ |
| 38 | +"checkpoints_timed": prometheus.NewDesc( |
| 39 | +prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_timed_total"), |
| 40 | +"Number of scheduled checkpoints that have been performed", |
| 41 | +[]string{"server"}, |
| 42 | +prometheus.Labels{}, |
| 43 | +), |
| 44 | +"checkpoints_req": prometheus.NewDesc( |
| 45 | +prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_req_total"), |
| 46 | +"Number of requested checkpoints that have been performed", |
| 47 | +[]string{"server"}, |
| 48 | +prometheus.Labels{}, |
| 49 | +), |
| 50 | +"checkpoint_write_time": prometheus.NewDesc( |
| 51 | +prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoint_write_time_total"), |
| 52 | +"Total amount of time that has been spent in the portion of checkpoint processing where files are written to disk, in milliseconds", |
| 53 | +[]string{"server"}, |
| 54 | +prometheus.Labels{}, |
| 55 | +), |
| 56 | +"checkpoint_sync_time": prometheus.NewDesc( |
| 57 | +prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoint_sync_time_total"), |
| 58 | +"Total amount of time that has been spent in the portion of checkpoint processing where files are synchronized to disk, in milliseconds", |
| 59 | +[]string{"server"}, |
| 60 | +prometheus.Labels{}, |
| 61 | +), |
| 62 | +"buffers_checkpoint": prometheus.NewDesc( |
| 63 | +prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_checkpoint_total"), |
| 64 | +"Number of buffers written during checkpoints", |
| 65 | +[]string{"server"}, |
| 66 | +prometheus.Labels{}, |
| 67 | +), |
| 68 | +"buffers_clean": prometheus.NewDesc( |
| 69 | +prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_clean_total"), |
| 70 | +"Number of buffers written by the background writer", |
| 71 | +[]string{"server"}, |
| 72 | +prometheus.Labels{}, |
| 73 | +), |
| 74 | +"maxwritten_clean": prometheus.NewDesc( |
| 75 | +prometheus.BuildFQName(namespace, bgWriterSubsystem, "maxwritten_clean_total"), |
| 76 | +"Number of times the background writer stopped a cleaning scan because it had written too many buffers", |
| 77 | +[]string{"server"}, |
| 78 | +prometheus.Labels{}, |
| 79 | +), |
| 80 | +"buffers_backend": prometheus.NewDesc( |
| 81 | +prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_backend_total"), |
| 82 | +"Number of buffers written directly by a backend", |
| 83 | +[]string{"server"}, |
| 84 | +prometheus.Labels{}, |
| 85 | +), |
| 86 | +"buffers_backend_fsync": prometheus.NewDesc( |
| 87 | +prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_backend_fsync_total"), |
| 88 | +"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)", |
| 89 | +[]string{"server"}, |
| 90 | +prometheus.Labels{}, |
| 91 | +), |
| 92 | +"buffers_alloc": prometheus.NewDesc( |
| 93 | +prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_alloc_total"), |
| 94 | +"Number of buffers allocated", |
| 95 | +[]string{"server"}, |
| 96 | +prometheus.Labels{}, |
| 97 | +), |
| 98 | +"stats_reset": prometheus.NewDesc( |
| 99 | +prometheus.BuildFQName(namespace, bgWriterSubsystem, "stats_reset_total"), |
| 100 | +"Time at which these statistics were last reset", |
| 101 | +[]string{"server"}, |
| 102 | +prometheus.Labels{}, |
| 103 | +), |
| 104 | +} |
| 105 | + |
| 106 | +func (PGStatBGWriterCollector) Update(ctx context.Context, server *server, ch chan<- prometheus.Metric) error { |
| 107 | +db, err := server.GetDB() |
| 108 | +if err != nil { |
| 109 | +return err |
| 110 | +} |
| 111 | + |
| 112 | +row := db.QueryRowContext(ctx, |
| 113 | +`SELECT |
| 114 | + checkpoints_timed |
| 115 | + ,checkpoints_req |
| 116 | + ,checkpoint_write_time |
| 117 | + ,checkpoint_sync_time |
| 118 | + ,buffers_checkpoint |
| 119 | + ,buffers_clean |
| 120 | + ,maxwritten_clean |
| 121 | + ,buffers_backend |
| 122 | + ,buffers_backend_fsync |
| 123 | + ,buffers_alloc |
| 124 | + ,stats_reset |
| 125 | + FROM pg_stat_bgwriter;`) |
| 126 | + |
| 127 | +var cpt int |
| 128 | +var cpr int |
| 129 | +var cpwt int |
| 130 | +var cpst int |
| 131 | +var bcp int |
| 132 | +var bc int |
| 133 | +var mwc int |
| 134 | +var bb int |
| 135 | +var bbf int |
| 136 | +var ba int |
| 137 | +var sr time.Time |
| 138 | + |
| 139 | +err = row.Scan(&cpt, &cpr, &cpwt, &cpst, &bcp, &bc, &mwc, &bb, &bbf, &ba, &sr) |
| 140 | +if err != nil { |
| 141 | +return err |
| 142 | +} |
| 143 | + |
| 144 | +ch <- prometheus.MustNewConstMetric( |
| 145 | +statBGWriter["checkpoints_timed"], |
| 146 | +prometheus.CounterValue, |
| 147 | +float64(cpt), |
| 148 | +server.GetName(), |
| 149 | +) |
| 150 | +ch <- prometheus.MustNewConstMetric( |
| 151 | +statBGWriter["checkpoints_req"], |
| 152 | +prometheus.CounterValue, |
| 153 | +float64(cpr), |
| 154 | +server.GetName(), |
| 155 | +) |
| 156 | +ch <- prometheus.MustNewConstMetric( |
| 157 | +statBGWriter["checkpoint_write_time"], |
| 158 | +prometheus.CounterValue, |
| 159 | +float64(cpwt), |
| 160 | +server.GetName(), |
| 161 | +) |
| 162 | +ch <- prometheus.MustNewConstMetric( |
| 163 | +statBGWriter["checkpoint_sync_time"], |
| 164 | +prometheus.CounterValue, |
| 165 | +float64(cpst), |
| 166 | +server.GetName(), |
| 167 | +) |
| 168 | +ch <- prometheus.MustNewConstMetric( |
| 169 | +statBGWriter["buffers_checkpoint"], |
| 170 | +prometheus.CounterValue, |
| 171 | +float64(bcp), |
| 172 | +server.GetName(), |
| 173 | +) |
| 174 | +ch <- prometheus.MustNewConstMetric( |
| 175 | +statBGWriter["buffers_clean"], |
| 176 | +prometheus.CounterValue, |
| 177 | +float64(bc), |
| 178 | +server.GetName(), |
| 179 | +) |
| 180 | +ch <- prometheus.MustNewConstMetric( |
| 181 | +statBGWriter["maxwritten_clean"], |
| 182 | +prometheus.CounterValue, |
| 183 | +float64(mwc), |
| 184 | +server.GetName(), |
| 185 | +) |
| 186 | +ch <- prometheus.MustNewConstMetric( |
| 187 | +statBGWriter["buffers_backend"], |
| 188 | +prometheus.CounterValue, |
| 189 | +float64(bb), |
| 190 | +server.GetName(), |
| 191 | +) |
| 192 | +ch <- prometheus.MustNewConstMetric( |
| 193 | +statBGWriter["buffers_backend_fsync"], |
| 194 | +prometheus.CounterValue, |
| 195 | +float64(bbf), |
| 196 | +server.GetName(), |
| 197 | +) |
| 198 | +ch <- prometheus.MustNewConstMetric( |
| 199 | +statBGWriter["buffers_alloc"], |
| 200 | +prometheus.CounterValue, |
| 201 | +float64(ba), |
| 202 | +server.GetName(), |
| 203 | +) |
| 204 | +ch <- prometheus.MustNewConstMetric( |
| 205 | +statBGWriter["stats_reset"], |
| 206 | +prometheus.CounterValue, |
| 207 | +float64(sr.Unix()), |
| 208 | +server.GetName(), |
| 209 | +) |
| 210 | + |
| 211 | +return nil |
| 212 | +} |
0 commit comments