pangolin_exporter

Форк
0
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

14
package collector
15

16
import (
17
	"context"
18
	"database/sql"
19

20
	"github.com/go-kit/log"
21
	"github.com/prometheus/client_golang/prometheus"
22
)
23

24
const rolesSubsystem = "roles"
25

26
func init() {
27
	registerCollector(rolesSubsystem, defaultEnabled, NewPGRolesCollector)
28
}
29

30
type PGRolesCollector struct {
31
	log log.Logger
32
}
33

34
func NewPGRolesCollector(config collectorConfig) (Collector, error) {
35
	return &PGRolesCollector{
36
		log: config.logger,
37
	}, nil
38
}
39

40
var (
41
	pgRolesConnectionLimitsDesc = prometheus.NewDesc(
42
		prometheus.BuildFQName(
43
			namespace,
44
			rolesSubsystem,
45
			"connection_limit",
46
		),
47
		"Connection limit set for the role",
48
		[]string{"rolname"}, nil,
49
	)
50

51
	pgRolesConnectionLimitsQuery = "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.
56
func (c PGRolesCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
57
	db := instance.getDB()
58
	// Query the list of databases
59
	rows, err := db.QueryContext(ctx,
60
		pgRolesConnectionLimitsQuery,
61
	)
62
	if err != nil {
63
		return err
64
	}
65
	defer rows.Close()
66

67
	for rows.Next() {
68
		var rolname sql.NullString
69
		var connLimit sql.NullInt64
70
		if err := rows.Scan(&rolname, &connLimit); err != nil {
71
			return err
72
		}
73

74
		if !rolname.Valid {
75
			continue
76
		}
77
		rolnameLabel := rolname.String
78

79
		if !connLimit.Valid {
80
			continue
81
		}
82
		connLimitMetric := float64(connLimit.Int64)
83

84
		ch <- prometheus.MustNewConstMetric(
85
			pgRolesConnectionLimitsDesc,
86
			prometheus.GaugeValue, connLimitMetric, rolnameLabel,
87
		)
88
	}
89

90
	return rows.Err()
91
}
92

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.