pangolin_exporter
69 строк · 1.8 Кб
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"database/sql"
19
20"github.com/prometheus/client_golang/prometheus"
21)
22
23const postmasterSubsystem = "postmaster"
24
25func init() {
26registerCollector(postmasterSubsystem, defaultEnabled, NewPGPostmasterCollector)
27}
28
29type PGPostmasterCollector struct {
30}
31
32func NewPGPostmasterCollector(collectorConfig) (Collector, error) {
33return &PGPostmasterCollector{}, nil
34}
35
36var (
37pgPostMasterStartTimeSeconds = prometheus.NewDesc(
38prometheus.BuildFQName(
39namespace,
40postmasterSubsystem,
41"start_time_seconds",
42),
43"Time at which postmaster started",
44[]string{}, nil,
45)
46
47pgPostmasterQuery = "SELECT extract(epoch from pg_postmaster_start_time) from pg_postmaster_start_time();"
48)
49
50func (c *PGPostmasterCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
51db := instance.getDB()
52row := db.QueryRowContext(ctx,
53pgPostmasterQuery)
54
55var startTimeSeconds sql.NullFloat64
56err := row.Scan(&startTimeSeconds)
57if err != nil {
58return err
59}
60startTimeSecondsMetric := 0.0
61if startTimeSeconds.Valid {
62startTimeSecondsMetric = startTimeSeconds.Float64
63}
64ch <- prometheus.MustNewConstMetric(
65pgPostMasterStartTimeSeconds,
66prometheus.GaugeValue, startTimeSecondsMetric,
67)
68return nil
69}
70