|
| 1 | +package collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "log/slog" |
| 7 | + |
| 8 | + "github.com/prometheus/client_golang/prometheus" |
| 9 | +) |
| 10 | + |
| 11 | +const proctabSubsystem = "proctab" |
| 12 | + |
| 13 | +func init() { |
| 14 | + registerCollector(proctabSubsystem, defaultDisabled, NewPGProctabCollector) |
| 15 | +} |
| 16 | + |
| 17 | +type PGProctabCollector struct { |
| 18 | + log *slog.Logger |
| 19 | +} |
| 20 | + |
| 21 | +func NewPGProctabCollector(config collectorConfig) (Collector, error) { |
| 22 | + return &PGProctabCollector{ |
| 23 | + log: config.logger, |
| 24 | + }, nil |
| 25 | +} |
| 26 | + |
| 27 | +var ( |
| 28 | + pgMemusedDesc = prometheus.NewDesc( |
| 29 | + prometheus.BuildFQName( |
| 30 | + namespace, |
| 31 | + proctabSubsystem, |
| 32 | + "memused", |
| 33 | + ), |
| 34 | + "used memory (from /proc/meminfo) in bytes", |
| 35 | + []string{}, nil, |
| 36 | + ) |
| 37 | + |
| 38 | + pgMemfreeDesc = prometheus.NewDesc( |
| 39 | + prometheus.BuildFQName( |
| 40 | + namespace, |
| 41 | + proctabSubsystem, |
| 42 | + "memfree", |
| 43 | + ), |
| 44 | + "free memory (from /proc/meminfo) in bytes", |
| 45 | + []string{}, nil, |
| 46 | + ) |
| 47 | + |
| 48 | + pgMemsharedDesc = prometheus.NewDesc( |
| 49 | + prometheus.BuildFQName( |
| 50 | + namespace, |
| 51 | + proctabSubsystem, |
| 52 | + "memshared", |
| 53 | + ), |
| 54 | + "shared memory (from /proc/meminfo) in bytes", |
| 55 | + []string{}, nil, |
| 56 | + ) |
| 57 | + |
| 58 | + pgMembuffersDesc = prometheus.NewDesc( |
| 59 | + prometheus.BuildFQName( |
| 60 | + namespace, |
| 61 | + proctabSubsystem, |
| 62 | + "membuffers", |
| 63 | + ), |
| 64 | + "buffered memory (from /proc/meminfo) in bytes", |
| 65 | + []string{}, nil, |
| 66 | + ) |
| 67 | + |
| 68 | + pgMemcachedDesc = prometheus.NewDesc( |
| 69 | + prometheus.BuildFQName( |
| 70 | + namespace, |
| 71 | + proctabSubsystem, |
| 72 | + "memcached", |
| 73 | + ), |
| 74 | + "cached memory (from /proc/meminfo) in bytes", |
| 75 | + []string{}, nil, |
| 76 | + ) |
| 77 | + pgSwapusedDesc = prometheus.NewDesc( |
| 78 | + prometheus.BuildFQName( |
| 79 | + namespace, |
| 80 | + proctabSubsystem, |
| 81 | + "swapused", |
| 82 | + ), |
| 83 | + "swap used (from /proc/meminfo) in bytes", |
| 84 | + []string{}, nil, |
| 85 | + ) |
| 86 | + |
| 87 | + // Loadavg metrics |
| 88 | + pgLoad1Desc = prometheus.NewDesc( |
| 89 | + prometheus.BuildFQName( |
| 90 | + namespace, |
| 91 | + proctabSubsystem, |
| 92 | + "load1", |
| 93 | + ), |
| 94 | + "load1 load Average", |
| 95 | + []string{}, nil, |
| 96 | + ) |
| 97 | + |
| 98 | + // CPU metrics |
| 99 | + pgCpuUserDesc = prometheus.NewDesc( |
| 100 | + prometheus.BuildFQName( |
| 101 | + namespace, |
| 102 | + proctabSubsystem, |
| 103 | + "cpu_user", |
| 104 | + ), |
| 105 | + "PG User cpu time", |
| 106 | + []string{}, nil, |
| 107 | + ) |
| 108 | + pgCpuNiceDesc = prometheus.NewDesc( |
| 109 | + prometheus.BuildFQName( |
| 110 | + namespace, |
| 111 | + proctabSubsystem, |
| 112 | + "cpu_nice", |
| 113 | + ), |
| 114 | + "PG nice cpu time (running queries)", |
| 115 | + []string{}, nil, |
| 116 | + ) |
| 117 | + pgCpuSystemDesc = prometheus.NewDesc( |
| 118 | + prometheus.BuildFQName( |
| 119 | + namespace, |
| 120 | + proctabSubsystem, |
| 121 | + "cpu_system", |
| 122 | + ), |
| 123 | + "PG system cpu time", |
| 124 | + []string{}, nil, |
| 125 | + ) |
| 126 | + pgCpuIdleDesc = prometheus.NewDesc( |
| 127 | + prometheus.BuildFQName( |
| 128 | + namespace, |
| 129 | + proctabSubsystem, |
| 130 | + "cpu_idle", |
| 131 | + ), |
| 132 | + "PG idle cpu time", |
| 133 | + []string{}, nil, |
| 134 | + ) |
| 135 | + pgCpuIowaitDesc = prometheus.NewDesc( |
| 136 | + prometheus.BuildFQName( |
| 137 | + namespace, |
| 138 | + proctabSubsystem, |
| 139 | + "cpu_iowait", |
| 140 | + ), |
| 141 | + "PG iowait time", |
| 142 | + []string{}, nil, |
| 143 | + ) |
| 144 | + |
| 145 | + memoryQuery = ` |
| 146 | + select |
| 147 | + memused, |
| 148 | + memfree, |
| 149 | + memshared, |
| 150 | + membuffers, |
| 151 | + memcached, |
| 152 | + swapused |
| 153 | + from |
| 154 | + pg_memusage() |
| 155 | + ` |
| 156 | + |
| 157 | + load1Query = ` |
| 158 | + select |
| 159 | + load1 |
| 160 | + from |
| 161 | + pg_loadavg() |
| 162 | + ` |
| 163 | + cpuQuery = ` |
| 164 | + select |
| 165 | + "user", |
| 166 | + nice, |
| 167 | + system, |
| 168 | + idle, |
| 169 | + iowait |
| 170 | + from |
| 171 | + pg_cputime() |
| 172 | + ` |
| 173 | +) |
| 174 | + |
| 175 | +func emitMemMetric(m sql.NullInt64, desc *prometheus.Desc, ch chan<- prometheus.Metric) { |
| 176 | + mM := 0.0 |
| 177 | + if m.Valid { |
| 178 | + mM = float64(m.Int64 * 1024) |
| 179 | + } |
| 180 | + ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, mM) |
| 181 | +} |
| 182 | +func emitCpuMetric(m sql.NullInt64, desc *prometheus.Desc, ch chan<- prometheus.Metric) { |
| 183 | + mM := 0.0 |
| 184 | + if m.Valid { |
| 185 | + mM = float64(m.Int64) |
| 186 | + } |
| 187 | + ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, mM) |
| 188 | +} |
| 189 | + |
| 190 | +// Update implements Collector and exposes database locks. |
| 191 | +// It is called by the Prometheus registry when collecting metrics. |
| 192 | +func (c PGProctabCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { |
| 193 | + db := instance.getDB() |
| 194 | + // Query the list of databases |
| 195 | + rows, err := db.QueryContext(ctx, memoryQuery) |
| 196 | + if err != nil { |
| 197 | + return err |
| 198 | + } |
| 199 | + defer rows.Close() |
| 200 | + |
| 201 | + var memused, memfree, memshared, membuffers, memcached, swapused sql.NullInt64 |
| 202 | + |
| 203 | + for rows.Next() { |
| 204 | + if err := rows.Scan(&memused, &memfree, &memshared, &membuffers, &memcached, &swapused); err != nil { |
| 205 | + return err |
| 206 | + } |
| 207 | + emitMemMetric(memused, pgMemusedDesc, ch) |
| 208 | + emitMemMetric(memfree, pgMemfreeDesc, ch) |
| 209 | + emitMemMetric(memshared, pgMemsharedDesc, ch) |
| 210 | + emitMemMetric(membuffers, pgMembuffersDesc, ch) |
| 211 | + emitMemMetric(memcached, pgMemcachedDesc, ch) |
| 212 | + emitMemMetric(swapused, pgSwapusedDesc, ch) |
| 213 | + } |
| 214 | + |
| 215 | + if err := rows.Err(); err != nil { |
| 216 | + return err |
| 217 | + } |
| 218 | + |
| 219 | + rows, err = db.QueryContext(ctx, load1Query) |
| 220 | + if err != nil { |
| 221 | + return err |
| 222 | + } |
| 223 | + defer rows.Close() |
| 224 | + |
| 225 | + var load1 sql.NullFloat64 |
| 226 | + for rows.Next() { |
| 227 | + if err := rows.Scan(&load1); err != nil { |
| 228 | + return err |
| 229 | + } |
| 230 | + load1Metric := 0.0 |
| 231 | + if load1.Valid { |
| 232 | + load1Metric = load1.Float64 |
| 233 | + } |
| 234 | + ch <- prometheus.MustNewConstMetric( |
| 235 | + pgLoad1Desc, |
| 236 | + prometheus.GaugeValue, load1Metric, |
| 237 | + ) |
| 238 | + } |
| 239 | + if err := rows.Err(); err != nil { |
| 240 | + return err |
| 241 | + } |
| 242 | + |
| 243 | + rows, err = db.QueryContext(ctx, cpuQuery) |
| 244 | + if err != nil { |
| 245 | + return err |
| 246 | + } |
| 247 | + defer rows.Close() |
| 248 | + var user, nice, system, idle, iowait sql.NullInt64 |
| 249 | + for rows.Next() { |
| 250 | + if err := rows.Scan(&user, &nice, &system, &idle, &iowait); err != nil { |
| 251 | + return err |
| 252 | + } |
| 253 | + emitCpuMetric(user, pgCpuUserDesc, ch) |
| 254 | + emitCpuMetric(nice, pgCpuNiceDesc, ch) |
| 255 | + emitCpuMetric(system, pgCpuSystemDesc, ch) |
| 256 | + emitCpuMetric(idle, pgCpuIdleDesc, ch) |
| 257 | + emitCpuMetric(iowait, pgCpuIowaitDesc, ch) |
| 258 | + } |
| 259 | + if err := rows.Err(); err != nil { |
| 260 | + return err |
| 261 | + } |
| 262 | + |
| 263 | + return nil |
| 264 | + |
| 265 | +} |
0 commit comments