mirror of
https://github.com/prometheus-community/postgres_exporter
synced 2025-04-25 04:28:01 +00:00
feat: add usename label
Signed-off-by: rezaxd <r324r11@gmail.com>
This commit is contained in:
parent
94b0651246
commit
0cb11b2987
@ -52,11 +52,12 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
|
|||||||
SELECT
|
SELECT
|
||||||
state,
|
state,
|
||||||
application_name,
|
application_name,
|
||||||
|
usename,
|
||||||
SUM(EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - state_change))::bigint)::float AS process_idle_seconds_sum,
|
SUM(EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - state_change))::bigint)::float AS process_idle_seconds_sum,
|
||||||
COUNT(*) AS process_idle_seconds_count
|
COUNT(*) AS process_idle_seconds_count
|
||||||
FROM pg_stat_activity
|
FROM pg_stat_activity
|
||||||
WHERE state ~ '^idle'
|
WHERE state ~ '^idle'
|
||||||
GROUP BY state, application_name
|
GROUP BY state, application_name, usename
|
||||||
),
|
),
|
||||||
buckets AS (
|
buckets AS (
|
||||||
SELECT
|
SELECT
|
||||||
@ -78,21 +79,23 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
|
|||||||
SELECT
|
SELECT
|
||||||
state,
|
state,
|
||||||
application_name,
|
application_name,
|
||||||
|
usename,
|
||||||
process_idle_seconds_sum as seconds_sum,
|
process_idle_seconds_sum as seconds_sum,
|
||||||
process_idle_seconds_count as seconds_count,
|
process_idle_seconds_count as seconds_count,
|
||||||
ARRAY_AGG(le) AS seconds,
|
ARRAY_AGG(le) AS seconds,
|
||||||
ARRAY_AGG(bucket) AS seconds_bucket
|
ARRAY_AGG(bucket) AS seconds_bucket
|
||||||
FROM metrics JOIN buckets USING (state, application_name)
|
FROM metrics JOIN buckets USING (state, application_name)
|
||||||
GROUP BY 1, 2, 3, 4;`)
|
GROUP BY 1, 2, 3, 4, 5;`)
|
||||||
|
|
||||||
var state sql.NullString
|
var state sql.NullString
|
||||||
var applicationName sql.NullString
|
var applicationName sql.NullString
|
||||||
|
var usename sql.NullString
|
||||||
var secondsSum sql.NullFloat64
|
var secondsSum sql.NullFloat64
|
||||||
var secondsCount sql.NullInt64
|
var secondsCount sql.NullInt64
|
||||||
var seconds []float64
|
var seconds []float64
|
||||||
var secondsBucket []int64
|
var secondsBucket []int64
|
||||||
|
|
||||||
err := row.Scan(&state, &applicationName, &secondsSum, &secondsCount, pq.Array(&seconds), pq.Array(&secondsBucket))
|
err := row.Scan(&state, &applicationName, &usename, &secondsSum, &secondsCount, pq.Array(&seconds), pq.Array(&secondsBucket))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -115,6 +118,11 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
|
|||||||
applicationNameLabel = applicationName.String
|
applicationNameLabel = applicationName.String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
usenameLabel := "unknown"
|
||||||
|
if usename.Valid {
|
||||||
|
usenameLabel = usename.String
|
||||||
|
}
|
||||||
|
|
||||||
var secondsCountMetric uint64
|
var secondsCountMetric uint64
|
||||||
if secondsCount.Valid {
|
if secondsCount.Valid {
|
||||||
secondsCountMetric = uint64(secondsCount.Int64)
|
secondsCountMetric = uint64(secondsCount.Int64)
|
||||||
@ -126,7 +134,7 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
|
|||||||
ch <- prometheus.MustNewConstHistogram(
|
ch <- prometheus.MustNewConstHistogram(
|
||||||
pgProcessIdleSeconds,
|
pgProcessIdleSeconds,
|
||||||
secondsCountMetric, secondsSumMetric, buckets,
|
secondsCountMetric, secondsSumMetric, buckets,
|
||||||
stateLabel, applicationNameLabel,
|
stateLabel, applicationNameLabel, usenameLabel,
|
||||||
)
|
)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user