Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include all idle processes in the process idle metrics #862

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}