Skip to content

Commit 0cb11b2

Browse files
committed
feat: add usename label
Signed-off-by: rezaxd <[email protected]>
1 parent 94b0651 commit 0cb11b2

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

collector/pg_process_idle.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
5252
SELECT
5353
state,
5454
application_name,
55+
usename,
5556
SUM(EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - state_change))::bigint)::float AS process_idle_seconds_sum,
5657
COUNT(*) AS process_idle_seconds_count
5758
FROM pg_stat_activity
5859
WHERE state ~ '^idle'
59-
GROUP BY state, application_name
60+
GROUP BY state, application_name, usename
6061
),
6162
buckets AS (
6263
SELECT
@@ -78,21 +79,23 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
7879
SELECT
7980
state,
8081
application_name,
82+
usename,
8183
process_idle_seconds_sum as seconds_sum,
8284
process_idle_seconds_count as seconds_count,
8385
ARRAY_AGG(le) AS seconds,
8486
ARRAY_AGG(bucket) AS seconds_bucket
8587
FROM metrics JOIN buckets USING (state, application_name)
86-
GROUP BY 1, 2, 3, 4;`)
88+
GROUP BY 1, 2, 3, 4, 5;`)
8789

8890
var state sql.NullString
8991
var applicationName sql.NullString
92+
var usename sql.NullString
9093
var secondsSum sql.NullFloat64
9194
var secondsCount sql.NullInt64
9295
var seconds []float64
9396
var secondsBucket []int64
9497

95-
err := row.Scan(&state, &applicationName, &secondsSum, &secondsCount, pq.Array(&seconds), pq.Array(&secondsBucket))
98+
err := row.Scan(&state, &applicationName, &usename, &secondsSum, &secondsCount, pq.Array(&seconds), pq.Array(&secondsBucket))
9699
if err != nil {
97100
return err
98101
}
@@ -115,6 +118,11 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
115118
applicationNameLabel = applicationName.String
116119
}
117120

121+
usenameLabel := "unknown"
122+
if usename.Valid {
123+
usenameLabel = usename.String
124+
}
125+
118126
var secondsCountMetric uint64
119127
if secondsCount.Valid {
120128
secondsCountMetric = uint64(secondsCount.Int64)
@@ -126,7 +134,7 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
126134
ch <- prometheus.MustNewConstHistogram(
127135
pgProcessIdleSeconds,
128136
secondsCountMetric, secondsSumMetric, buckets,
129-
stateLabel, applicationNameLabel,
137+
stateLabel, applicationNameLabel, usenameLabel,
130138
)
131139
return nil
132140
}

0 commit comments

Comments
 (0)