|
| 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 | + "database/sql" |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/blang/semver/v4" |
| 22 | + "github.com/go-kit/log" |
| 23 | + "github.com/go-kit/log/level" |
| 24 | + "github.com/prometheus/client_golang/prometheus" |
| 25 | +) |
| 26 | + |
| 27 | +func init() { |
| 28 | + registerCollector(statUserIndexesSubsystem, defaultDisabled, NewPGStatUserIndexesCollector) |
| 29 | +} |
| 30 | + |
| 31 | +type PGStatUserIndexesCollector struct { |
| 32 | + log log.Logger |
| 33 | +} |
| 34 | + |
| 35 | +const statUserIndexesSubsystem = "stat_user_indexes" |
| 36 | + |
| 37 | +func NewPGStatUserIndexesCollector(config collectorConfig) (Collector, error) { |
| 38 | + return &PGStatUserIndexesCollector{log: config.logger}, nil |
| 39 | +} |
| 40 | + |
| 41 | +var ( |
| 42 | + statUserIndexesIdxScan = prometheus.NewDesc( |
| 43 | + prometheus.BuildFQName(namespace, statUserIndexesSubsystem, "idx_scan_total"), |
| 44 | + "Number of scans for this index", |
| 45 | + []string{"datname", "schemaname", "relname", "indexrelname"}, |
| 46 | + prometheus.Labels{}, |
| 47 | + ) |
| 48 | + |
| 49 | + statUserIndexesLastIdxScan = prometheus.NewDesc( |
| 50 | + prometheus.BuildFQName(namespace, statUserIndexesSubsystem, "last_idx_scan_time"), |
| 51 | + "Last timestamp of scan for this index", |
| 52 | + []string{"datname", "schemaname", "relname", "indexrelname"}, |
| 53 | + prometheus.Labels{}, |
| 54 | + ) |
| 55 | + |
| 56 | + statUserIndexesIdxTupRead = prometheus.NewDesc( |
| 57 | + prometheus.BuildFQName(namespace, statUserIndexesSubsystem, "idx_tup_read"), |
| 58 | + "Number of tuples read for this index", |
| 59 | + []string{"datname", "schemaname", "relname", "indexrelname"}, |
| 60 | + prometheus.Labels{}, |
| 61 | + ) |
| 62 | + |
| 63 | + statUserIndexesIdxTupFetch = prometheus.NewDesc( |
| 64 | + prometheus.BuildFQName(namespace, statUserIndexesSubsystem, "idx_tup_fetch"), |
| 65 | + "Number of tuples fetch for this index", |
| 66 | + []string{"datname", "schemaname", "relname", "indexrelname"}, |
| 67 | + prometheus.Labels{}, |
| 68 | + ) |
| 69 | +) |
| 70 | + |
| 71 | +func statUserIndexesQuery(columns []string) string { |
| 72 | + return fmt.Sprintf("SELECT %s FROM pg_stat_user_indexes;", strings.Join(columns, ",")) |
| 73 | +} |
| 74 | + |
| 75 | +func (c *PGStatUserIndexesCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { |
| 76 | + db := instance.getDB() |
| 77 | + |
| 78 | + columns := []string{ |
| 79 | + "current_database() datname", |
| 80 | + "schemaname", |
| 81 | + "relname", |
| 82 | + "indexrelname", |
| 83 | + "idx_scan", |
| 84 | + "idx_tup_read", |
| 85 | + "idx_tup_fetch", |
| 86 | + } |
| 87 | + |
| 88 | + lastIdxScanAvail := instance.version.GTE(semver.MustParse("16.0.0")) |
| 89 | + if lastIdxScanAvail { |
| 90 | + columns = append(columns, "date_part('epoch', last_idx_scan) as last_idx_scan") |
| 91 | + } |
| 92 | + |
| 93 | + rows, err := db.QueryContext(ctx, |
| 94 | + statUserIndexesQuery(columns), |
| 95 | + ) |
| 96 | + |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + defer rows.Close() |
| 101 | + for rows.Next() { |
| 102 | + var datname, schemaname, relname, indexrelname sql.NullString |
| 103 | + var idxScan, lastIdxScan, idxTupRead, idxTupFetch sql.NullFloat64 |
| 104 | + |
| 105 | + r := []any{ |
| 106 | + &datname, |
| 107 | + &schemaname, |
| 108 | + &relname, |
| 109 | + &indexrelname, |
| 110 | + &idxScan, |
| 111 | + &idxTupRead, |
| 112 | + &idxTupFetch, |
| 113 | + } |
| 114 | + |
| 115 | + if lastIdxScanAvail { |
| 116 | + r = append(r, &lastIdxScan) |
| 117 | + } |
| 118 | + |
| 119 | + if err := rows.Scan(r...); err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + datnameLabel := "unknown" |
| 123 | + if datname.Valid { |
| 124 | + datnameLabel = datname.String |
| 125 | + } |
| 126 | + schemanameLabel := "unknown" |
| 127 | + if schemaname.Valid { |
| 128 | + schemanameLabel = schemaname.String |
| 129 | + } |
| 130 | + relnameLabel := "unknown" |
| 131 | + if relname.Valid { |
| 132 | + relnameLabel = relname.String |
| 133 | + } |
| 134 | + indexrelnameLabel := "unknown" |
| 135 | + if indexrelname.Valid { |
| 136 | + indexrelnameLabel = indexrelname.String |
| 137 | + } |
| 138 | + |
| 139 | + if lastIdxScanAvail && !lastIdxScan.Valid { |
| 140 | + level.Debug(c.log).Log("msg", "Skipping collecting metric because it has no active_time") |
| 141 | + continue |
| 142 | + } |
| 143 | + |
| 144 | + labels := []string{datnameLabel, schemanameLabel, relnameLabel, indexrelnameLabel} |
| 145 | + |
| 146 | + idxScanMetric := 0.0 |
| 147 | + if idxScan.Valid { |
| 148 | + idxScanMetric = idxScan.Float64 |
| 149 | + } |
| 150 | + ch <- prometheus.MustNewConstMetric( |
| 151 | + statUserIndexesIdxScan, |
| 152 | + prometheus.CounterValue, |
| 153 | + idxScanMetric, |
| 154 | + labels..., |
| 155 | + ) |
| 156 | + |
| 157 | + idxTupReadMetric := 0.0 |
| 158 | + if idxTupRead.Valid { |
| 159 | + idxTupReadMetric = idxTupRead.Float64 |
| 160 | + } |
| 161 | + ch <- prometheus.MustNewConstMetric( |
| 162 | + statUserIndexesIdxTupRead, |
| 163 | + prometheus.CounterValue, |
| 164 | + idxTupReadMetric, |
| 165 | + labels..., |
| 166 | + ) |
| 167 | + |
| 168 | + idxTupFetchMetric := 0.0 |
| 169 | + if idxTupFetch.Valid { |
| 170 | + idxTupFetchMetric = idxTupFetch.Float64 |
| 171 | + } |
| 172 | + ch <- prometheus.MustNewConstMetric( |
| 173 | + statUserIndexesIdxTupFetch, |
| 174 | + prometheus.CounterValue, |
| 175 | + idxTupFetchMetric, |
| 176 | + labels..., |
| 177 | + ) |
| 178 | + |
| 179 | + if lastIdxScanAvail { |
| 180 | + ch <- prometheus.MustNewConstMetric( |
| 181 | + statUserIndexesLastIdxScan, |
| 182 | + prometheus.CounterValue, |
| 183 | + lastIdxScan.Float64, |
| 184 | + labels..., |
| 185 | + ) |
| 186 | + } |
| 187 | + } |
| 188 | + if err := rows.Err(); err != nil { |
| 189 | + return err |
| 190 | + } |
| 191 | + return nil |
| 192 | +} |
0 commit comments