1
0
mirror of https://github.com/prometheus-community/postgres_exporter.git synced 2025-12-19 15:42:41 +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 ( import (
"context" "context"
"database/sql"
"log/slog" "log/slog"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@@ -72,12 +73,20 @@ func (PGLongRunningTransactionsCollector) Update(ctx context.Context, instance *
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var transactions, ageInSeconds float64 var transactions float64
var ageInSeconds sql.NullFloat64
if err := rows.Scan(&transactions, &ageInSeconds); err != nil { if err := rows.Scan(&transactions, &ageInSeconds); err != nil {
return err 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( ch <- prometheus.MustNewConstMetric(
longRunningTransactionsCount, longRunningTransactionsCount,
prometheus.GaugeValue, prometheus.GaugeValue,
@@ -86,7 +95,7 @@ func (PGLongRunningTransactionsCollector) Update(ctx context.Context, instance *
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
longRunningTransactionsAgeInSeconds, longRunningTransactionsAgeInSeconds,
prometheus.GaugeValue, prometheus.GaugeValue,
ageInSeconds, age,
) )
} }
if err := rows.Err(); err != nil { if err := rows.Err(); err != nil {

View File

@@ -61,3 +61,44 @@ func TestPGLongRunningTransactionsCollector(t *testing.T) {
t.Errorf("there were unfulfilled exceptions: %s", err) t.Errorf("there were unfulfilled exceptions: %s", err)
} }
} }
func TestPGLongRunningTransactionsCollectorNull(t *testing.T) {
// Test when no long running transactions are present
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("Error opening a stub db connection: %s", err)
}
defer db.Close()
inst := &instance{db: db}
columns := []string{
"transactions",
"age_in_seconds",
}
rows := sqlmock.NewRows(columns).
AddRow(0, nil)
mock.ExpectQuery(sanitizeQuery(longRunningTransactionsQuery)).WillReturnRows(rows)
ch := make(chan prometheus.Metric)
go func() {
defer close(ch)
c := PGLongRunningTransactionsCollector{}
if err := c.Update(context.Background(), inst, ch); err != nil {
t.Errorf("Error calling PGLongRunningTransactionsCollector.Update: %s", err)
}
}()
expected := []MetricResult{
{labels: labelMap{}, value: 0, metricType: dto.MetricType_GAUGE},
{labels: labelMap{}, value: 0, metricType: dto.MetricType_GAUGE},
}
convey.Convey("Metrics comparison", t, func() {
for _, expect := range expected {
m := readMetric(<-ch)
convey.So(expect, convey.ShouldResemble, m)
}
})
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled exceptions: %s", err)
}
}