|
| 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 statActivityAutovacuumSubsystem = "stat_activity_autovacuum" |
| 24 | + |
| 25 | +func init() { |
| 26 | +registerCollector(statActivityAutovacuumSubsystem, defaultDisabled, NewPGStatActivityAutovacuumCollector) |
| 27 | +} |
| 28 | + |
| 29 | +type PGStatActivityAutovacuumCollector struct { |
| 30 | +log log.Logger |
| 31 | +} |
| 32 | + |
| 33 | +func NewPGStatActivityAutovacuumCollector(config collectorConfig) (Collector, error) { |
| 34 | +return &PGStatActivityAutovacuumCollector{log: config.logger}, nil |
| 35 | +} |
| 36 | + |
| 37 | +var ( |
| 38 | +statActivityAutovacuumAgeInSeconds = prometheus.NewDesc( |
| 39 | +prometheus.BuildFQName(namespace, statActivityAutovacuumSubsystem, "timestamp_seconds"), |
| 40 | +"Start timestamp of the vacuum process in seconds", |
| 41 | +[]string{"relname"}, |
| 42 | +prometheus.Labels{}, |
| 43 | +) |
| 44 | + |
| 45 | +statActivityAutovacuumQuery = ` |
| 46 | + SELECT |
| 47 | +SPLIT_PART(query, '.', 2) AS relname, |
| 48 | +EXTRACT(xact_start) AS timestamp_seconds |
| 49 | + FROM |
| 50 | + pg_catalog.pg_stat_activity |
| 51 | + WHERE |
| 52 | +query LIKE 'autovacuum:%' |
| 53 | +` |
| 54 | +) |
| 55 | + |
| 56 | +func (PGStatActivityAutovacuumCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error { |
| 57 | +db := instance.getDB() |
| 58 | +rows, err := db.QueryContext(ctx, |
| 59 | +statActivityAutovacuumQuery) |
| 60 | + |
| 61 | +if err != nil { |
| 62 | +return err |
| 63 | +} |
| 64 | +defer rows.Close() |
| 65 | + |
| 66 | +for rows.Next() { |
| 67 | +var relname string |
| 68 | +var ageInSeconds float64 |
| 69 | + |
| 70 | +if err := rows.Scan(&relname, &ageInSeconds); err != nil { |
| 71 | +return err |
| 72 | +} |
| 73 | + |
| 74 | +ch <- prometheus.MustNewConstMetric( |
| 75 | +statActivityAutovacuumAgeInSeconds, |
| 76 | +prometheus.GaugeValue, |
| 77 | +ageInSeconds, relname, |
| 78 | +) |
| 79 | +} |
| 80 | +if err := rows.Err(); err != nil { |
| 81 | +return err |
| 82 | +} |
| 83 | +return nil |
| 84 | +} |
0 commit comments