Skip to content
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## main / (unreleased)

* [CHANGE] ...
* [FEATURE] ...
* [FEATURE] Add Amazon Aurora PostgreSQL support: auto-detection via `aurora_version()` and 9 new `aurora_*` collectors, all disabled by default.
* [ENHANCEMENT] ...
* [BUGFIX] ...

Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,39 @@ This will build the docker image as `prometheuscommunity/postgres_exporter:${bra
* `[no-]collector.xlog_location`
Enable the `xlog_location` collector (default: disabled).

#### Amazon Aurora PostgreSQL collectors

These collectors expose metrics that only exist on Amazon Aurora PostgreSQL.
They are all disabled by default and detect Aurora automatically — when
enabled on a non-Aurora server they silently emit no data (no log noise).

* `[no-]collector.aurora_replica_status`
Replica lag, replay latency, pending read IOs from `aurora_replica_status()`. (default: disabled)

* `[no-]collector.aurora_global_db_status`
Per-region durability lag and RPO lag from `aurora_global_db_status()`. (default: disabled)

* `[no-]collector.aurora_global_db_instance_status`
Per-instance visibility lag from `aurora_global_db_instance_status()`. (default: disabled)

* `[no-]collector.aurora_stat_database`
Aurora-specific per-database I/O counters (storage / optimized reads cache / local) from `aurora_stat_database()`. (default: disabled)

* `[no-]collector.aurora_stat_bgwriter`
Aurora Optimized Reads cache writes from `aurora_stat_bgwriter()`. (default: disabled)

* `[no-]collector.aurora_stat_commit_latency`
Cumulative per-database commit latency from `aurora_stat_get_db_commit_latency()`. (default: disabled)

* `[no-]collector.aurora_stat_dml_activity`
Per-database SELECT/INSERT/UPDATE/DELETE counts and latency from `aurora_stat_dml_activity()`. (default: disabled)

* `[no-]collector.aurora_stat_optimized_reads_cache`
Aurora Optimized Reads cache total/used size from `aurora_stat_optimized_reads_cache()`. (default: disabled)

* `[no-]collector.aurora_stat_logical_wal_cache`
Per-replication-slot WAL cache hits/misses from `aurora_stat_logical_wal_cache()`. (default: disabled)

* `config.file`
Set the config file path. Default is `postgres_exporter.yml`

Expand Down
84 changes: 84 additions & 0 deletions collector/aurora_global_db_instance_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2025 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"context"
"database/sql"

"github.com/prometheus/client_golang/prometheus"
)

// aurora_global_db_instance_status() exposes per-instance lag across regions
// in an Aurora Global Database. Disabled by default; only useful on Aurora.
const auroraGlobalDBInstanceSubsystem = "aurora_global_db_instance"

func init() {
registerCollector("aurora_global_db_instance_status", defaultDisabled, NewAuroraGlobalDBInstanceStatusCollector)
}

type AuroraGlobalDBInstanceStatusCollector struct{}

func NewAuroraGlobalDBInstanceStatusCollector(collectorConfig) (Collector, error) {
return &AuroraGlobalDBInstanceStatusCollector{}, nil
}

var (
auroraGlobalDBInstanceVisibilityLagDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, auroraGlobalDBInstanceSubsystem, "visibility_lag_milliseconds"),
"How far this DB instance is lagging behind the writer DB instance in milliseconds. NULL for the writer.",
[]string{"server_id", "aws_region"}, nil,
)

auroraGlobalDBInstanceStatusQuery = `SELECT
server_id,
aws_region,
visibility_lag_in_msec
FROM aurora_global_db_instance_status()`
)

func (c AuroraGlobalDBInstanceStatusCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
if !instance.isAurora {
return ErrNoData
}
rows, err := instance.getDB().QueryContext(ctx, auroraGlobalDBInstanceStatusQuery)
if err != nil {
return err
}
defer rows.Close()

var found bool
for rows.Next() {
found = true

var serverID, awsRegion string
var visibilityLag sql.NullFloat64

if err := rows.Scan(&serverID, &awsRegion, &visibilityLag); err != nil {
return err
}

if visibilityLag.Valid {
ch <- prometheus.MustNewConstMetric(auroraGlobalDBInstanceVisibilityLagDesc, prometheus.GaugeValue, visibilityLag.Float64, serverID, awsRegion)
}
}

if err := rows.Err(); err != nil {
return err
}
if !found {
return ErrNoData
}
return nil
}
81 changes: 81 additions & 0 deletions collector/aurora_global_db_instance_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2025 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"context"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/prometheus/client_golang/prometheus"
"github.com/smartystreets/goconvey/convey"
)

func TestAuroraGlobalDBInstanceStatusCollector(t *testing.T) {
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, isAurora: true}

columns := []string{"server_id", "aws_region", "visibility_lag_in_msec"}
rows := sqlmock.NewRows(columns).
AddRow("writer", "eu-west-1", nil). // writer: NULL lag, skipped
AddRow("reader-1", "eu-west-1", 6.0).
AddRow("reader-2", "eu-central-1", 996.0)
mock.ExpectQuery(sanitizeQuery(auroraGlobalDBInstanceStatusQuery)).WillReturnRows(rows)

ch := make(chan prometheus.Metric)
go func() {
defer close(ch)
c := AuroraGlobalDBInstanceStatusCollector{}
if err := c.Update(context.Background(), inst, ch); err != nil {
t.Errorf("Error calling Update: %s", err)
}
}()

got := 0
for range ch {
got++
}
convey.Convey("Metrics count", t, func() {
convey.So(got, convey.ShouldEqual, 2) // writer skipped (NULL), 2 readers emitted
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("Unfulfilled expectations: %s", err)
}
})
}

func TestAuroraGlobalDBInstanceStatusCollectorNotAurora(t *testing.T) {
db, _, err := sqlmock.New()
if err != nil {
t.Fatalf("Error opening a stub db connection: %s", err)
}
defer db.Close()

inst := &instance{db: db, isAurora: false}

ch := make(chan prometheus.Metric)
go func() {
defer close(ch)
c := AuroraGlobalDBInstanceStatusCollector{}
if err := c.Update(context.Background(), inst, ch); err != ErrNoData {
t.Errorf("Expected ErrNoData, got: %v", err)
}
}()
for range ch {
}
}
93 changes: 93 additions & 0 deletions collector/aurora_global_db_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2025 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"context"
"database/sql"

"github.com/prometheus/client_golang/prometheus"
)

// auroraGlobalDBSubsystem exposes metrics from Aurora PostgreSQL's
// aurora_global_db_status() function. Disabled by default; only useful for
// Aurora Global Database deployments.
const auroraGlobalDBSubsystem = "aurora_global_db"

func init() {
registerCollector("aurora_global_db_status", defaultDisabled, NewAuroraGlobalDBStatusCollector)
}

type AuroraGlobalDBStatusCollector struct{}

func NewAuroraGlobalDBStatusCollector(collectorConfig) (Collector, error) {
return &AuroraGlobalDBStatusCollector{}, nil
}

var (
auroraGlobalDurabilityLagDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, auroraGlobalDBSubsystem, "durability_lag_milliseconds"),
"Storage lag vs primary cluster in milliseconds (-1 for the primary cluster).",
[]string{"aws_region"}, nil,
)
auroraGlobalRPOLagDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, auroraGlobalDBSubsystem, "rpo_lag_milliseconds"),
"Recovery point objective lag in milliseconds (-1 for the primary cluster).",
[]string{"aws_region"}, nil,
)

auroraGlobalDBStatusQuery = `SELECT
aws_region,
durability_lag_in_msec,
rpo_lag_in_msec
FROM aurora_global_db_status()`
)

func (c AuroraGlobalDBStatusCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
if !instance.isAurora {
return ErrNoData
}
rows, err := instance.getDB().QueryContext(ctx, auroraGlobalDBStatusQuery)
if err != nil {
return err
}
defer rows.Close()

var found bool
for rows.Next() {
found = true

var awsRegion string
var durabilityLag, rpoLag sql.NullFloat64

if err := rows.Scan(&awsRegion, &durabilityLag, &rpoLag); err != nil {
return err
}

if durabilityLag.Valid {
ch <- prometheus.MustNewConstMetric(auroraGlobalDurabilityLagDesc, prometheus.GaugeValue, durabilityLag.Float64, awsRegion)
}
if rpoLag.Valid {
ch <- prometheus.MustNewConstMetric(auroraGlobalRPOLagDesc, prometheus.GaugeValue, rpoLag.Float64, awsRegion)
}
}

if err := rows.Err(); err != nil {
return err
}
if !found {
return ErrNoData
}
return nil
}
Loading