Add connection limits metrics for pg_roles and pg_database (#997)
* 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>
This commit is contained in:
@@ -53,12 +53,21 @@ var (
|
||||
"Disk space used by the database",
|
||||
[]string{"datname"}, nil,
|
||||
)
|
||||
pgDatabaseConnectionLimitsDesc = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(
|
||||
namespace,
|
||||
databaseSubsystem,
|
||||
"connection_limit",
|
||||
),
|
||||
"Connection limit set for the database",
|
||||
[]string{"datname"}, nil,
|
||||
)
|
||||
|
||||
pgDatabaseQuery = "SELECT pg_database.datname FROM pg_database;"
|
||||
pgDatabaseQuery = "SELECT pg_database.datname, pg_database.datconnlimit FROM pg_database;"
|
||||
pgDatabaseSizeQuery = "SELECT pg_database_size($1)"
|
||||
)
|
||||
|
||||
// Update implements Collector and exposes database size.
|
||||
// Update implements Collector and exposes database size and connection limits.
|
||||
// It is called by the Prometheus registry when collecting metrics.
|
||||
// The list of databases is retrieved from pg_database and filtered
|
||||
// by the excludeDatabase config parameter. The tradeoff here is that
|
||||
@@ -81,21 +90,32 @@ func (c PGDatabaseCollector) Update(ctx context.Context, instance *instance, ch
|
||||
|
||||
for rows.Next() {
|
||||
var datname sql.NullString
|
||||
if err := rows.Scan(&datname); err != nil {
|
||||
var connLimit sql.NullInt64
|
||||
if err := rows.Scan(&datname, &connLimit); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !datname.Valid {
|
||||
continue
|
||||
}
|
||||
database := datname.String
|
||||
// Ignore excluded databases
|
||||
// Filtering is done here instead of in the query to avoid
|
||||
// a complicated NOT IN query with a variable number of parameters
|
||||
if sliceContains(c.excludedDatabases, datname.String) {
|
||||
if sliceContains(c.excludedDatabases, database) {
|
||||
continue
|
||||
}
|
||||
|
||||
databases = append(databases, datname.String)
|
||||
databases = append(databases, database)
|
||||
|
||||
connLimitMetric := 0.0
|
||||
if connLimit.Valid {
|
||||
connLimitMetric = float64(connLimit.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
pgDatabaseConnectionLimitsDesc,
|
||||
prometheus.GaugeValue, connLimitMetric, database,
|
||||
)
|
||||
}
|
||||
|
||||
// Query the size of the databases
|
||||
@@ -114,6 +134,7 @@ func (c PGDatabaseCollector) Update(ctx context.Context, instance *instance, ch
|
||||
pgDatabaseSizeDesc,
|
||||
prometheus.GaugeValue, sizeMetric, datname,
|
||||
)
|
||||
|
||||
}
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user