Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions collector/pg_locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ var (
"count",
),
"Number of locks",
[]string{"datname", "mode"}, nil,
[]string{"datname", "mode", "usename", "application_name"}, nil,
)

pgLocksQuery = `
SELECT
pg_database.datname as datname,
tmp.mode as mode,
COALESCE(usename, ''),
COALESCE(application_name, ''),
COALESCE(count, 0) as count
FROM
(
Expand All @@ -71,14 +73,18 @@ var (
SELECT
database,
lower(mode) AS mode,
count(*) AS count
FROM
pg_locks
count(*) AS count,
usename,
application_name
FROM
pg_locks l JOIN pg_stat_activity a ON a.pid = l.pid
WHERE
database IS NOT NULL
GROUP BY
database,
lower(mode)
GROUP BY
database,
lower(mode),
usename,
application_name
) AS tmp2 ON tmp.mode = tmp2.mode
and pg_database.oid = tmp2.database
ORDER BY
Expand All @@ -99,15 +105,15 @@ func (c PGLocksCollector) Update(ctx context.Context, instance *instance, ch cha
}
defer rows.Close()

var datname, mode sql.NullString
var datname, mode, usename, applicationName sql.NullString
var count sql.NullInt64

for rows.Next() {
if err := rows.Scan(&datname, &mode, &count); err != nil {
if err := rows.Scan(&datname, &mode, &usename, &applicationName, &count); err != nil {
return err
}

if !datname.Valid || !mode.Valid {
if !datname.Valid || !mode.Valid || !usename.Valid || !applicationName.Valid {
continue
}

Expand All @@ -119,7 +125,7 @@ func (c PGLocksCollector) Update(ctx context.Context, instance *instance, ch cha
ch <- prometheus.MustNewConstMetric(
pgLocksDesc,
prometheus.GaugeValue, countMetric,
datname.String, mode.String,
datname.String, mode.String, usename.String, applicationName.String,
)
}
if err := rows.Err(); err != nil {
Expand Down
8 changes: 5 additions & 3 deletions collector/pg_locks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func TestPGLocksCollector(t *testing.T) {

inst := &instance{db: db}

rows := sqlmock.NewRows([]string{"datname", "mode", "count"}).
AddRow("test", "exclusivelock", 42)
rows := sqlmock.NewRows([]string{"datname", "mode", "usename", "application_name", "count"}).
AddRow("test", "exclusivelock", "", "", 42).
AddRow("test2", "exclusivelock", "myaccount", "myapp", 21)

mock.ExpectQuery(sanitizeQuery(pgLocksQuery)).WillReturnRows(rows)

Expand All @@ -46,7 +47,8 @@ func TestPGLocksCollector(t *testing.T) {
}()

expected := []MetricResult{
{labels: labelMap{"datname": "test", "mode": "exclusivelock"}, value: 42, metricType: dto.MetricType_GAUGE},
{labels: labelMap{"datname": "test", "mode": "exclusivelock", "usename": "", "application_name": ""}, value: 42, metricType: dto.MetricType_GAUGE},
{labels: labelMap{"datname": "test2", "mode": "exclusivelock", "usename": "myaccount", "application_name": "myapp"}, value: 21, metricType: dto.MetricType_GAUGE},
}
convey.Convey("Metrics comparison", t, func() {
for _, expect := range expected {
Expand Down