This application is a Prometheus exporter for PostgreSQL, designed to collect and expose a wide range of PostgreSQL metrics. It provides detailed insights into PostgreSQL performance, including connections, queries, table statistics, replication, and more.
The exporter collects and exposes the following metrics:
- Connection usage percentage
- Idle connections
- Long-running queries
- Active and waiting queries
- Live tuples
- Dead tuples
- Table size
- Index size
- WAL size in bytes
- WAL files count
- Cache hits and reads
- Total buffer usage
- Replication lag in bytes
- Commit rate
- Rollback transactions
- Table bloat
- Autovacuum workers
- Temporary table size
- Checkpoint write time
- Deadlocks
- Locks
- User connections
- User queries
- Database transaction rate
- A PostgreSQL instance with sufficient permissions to access
pg_stat
and related views. - A Prometheus server to scrape and visualize the metrics.
- Go 1.16 or later (for building the application).
Follow these steps to set up PostMetric:
-
Clone the Repository
git clone [email protected]:kintsdev/postmetric.git cd postmetric
-
Build the Application
go build -o postmetric
-
Configure the PostgreSQL Connection Set the PostgreSQL connection string as an environment variable:
export POSTGRES_CONNECTION_STRING="postgresql://user:password@host:port/dbname?sslmode=disable"
-
Run the Application
./postmetric
Add the following scrape configuration to your Prometheus configuration file:
scrape_configs:
- job_name: 'postmetric'
static_configs:
- targets: ['localhost:8080']
PostMetric exposes metrics at:
http://localhost:8080/metrics
Below is an overview of the metrics exposed by PostMetric:
postgres_connection_usage
: Percentage of used connections out of the maximum connections.postgres_idle_connections
: Number of idle connections.
postgres_active_queries
: Number of currently active queries.postgres_waiting_queries
: Number of queries waiting for locks.postgres_long_running_queries
: Queries running for more than 5 minutes.
postgres_table_stats
: Includes the following statistics:- Live tuples
- Dead tuples
- Total size
- Table size
- Index size
- (Labeled by
table_name
andstat_type
)
postgres_replication_lag
: Replication lag in bytes.
postgres_wal_stats
: Includes:- WAL size
- WAL file count
postgres_buffer_cache
: Includes:- Cache hits
- Cache reads
- Total usage
postgres_commit_rate
: Percentage of committed transactions.postgres_rollback_transactions
: Number of rolled-back transactions.
postgres_table_bloat_bytes
: Estimated bloat size in bytes.postgres_autovacuum_workers
: Number of active autovacuum workers.postgres_temp_table_size_bytes
: Total size of temporary tables.postgres_checkpoint_write_time_ms
: Average checkpoint write time in milliseconds.postgres_deadlocks
: Number of deadlocks detected.postgres_locks
: Number of active locks.
postgres_user_connections
: Number of connections per user.postgres_user_queries
: Number of queries executed per user.
postgres_database_transaction_rate
: Transaction rate per database.
PostMetric provides a comprehensive set of metrics to help you monitor and optimize PostgreSQL performance effectively. Happy monitoring!
The codebase is organized into the following packages:
main
: The entry point of the application.metrics
: Contains metric definitions and registration logic.collector
: Contains the logic for collecting metrics from PostgreSQL.
To add new metrics, follow these steps:
- Define the Metric: Add the new metric definition in
pkg/metrics/metrics.go
. - Register the Metric: Ensure the new metric is registered in the
RegisterMetrics
function inpkg/metrics/metrics.go
. - Collect the Metric: Update the
CollectMetrics
function inpkg/metrics/collector.go
to collect the new metric.