@@ -52,11 +52,12 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
52
52
SELECT
53
53
state,
54
54
application_name,
55
+ usename,
55
56
SUM(EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - state_change))::bigint)::float AS process_idle_seconds_sum,
56
57
COUNT(*) AS process_idle_seconds_count
57
58
FROM pg_stat_activity
58
59
WHERE state ~ '^idle'
59
- GROUP BY state, application_name
60
+ GROUP BY state, application_name, usename
60
61
),
61
62
buckets AS (
62
63
SELECT
@@ -78,21 +79,23 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
78
79
SELECT
79
80
state,
80
81
application_name,
82
+ usename,
81
83
process_idle_seconds_sum as seconds_sum,
82
84
process_idle_seconds_count as seconds_count,
83
85
ARRAY_AGG(le) AS seconds,
84
86
ARRAY_AGG(bucket) AS seconds_bucket
85
87
FROM metrics JOIN buckets USING (state, application_name)
86
- GROUP BY 1, 2, 3, 4;` )
88
+ GROUP BY 1, 2, 3, 4, 5 ;` )
87
89
88
90
var state sql.NullString
89
91
var applicationName sql.NullString
92
+ var usename sql.NullString
90
93
var secondsSum sql.NullFloat64
91
94
var secondsCount sql.NullInt64
92
95
var seconds []float64
93
96
var secondsBucket []int64
94
97
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 ))
96
99
if err != nil {
97
100
return err
98
101
}
@@ -115,6 +118,11 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
115
118
applicationNameLabel = applicationName .String
116
119
}
117
120
121
+ usenameLabel := "unknown"
122
+ if usename .Valid {
123
+ usenameLabel = usename .String
124
+ }
125
+
118
126
var secondsCountMetric uint64
119
127
if secondsCount .Valid {
120
128
secondsCountMetric = uint64 (secondsCount .Int64 )
@@ -126,7 +134,7 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
126
134
ch <- prometheus .MustNewConstHistogram (
127
135
pgProcessIdleSeconds ,
128
136
secondsCountMetric , secondsSumMetric , buckets ,
129
- stateLabel , applicationNameLabel ,
137
+ stateLabel , applicationNameLabel , usenameLabel ,
130
138
)
131
139
return nil
132
140
}
0 commit comments