1
0
mirror of https://github.com/prometheus-community/postgres_exporter.git synced 2025-12-21 04:00:58 +03:00

Fix NULL on long_running_transactions collector (#1230)

When there are no long running transactions, the oldest timestamp will be NULL. This sets the metrics value to 0 (age). Also adds a test for this behavior.

Fixes #1223

Signed-off-by: Joe Adams <github@joeadams.io>
This commit is contained in:
Joe Adams
2025-12-13 07:46:30 -05:00
committed by GitHub
parent d613418b00
commit 778660147e
2 changed files with 53 additions and 3 deletions

View File

@@ -15,6 +15,7 @@ package collector
import (
"context"
"database/sql"
"log/slog"
"github.com/prometheus/client_golang/prometheus"
@@ -50,7 +51,7 @@ var (
)
longRunningTransactionsQuery = `
SELECT
SELECT
COUNT(*) as transactions,
MAX(EXTRACT(EPOCH FROM clock_timestamp() - pg_stat_activity.xact_start)) AS oldest_timestamp_seconds
FROM pg_catalog.pg_stat_activity
@@ -72,12 +73,20 @@ func (PGLongRunningTransactionsCollector) Update(ctx context.Context, instance *
defer rows.Close()
for rows.Next() {
var transactions, ageInSeconds float64
var transactions float64
var ageInSeconds sql.NullFloat64
if err := rows.Scan(&transactions, &ageInSeconds); err != nil {
return err
}
// If there are no long running transactions, ageInSeconds will be NULL
// so we set it to 0
age := 0.0
if ageInSeconds.Valid {
age = ageInSeconds.Float64
}
ch <- prometheus.MustNewConstMetric(
longRunningTransactionsCount,
prometheus.GaugeValue,
@@ -86,7 +95,7 @@ func (PGLongRunningTransactionsCollector) Update(ctx context.Context, instance *
ch <- prometheus.MustNewConstMetric(
longRunningTransactionsAgeInSeconds,
prometheus.GaugeValue,
ageInSeconds,
age,
)
}
if err := rows.Err(); err != nil {