|
| 1 | +// Copyright 2023 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package collector |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "database/sql" |
| 19 | + |
| 20 | + "github.com/go-kit/log" |
| 21 | + "github.com/prometheus/client_golang/prometheus" |
| 22 | +) |
| 23 | + |
| 24 | +func init() { |
| 25 | + registerCollector("statements", defaultEnabled, NewPGProcessIdleCollector) |
| 26 | +} |
| 27 | + |
| 28 | +type PGProcessIdleCollector struct { |
| 29 | + log log.Logger |
| 30 | +} |
| 31 | + |
| 32 | +const processIdleSubsystem = "process_idle" |
| 33 | + |
| 34 | +func NewPGProcessIdleCollector(config collectorConfig) (Collector, error) { |
| 35 | + return &PGProcessIdleCollector{log: config.logger}, nil |
| 36 | +} |
| 37 | + |
| 38 | +var pgProcessIdleSeconds = prometheus.NewDesc( |
| 39 | + prometheus.BuildFQName(namespace, processIdleSubsystem, "seconds"), |
| 40 | + "Idle time of server processes", |
| 41 | + []string{"application_name"}, |
| 42 | + prometheus.Labels{}, |
| 43 | +) |
| 44 | + |
| 45 | +func (PGProcessIdleCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error { |
| 46 | + row := db.QueryRowContext(ctx, |
| 47 | + `WITH |
| 48 | + metrics AS ( |
| 49 | + SELECT |
| 50 | + application_name, |
| 51 | + SUM(EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - state_change))::bigint)::float AS process_idle_seconds_sum, |
| 52 | + COUNT(*) AS process_idle_seconds_count |
| 53 | + FROM pg_stat_activity |
| 54 | + WHERE state = 'idle' |
| 55 | + GROUP BY application_name |
| 56 | + ), |
| 57 | + buckets AS ( |
| 58 | + SELECT |
| 59 | + application_name, |
| 60 | + le, |
| 61 | + SUM( |
| 62 | + CASE WHEN EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - state_change)) <= le |
| 63 | + THEN 1 |
| 64 | + ELSE 0 |
| 65 | + END |
| 66 | + )::bigint AS bucket |
| 67 | + FROM |
| 68 | + pg_stat_activity, |
| 69 | + UNNEST(ARRAY[1, 2, 5, 15, 30, 60, 90, 120, 300]) AS le |
| 70 | + GROUP BY application_name, le |
| 71 | + ORDER BY application_name, le |
| 72 | + ) |
| 73 | + SELECT |
| 74 | + application_name, |
| 75 | + process_idle_seconds_sum as seconds_sum, |
| 76 | + process_idle_seconds_count as seconds_count, |
| 77 | + ARRAY_AGG(le) AS seconds, |
| 78 | + ARRAY_AGG(bucket) AS seconds_bucket |
| 79 | + FROM metrics JOIN buckets USING (application_name) |
| 80 | + GROUP BY 1, 2, 3;`) |
| 81 | + |
| 82 | + var applicationName string |
| 83 | + var secondsSum int64 |
| 84 | + var secondsCount uint64 |
| 85 | + var seconds []int64 |
| 86 | + var secondsBucket []uint64 |
| 87 | + |
| 88 | + err := row.Scan(&applicationName, &secondsSum, &secondsCount, &seconds, &secondsBucket) |
| 89 | + |
| 90 | + var buckets = make(map[float64]uint64, len(seconds)) |
| 91 | + for i, second := range seconds { |
| 92 | + if i >= len(secondsBucket) { |
| 93 | + break |
| 94 | + } |
| 95 | + buckets[float64(second)] = secondsBucket[i] |
| 96 | + } |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + ch <- prometheus.MustNewConstHistogram( |
| 101 | + pgProcessIdleSeconds, |
| 102 | + secondsCount, float64(secondsSum), buckets, |
| 103 | + applicationName, |
| 104 | + ) |
| 105 | + return nil |
| 106 | +} |
0 commit comments