pangolin_exporter
84 строки · 1.9 Кб
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/prometheus/client_golang/prometheus"
20)
21
22const walSubsystem = "wal"
23
24func init() {
25registerCollector(walSubsystem, defaultEnabled, NewPGWALCollector)
26}
27
28type PGWALCollector struct {
29}
30
31func NewPGWALCollector(config collectorConfig) (Collector, error) {
32return &PGWALCollector{}, nil
33}
34
35var (
36pgWALSegments = prometheus.NewDesc(
37prometheus.BuildFQName(
38namespace,
39walSubsystem,
40"segments",
41),
42"Number of WAL segments",
43[]string{}, nil,
44)
45pgWALSize = prometheus.NewDesc(
46prometheus.BuildFQName(
47namespace,
48walSubsystem,
49"size_bytes",
50),
51"Total size of WAL segments",
52[]string{}, nil,
53)
54
55pgWALQuery = `
56SELECT
57COUNT(*) AS segments,
58SUM(size) AS size
59FROM pg_ls_waldir()
60WHERE name ~ '^[0-9A-F]{24}$'`
61)
62
63func (c PGWALCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
64db := instance.getDB()
65row := db.QueryRowContext(ctx,
66pgWALQuery,
67)
68
69var segments uint64
70var size uint64
71err := row.Scan(&segments, &size)
72if err != nil {
73return err
74}
75ch <- prometheus.MustNewConstMetric(
76pgWALSegments,
77prometheus.GaugeValue, float64(segments),
78)
79ch <- prometheus.MustNewConstMetric(
80pgWALSize,
81prometheus.GaugeValue, float64(size),
82)
83return nil
84}
85