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