|
| 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 | + |
| 14 | +package collector |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + |
| 19 | + "github.com/go-kit/log" |
| 20 | + "github.com/prometheus/client_golang/prometheus" |
| 21 | +) |
| 22 | + |
| 23 | +const locksSubsystem = "locks" |
| 24 | + |
| 25 | +func init() { |
| 26 | + registerCollector(locksSubsystem, defaultEnabled, NewPGLocksCollector) |
| 27 | +} |
| 28 | + |
| 29 | +type PGLocksCollector struct { |
| 30 | + log log.Logger |
| 31 | +} |
| 32 | + |
| 33 | +func NewPGLocksCollector(config collectorConfig) (Collector, error) { |
| 34 | + return &PGLocksCollector{ |
| 35 | + log: config.logger, |
| 36 | + }, nil |
| 37 | +} |
| 38 | + |
| 39 | +var ( |
| 40 | + pgLocksDesc = prometheus.NewDesc( |
| 41 | + prometheus.BuildFQName( |
| 42 | + namespace, |
| 43 | + locksSubsystem, |
| 44 | + "count", |
| 45 | + ), |
| 46 | + "Number of locks", |
| 47 | + []string{"datname", "mode"}, nil, |
| 48 | + ) |
| 49 | + |
| 50 | + pgLocksQuery = ` |
| 51 | + SELECT |
| 52 | + pg_database.datname as datname, |
| 53 | + tmp.mode as mode, |
| 54 | + COALESCE(count, 0) as count |
| 55 | + FROM |
| 56 | + ( |
| 57 | + VALUES |
| 58 | + ('accesssharelock'), |
| 59 | + ('rowsharelock'), |
| 60 | + ('rowexclusivelock'), |
| 61 | + ('shareupdateexclusivelock'), |
| 62 | + ('sharelock'), |
| 63 | + ('sharerowexclusivelock'), |
| 64 | + ('exclusivelock'), |
| 65 | + ('accessexclusivelock'), |
| 66 | + ('sireadlock') |
| 67 | + ) AS tmp(mode) CROSS |
| 68 | + JOIN pg_database |
| 69 | + LEFT JOIN ( |
| 70 | + SELECT |
| 71 | + database, |
| 72 | + lower(mode) AS mode, |
| 73 | + count(*) AS count |
| 74 | + FROM |
| 75 | + pg_locks |
| 76 | + WHERE |
| 77 | + database IS NOT NULL |
| 78 | + GROUP BY |
| 79 | + database, |
| 80 | + lower(mode) |
| 81 | + ) AS tmp2 ON tmp.mode = tmp2.mode |
| 82 | + and pg_database.oid = tmp2.database |
| 83 | + ORDER BY |
| 84 | + 1 |
| 85 | + ` |
| 86 | +) |
| 87 | + |
| 88 | +// Update implements Collector and exposes database locks. |
| 89 | +// It is called by the Prometheus registry when collecting metrics. |
| 90 | +func (c PGLocksCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { |
| 91 | + db := instance.getDB() |
| 92 | + // Query the list of databases |
| 93 | + rows, err := db.QueryContext(ctx, |
| 94 | + pgLocksQuery, |
| 95 | + ) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + defer rows.Close() |
| 100 | + |
| 101 | + var datname, mode string |
| 102 | + var count int64 |
| 103 | + |
| 104 | + for rows.Next() { |
| 105 | + if err := rows.Scan(&datname, &mode, &count); err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + ch <- prometheus.MustNewConstMetric( |
| 110 | + pgLocksDesc, |
| 111 | + prometheus.GaugeValue, float64(count), |
| 112 | + datname, mode, |
| 113 | + ) |
| 114 | + } |
| 115 | + if err := rows.Err(); err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + return nil |
| 119 | +} |
0 commit comments