Skip to content

Commit

Permalink
Merge pull request #862 from tomhughes/idle-state
Browse files Browse the repository at this point in the history
Include all idle processes in the process idle metrics
  • Loading branch information
sysadmind authored Jul 6, 2023
2 parents 9a9a429 + a8b86cf commit 4ac5481
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions collector/pg_process_idle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
)

Expand All @@ -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(
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit 4ac5481

Please sign in to comment.