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

feat: add usename to pg_process_idle metric #954

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions collector/pg_process_idle.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
SELECT
state,
application_name,
usename,
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 state, application_name
GROUP BY state, application_name, usename
),
buckets AS (
SELECT
Expand All @@ -78,21 +79,23 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
SELECT
state,
application_name,
usename,
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 (state, application_name)
GROUP BY 1, 2, 3, 4;`)
GROUP BY 1, 2, 3, 4, 5;`)

var state sql.NullString
var applicationName sql.NullString
var usename sql.NullString
var secondsSum sql.NullFloat64
var secondsCount sql.NullInt64
var seconds []float64
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 {
return err
}
Expand All @@ -115,6 +118,11 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
applicationNameLabel = applicationName.String
}

usenameLabel := "unknown"
if usename.Valid {
usenameLabel = usename.String
}

var secondsCountMetric uint64
if secondsCount.Valid {
secondsCountMetric = uint64(secondsCount.Int64)
Expand All @@ -126,7 +134,7 @@ func (PGProcessIdleCollector) Update(ctx context.Context, instance *instance, ch
ch <- prometheus.MustNewConstHistogram(
pgProcessIdleSeconds,
secondsCountMetric, secondsSumMetric, buckets,
stateLabel, applicationNameLabel,
stateLabel, applicationNameLabel, usenameLabel,
)
return nil
}