/pgxpoolprometheus

A Prometheus Collector to gather stats from pgxpool.Stat

Primary LanguageGoApache License 2.0Apache-2.0

Build Status Release GitHub go.mod Go version License

Prometheus Collector for PGX Pool

This is a Prometheus Collector for PGX Pool.

Example Usage

package main

import (
	"context"
	"log"
	"net/http"
	"os"

	"github.com/jackc/pgx/v5/pgxpool"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"

	"github.com/IBM/pgxpoolprometheus"
)

func main() {
	pool, err := pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Fatal(err)
	}

	collector := pgxpoolprometheus.NewCollector(pool, map[string]string{"db_name": "my_db"})
	prometheus.MustRegister(collector)

	http.Handle("/metrics", promhttp.Handler())
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Metrics Collected

This collector provides metrics for all the stats produced by pgxpool.Stat all prefixed with pgxpool:

Name Description
pgxpool_acquire_count Cumulative count of successful acquires from the pool.
pgxpool_acquire_duration_ns Total duration of all successful acquires from the pool in nanoseconds.
pgxpool_acquired_conns Number of currently acquired connections in the pool.
pgxpool_canceled_acquire_count Cumulative count of acquires from the pool that were canceled by a context.
pgxpool_constructing_conns Number of conns with construction in progress in the pool.
pgxpool_empty_acquire Cumulative count of successful acquires from the pool that waited for a resource to be released or constructed because the pool was empty.
pgxpool_idle_conns Number of currently idle conns in the pool.
pgxpool_max_conns Maximum size of the pool.
pgxpool_total_conns Total number of resources currently in the pool. The value is the sum of ConstructingConns, AcquiredConns, and IdleConns.