Skip to content

Commit 004dfcb

Browse files
committed
Add usename and application_name to pg_locks_count
Signed-off-by: killzoner <[email protected]>
1 parent 3c5ef40 commit 004dfcb

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

collector/pg_locks.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ var (
4545
"count",
4646
),
4747
"Number of locks",
48-
[]string{"datname", "mode"}, nil,
48+
[]string{"datname", "mode", "usename", "application_name"}, nil,
4949
)
5050

5151
pgLocksQuery = `
5252
SELECT
5353
pg_database.datname as datname,
5454
tmp.mode as mode,
55+
COALESCE(usename, ''),
56+
COALESCE(application_name, ''),
5557
COALESCE(count, 0) as count
5658
FROM
5759
(
@@ -71,14 +73,18 @@ var (
7173
SELECT
7274
database,
7375
lower(mode) AS mode,
74-
count(*) AS count
75-
FROM
76-
pg_locks
76+
count(*) AS count,
77+
usename,
78+
application_name
79+
FROM
80+
pg_locks l JOIN pg_stat_activity a ON a.pid = l.pid
7781
WHERE
7882
database IS NOT NULL
79-
GROUP BY
80-
database,
81-
lower(mode)
83+
GROUP BY
84+
database,
85+
lower(mode),
86+
usename,
87+
application_name
8288
) AS tmp2 ON tmp.mode = tmp2.mode
8389
and pg_database.oid = tmp2.database
8490
ORDER BY
@@ -99,15 +105,15 @@ func (c PGLocksCollector) Update(ctx context.Context, instance *instance, ch cha
99105
}
100106
defer rows.Close()
101107

102-
var datname, mode sql.NullString
108+
var datname, mode, usename, applicationName sql.NullString
103109
var count sql.NullInt64
104110

105111
for rows.Next() {
106-
if err := rows.Scan(&datname, &mode, &count); err != nil {
112+
if err := rows.Scan(&datname, &mode, &usename, &applicationName, &count); err != nil {
107113
return err
108114
}
109115

110-
if !datname.Valid || !mode.Valid {
116+
if !datname.Valid || !mode.Valid || !usename.Valid || !applicationName.Valid {
111117
continue
112118
}
113119

@@ -119,7 +125,7 @@ func (c PGLocksCollector) Update(ctx context.Context, instance *instance, ch cha
119125
ch <- prometheus.MustNewConstMetric(
120126
pgLocksDesc,
121127
prometheus.GaugeValue, countMetric,
122-
datname.String, mode.String,
128+
datname.String, mode.String, usename.String, applicationName.String,
123129
)
124130
}
125131
if err := rows.Err(); err != nil {

collector/pg_locks_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ func TestPGLocksCollector(t *testing.T) {
3131

3232
inst := &instance{db: db}
3333

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

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

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

4849
expected := []MetricResult{
49-
{labels: labelMap{"datname": "test", "mode": "exclusivelock"}, value: 42, metricType: dto.MetricType_GAUGE},
50+
{labels: labelMap{"datname": "test", "mode": "exclusivelock", "usename": "", "application_name": ""}, value: 42, metricType: dto.MetricType_GAUGE},
51+
{labels: labelMap{"datname": "test2", "mode": "exclusivelock", "usename": "myaccount", "application_name": "myapp"}, value: 21, metricType: dto.MetricType_GAUGE},
5052
}
5153
convey.Convey("Metrics comparison", t, func() {
5254
for _, expect := range expected {

0 commit comments

Comments
 (0)