pangolin_exporter
91 строка · 2.2 Кб
1// Copyright 2024 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/go-kit/log"
21"github.com/prometheus/client_golang/prometheus"
22)
23
24const rolesSubsystem = "roles"
25
26func init() {
27registerCollector(rolesSubsystem, defaultEnabled, NewPGRolesCollector)
28}
29
30type PGRolesCollector struct {
31log log.Logger
32}
33
34func NewPGRolesCollector(config collectorConfig) (Collector, error) {
35return &PGRolesCollector{
36log: config.logger,
37}, nil
38}
39
40var (
41pgRolesConnectionLimitsDesc = prometheus.NewDesc(
42prometheus.BuildFQName(
43namespace,
44rolesSubsystem,
45"connection_limit",
46),
47"Connection limit set for the role",
48[]string{"rolname"}, nil,
49)
50
51pgRolesConnectionLimitsQuery = "SELECT pg_roles.rolname, pg_roles.rolconnlimit FROM pg_roles"
52)
53
54// Update implements Collector and exposes roles connection limits.
55// It is called by the Prometheus registry when collecting metrics.
56func (c PGRolesCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
57db := instance.getDB()
58// Query the list of databases
59rows, err := db.QueryContext(ctx,
60pgRolesConnectionLimitsQuery,
61)
62if err != nil {
63return err
64}
65defer rows.Close()
66
67for rows.Next() {
68var rolname sql.NullString
69var connLimit sql.NullInt64
70if err := rows.Scan(&rolname, &connLimit); err != nil {
71return err
72}
73
74if !rolname.Valid {
75continue
76}
77rolnameLabel := rolname.String
78
79if !connLimit.Valid {
80continue
81}
82connLimitMetric := float64(connLimit.Int64)
83
84ch <- prometheus.MustNewConstMetric(
85pgRolesConnectionLimitsDesc,
86prometheus.GaugeValue, connLimitMetric, rolnameLabel,
87)
88}
89
90return rows.Err()
91}
92