mirror of
https://github.com/prometheus-community/postgres_exporter
synced 2025-04-08 10:11:26 +00:00
* Add database connection limits metrics Signed-off-by: Jocelyn Thode <jocelyn@thode.email> * Add roles connection limits metrics Signed-off-by: Jocelyn Thode <jocelyn@thode.email> * Fix copyright year Co-authored-by: Joe Adams <github@joeadams.io> Signed-off-by: Jocelyn Thode <jocelynthode@users.noreply.github.com> * Fix spacing in pgDatabaseQuery Co-authored-by: Joe Adams <github@joeadams.io> Signed-off-by: Jocelyn Thode <jocelynthode@users.noreply.github.com> * Fix case on pgRolesConnectionLimitsQuery Co-authored-by: Joe Adams <github@joeadams.io> Signed-off-by: Jocelyn Thode <jocelynthode@users.noreply.github.com> * Do not add roleMetrics when row is not valid Signed-off-by: Jocelyn Thode <jocelyn@thode.email> --------- Signed-off-by: Jocelyn Thode <jocelyn@thode.email> Signed-off-by: Jocelyn Thode <jocelynthode@users.noreply.github.com> Co-authored-by: Joe Adams <github@joeadams.io>
92 lines
2.2 KiB
Go
92 lines
2.2 KiB
Go
// Copyright 2024 The Prometheus Authors
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package collector
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/go-kit/log"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
const rolesSubsystem = "roles"
|
|
|
|
func init() {
|
|
registerCollector(rolesSubsystem, defaultEnabled, NewPGRolesCollector)
|
|
}
|
|
|
|
type PGRolesCollector struct {
|
|
log log.Logger
|
|
}
|
|
|
|
func NewPGRolesCollector(config collectorConfig) (Collector, error) {
|
|
return &PGRolesCollector{
|
|
log: config.logger,
|
|
}, nil
|
|
}
|
|
|
|
var (
|
|
pgRolesConnectionLimitsDesc = prometheus.NewDesc(
|
|
prometheus.BuildFQName(
|
|
namespace,
|
|
rolesSubsystem,
|
|
"connection_limit",
|
|
),
|
|
"Connection limit set for the role",
|
|
[]string{"rolname"}, nil,
|
|
)
|
|
|
|
pgRolesConnectionLimitsQuery = "SELECT pg_roles.rolname, pg_roles.rolconnlimit FROM pg_roles"
|
|
)
|
|
|
|
// Update implements Collector and exposes roles connection limits.
|
|
// It is called by the Prometheus registry when collecting metrics.
|
|
func (c PGRolesCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
|
|
db := instance.getDB()
|
|
// Query the list of databases
|
|
rows, err := db.QueryContext(ctx,
|
|
pgRolesConnectionLimitsQuery,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var rolname sql.NullString
|
|
var connLimit sql.NullInt64
|
|
if err := rows.Scan(&rolname, &connLimit); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !rolname.Valid {
|
|
continue
|
|
}
|
|
rolnameLabel := rolname.String
|
|
|
|
if !connLimit.Valid {
|
|
continue
|
|
}
|
|
connLimitMetric := float64(connLimit.Int64)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
pgRolesConnectionLimitsDesc,
|
|
prometheus.GaugeValue, connLimitMetric, rolnameLabel,
|
|
)
|
|
}
|
|
|
|
return rows.Err()
|
|
}
|