mirror of
https://github.com/prometheus-community/postgres_exporter
synced 2025-02-28 08:21:05 +00:00
Include all idle processes in the process idle metrics
Signed-off-by: Tom Hughes <tom@compton.nu>
This commit is contained in:
parent
6b56e2f057
commit
a8b86cf7da
@ -40,7 +40,7 @@ func NewPGProcessIdleCollector(config collectorConfig) (Collector, error) {
|
||||
var pgProcessIdleSeconds = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, processIdleSubsystem, "seconds"),
|
||||
"Idle time of server processes",
|
||||
[]string{"application_name"},
|
||||
[]string{"state", "application_name"},
|
||||
prometheus.Labels{},
|
||||
)
|
||||
|
||||
@ -50,15 +50,17 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
|
||||
`WITH
|
||||
metrics AS (
|
||||
SELECT
|
||||
state,
|
||||
application_name,
|
||||
SUM(EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - state_change))::bigint)::float AS process_idle_seconds_sum,
|
||||
COUNT(*) AS process_idle_seconds_count
|
||||
FROM pg_stat_activity
|
||||
WHERE state = 'idle'
|
||||
GROUP BY application_name
|
||||
WHERE state ~ '^idle'
|
||||
GROUP BY state, application_name
|
||||
),
|
||||
buckets AS (
|
||||
SELECT
|
||||
state,
|
||||
application_name,
|
||||
le,
|
||||
SUM(
|
||||
@ -70,25 +72,27 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
|
||||
FROM
|
||||
pg_stat_activity,
|
||||
UNNEST(ARRAY[1, 2, 5, 15, 30, 60, 90, 120, 300]) AS le
|
||||
GROUP BY application_name, le
|
||||
ORDER BY application_name, le
|
||||
GROUP BY state, application_name, le
|
||||
ORDER BY state, application_name, le
|
||||
)
|
||||
SELECT
|
||||
state,
|
||||
application_name,
|
||||
process_idle_seconds_sum as seconds_sum,
|
||||
process_idle_seconds_count as seconds_count,
|
||||
ARRAY_AGG(le) AS seconds,
|
||||
ARRAY_AGG(bucket) AS seconds_bucket
|
||||
FROM metrics JOIN buckets USING (application_name)
|
||||
GROUP BY 1, 2, 3;`)
|
||||
FROM metrics JOIN buckets USING (state, application_name)
|
||||
GROUP BY 1, 2, 3, 4;`)
|
||||
|
||||
var state sql.NullString
|
||||
var applicationName sql.NullString
|
||||
var secondsSum sql.NullFloat64
|
||||
var secondsCount sql.NullInt64
|
||||
var seconds []float64
|
||||
var secondsBucket []int64
|
||||
|
||||
err := row.Scan(&applicationName, &secondsSum, &secondsCount, pq.Array(&seconds), pq.Array(&secondsBucket))
|
||||
err := row.Scan(&state, &applicationName, &secondsSum, &secondsCount, pq.Array(&seconds), pq.Array(&secondsBucket))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -101,6 +105,11 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
|
||||
buckets[second] = uint64(secondsBucket[i])
|
||||
}
|
||||
|
||||
stateLabel := "unknown"
|
||||
if state.Valid {
|
||||
stateLabel = state.String
|
||||
}
|
||||
|
||||
applicationNameLabel := "unknown"
|
||||
if applicationName.Valid {
|
||||
applicationNameLabel = applicationName.String
|
||||
@ -117,7 +126,7 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
|
||||
ch <- prometheus.MustNewConstHistogram(
|
||||
pgProcessIdleSeconds,
|
||||
secondsCountMetric, secondsSumMetric, buckets,
|
||||
applicationNameLabel,
|
||||
stateLabel, applicationNameLabel,
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user