package metrics import ( "context" "strings" "testing" "github.com/jackc/pgx/v5/pgxpool" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestPoolCollector(t *testing.T) { // The pool connects lazily, so no database is needed to read its statistics. pool, err := pgxpool.New(context.Background(), "postgres://user:pass@127.0.0.1:1/music?pool_max_conns=7") require.NoError(t, err) t.Cleanup(pool.Close) registry := prometheus.NewRegistry() registry.MustRegister(NewPoolCollector(pool)) assert.Equal(t, 11, testutil.CollectAndCount(registry)) require.NoError(t, testutil.GatherAndCompare(registry, strings.NewReader(` # HELP musicserver_db_pool_max_conns Most connections the pool opens. # TYPE musicserver_db_pool_max_conns gauge musicserver_db_pool_max_conns 7 # HELP musicserver_db_pool_acquired_conns Connections in use. # TYPE musicserver_db_pool_acquired_conns gauge musicserver_db_pool_acquired_conns 0 `), "musicserver_db_pool_max_conns", "musicserver_db_pool_acquired_conns")) }