|
| 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 | + |
| 19 | +"github.com/go-kit/log" |
| 20 | +"github.com/prometheus/client_golang/prometheus" |
| 21 | +) |
| 22 | + |
| 23 | +func init() { |
| 24 | +registerCollector("replication", defaultEnabled, NewPGStatWalReceiverCollector) |
| 25 | +} |
| 26 | + |
| 27 | +type PGStatioUserIndexesCollector struct { |
| 28 | +log log.Logger |
| 29 | +} |
| 30 | + |
| 31 | +const statioUserIndexesSubsystem = "statio_user_indexes" |
| 32 | + |
| 33 | +func NewPGStatioUserIndexesCollector(collectorConfig) (Collector, error) { |
| 34 | +return &PGStatioUserIndexesCollector{}, nil |
| 35 | +} |
| 36 | + |
| 37 | +var ( |
| 38 | +statioUserIndexesIdxBlksRead = prometheus.NewDesc( |
| 39 | +prometheus.BuildFQName(namespace, statioUserIndexesSubsystem, "idx_blks_read"), |
| 40 | +"Number of disk blocks read from this index", |
| 41 | +[]string{"schemaname", "relname", "indexrelname"}, |
| 42 | +prometheus.Labels{}, |
| 43 | +) |
| 44 | +statioUserIndexesIdxBlksHit = prometheus.NewDesc( |
| 45 | +prometheus.BuildFQName(namespace, statioUserIndexesSubsystem, "idx_blks_hit"), |
| 46 | +"Number of buffer hits in this index", |
| 47 | +[]string{"schemaname", "relname", "indexrelname"}, |
| 48 | +prometheus.Labels{}, |
| 49 | +) |
| 50 | + |
| 51 | +statioUserIndexesQuery = ` |
| 52 | +SELECT |
| 53 | +schemaname, |
| 54 | +relname, |
| 55 | +indexrelname, |
| 56 | +idx_blks_read, |
| 57 | +idx_blks_hit |
| 58 | +FROM pg_statio_user_indexes |
| 59 | +` |
| 60 | +) |
| 61 | + |
| 62 | +func (c *PGStatioUserIndexesCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error { |
| 63 | +rows, err := db.QueryContext(ctx, |
| 64 | +statioUserIndexesQuery) |
| 65 | + |
| 66 | +if err != nil { |
| 67 | +return err |
| 68 | +} |
| 69 | +defer rows.Close() |
| 70 | +for rows.Next() { |
| 71 | +var schemaname, relname, indexrelname string |
| 72 | +var idxBlksRead, idxBlksHit float64 |
| 73 | + |
| 74 | +if err := rows.Scan(&schemaname, &relname, &indexrelname, &idxBlksRead, &idxBlksHit); err != nil { |
| 75 | +return err |
| 76 | +} |
| 77 | + |
| 78 | +ch <- prometheus.MustNewConstMetric( |
| 79 | +statioUserIndexesIdxBlksRead, |
| 80 | +prometheus.CounterValue, |
| 81 | +idxBlksRead, |
| 82 | +schemaname, relname, indexrelname, |
| 83 | +) |
| 84 | +ch <- prometheus.MustNewConstMetric( |
| 85 | +statioUserIndexesIdxBlksHit, |
| 86 | +prometheus.CounterValue, |
| 87 | +idxBlksHit, |
| 88 | +schemaname, relname, indexrelname, |
| 89 | +) |
| 90 | +} |
| 91 | +if err := rows.Err(); err != nil { |
| 92 | +return err |
| 93 | +} |
| 94 | +return nil |
| 95 | +} |
0 commit comments