Metrics are served on METRICS_PORT (default 2112, "off" to disable), never on the public port. Besides the Go runtime and process metrics this adds: - http_requests_total and http_request_duration_seconds per route pattern, from an Echo middleware. - musicserver_songs_requested_total and musicserver_songs_played_total by pick mode (random, low, classic). The mode is now stored with each queued song (migration 000006, not released yet). - Library size, sync duration, last successful sync, broken songs from the last full sync, and the database connection pool. Refs #35, #36, #37 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
34 lines
1.1 KiB
Go
34 lines
1.1 KiB
Go
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"))
|
|
}
|