@@ -53,12 +53,21 @@ var (
53
53
"Disk space used by the database" ,
54
54
[]string {"datname" }, nil ,
55
55
)
56
+ pgDatabaseConnectionLimitsDesc = prometheus .NewDesc (
57
+ prometheus .BuildFQName (
58
+ namespace ,
59
+ databaseSubsystem ,
60
+ "connection_limit" ,
61
+ ),
62
+ "Connection limit set for the database" ,
63
+ []string {"datname" }, nil ,
64
+ )
56
65
57
- pgDatabaseQuery = "SELECT pg_database.datname FROM pg_database; "
66
+ pgDatabaseQuery = "select pg_database.datname,pg_database.datconnlimit FROM pg_database"
58
67
pgDatabaseSizeQuery = "SELECT pg_database_size($1)"
59
68
)
60
69
61
- // Update implements Collector and exposes database size.
70
+ // Update implements Collector and exposes database size and connection limits .
62
71
// It is called by the Prometheus registry when collecting metrics.
63
72
// The list of databases is retrieved from pg_database and filtered
64
73
// by the excludeDatabase config parameter. The tradeoff here is that
@@ -81,21 +90,32 @@ func (c PGDatabaseCollector) Update(ctx context.Context, instance *instance, ch
81
90
82
91
for rows .Next () {
83
92
var datname sql.NullString
84
- if err := rows .Scan (& datname ); err != nil {
93
+ var connLimit sql.NullInt64
94
+ if err := rows .Scan (& datname , & connLimit ); err != nil {
85
95
return err
86
96
}
87
97
88
98
if ! datname .Valid {
89
99
continue
90
100
}
101
+ database := datname .String
91
102
// Ignore excluded databases
92
103
// Filtering is done here instead of in the query to avoid
93
104
// a complicated NOT IN query with a variable number of parameters
94
- if sliceContains (c .excludedDatabases , datname . String ) {
105
+ if sliceContains (c .excludedDatabases , database ) {
95
106
continue
96
107
}
97
108
98
- databases = append (databases , datname .String )
109
+ databases = append (databases , database )
110
+
111
+ connLimitMetric := 0.0
112
+ if connLimit .Valid {
113
+ connLimitMetric = float64 (connLimit .Int64 )
114
+ }
115
+ ch <- prometheus .MustNewConstMetric (
116
+ pgDatabaseConnectionLimitsDesc ,
117
+ prometheus .GaugeValue , connLimitMetric , database ,
118
+ )
99
119
}
100
120
101
121
// Query the size of the databases
@@ -114,6 +134,7 @@ func (c PGDatabaseCollector) Update(ctx context.Context, instance *instance, ch
114
134
pgDatabaseSizeDesc ,
115
135
prometheus .GaugeValue , sizeMetric , datname ,
116
136
)
137
+
117
138
}
118
139
return rows .Err ()
119
140
}
0 commit comments