Files
uptimesebi/internal/metrics/pools.go
T
Sebastian UnterschützandClaude Opus 5.5 e9984f8271
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 10m23s
ci / test (pull_request) Successful in 12m59s
ci / build (pull_request) Canceled after 7m17s
ci / image (pull_request) Canceled after 0s
feat(api): Prometheus metrics for operators
The API serves Prometheus metrics on a listener of its own
(UPTIME_API_METRICS_LISTEN, default :9090, "off" disables it), so the
public ingress never exposes them:

- requests by route and status, with durations except for long polls
  and agent streams
- dispatched checks, stored and rejected results per region, how late
  results arrive, live checkers and overdue checks per platform region
- notification and subscriber mail deliveries, incidents opened and
  resolved
- runs, duration and last success of the background jobs
- platform totals (orgs, users, monitors by type, status pages,
  subscribers, open incidents, pending notifications), read at most
  every 15 seconds when scraped
- the Postgres and Redis connection pools, Go runtime and build info

The Helm chart exposes the port on the API service and can add a
ServiceMonitor for the Prometheus Operator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-29 11:56:46 +01:00

82 lines
3.8 KiB
Go

package metrics
import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/prometheus/client_golang/prometheus"
"github.com/redis/go-redis/v9"
)
// DBPool reports the state of a Postgres connection pool when scraped.
func DBPool(pool *pgxpool.Pool) prometheus.Collector { return dbPool{pool} }
type dbPool struct{ pool *pgxpool.Pool }
var (
dbConns = prometheus.NewDesc(namespace+"_db_pool_connections",
"Connections of the Postgres pool by state.", []string{"state"}, nil)
dbMax = prometheus.NewDesc(namespace+"_db_pool_max_connections",
"Size limit of the Postgres pool.", nil, nil)
dbAcquires = prometheus.NewDesc(namespace+"_db_pool_acquires_total",
"Connections taken from the Postgres pool.", nil, nil)
dbEmptyAcquires = prometheus.NewDesc(namespace+"_db_pool_empty_acquires_total",
"Acquires that had to wait for or open a connection because none was idle.", nil, nil)
dbAcquireWait = prometheus.NewDesc(namespace+"_db_pool_acquire_seconds_total",
"Total time spent acquiring Postgres connections.", nil, nil)
)
// Describe implements prometheus.Collector.
func (dbPool) Describe(ch chan<- *prometheus.Desc) {
for _, d := range []*prometheus.Desc{dbConns, dbMax, dbAcquires, dbEmptyAcquires, dbAcquireWait} {
ch <- d
}
}
// Collect implements prometheus.Collector.
func (p dbPool) Collect(ch chan<- prometheus.Metric) {
s := p.pool.Stat()
ch <- prometheus.MustNewConstMetric(dbConns, prometheus.GaugeValue, float64(s.AcquiredConns()), "acquired")
ch <- prometheus.MustNewConstMetric(dbConns, prometheus.GaugeValue, float64(s.IdleConns()), "idle")
ch <- prometheus.MustNewConstMetric(dbConns, prometheus.GaugeValue, float64(s.ConstructingConns()), "constructing")
ch <- prometheus.MustNewConstMetric(dbMax, prometheus.GaugeValue, float64(s.MaxConns()))
ch <- prometheus.MustNewConstMetric(dbAcquires, prometheus.CounterValue, float64(s.AcquireCount()))
ch <- prometheus.MustNewConstMetric(dbEmptyAcquires, prometheus.CounterValue, float64(s.EmptyAcquireCount()))
ch <- prometheus.MustNewConstMetric(dbAcquireWait, prometheus.CounterValue, s.AcquireDuration().Seconds())
}
// RedisPool reports the state of a Redis client's connection pool when
// scraped.
func RedisPool(rdb redis.UniversalClient) prometheus.Collector { return redisPool{rdb} }
type redisPool struct{ rdb redis.UniversalClient }
var (
redisConns = prometheus.NewDesc(namespace+"_redis_pool_connections",
"Connections of the Redis pool by state.", []string{"state"}, nil)
redisHits = prometheus.NewDesc(namespace+"_redis_pool_hits_total",
"Times an idle Redis connection was reused.", nil, nil)
redisMisses = prometheus.NewDesc(namespace+"_redis_pool_misses_total",
"Times no idle Redis connection was available.", nil, nil)
redisTimeouts = prometheus.NewDesc(namespace+"_redis_pool_timeouts_total",
"Times waiting for a Redis connection timed out.", nil, nil)
redisStale = prometheus.NewDesc(namespace+"_redis_pool_stale_total",
"Stale Redis connections the pool closed.", nil, nil)
)
// Describe implements prometheus.Collector.
func (redisPool) Describe(ch chan<- *prometheus.Desc) {
for _, d := range []*prometheus.Desc{redisConns, redisHits, redisMisses, redisTimeouts, redisStale} {
ch <- d
}
}
// Collect implements prometheus.Collector.
func (p redisPool) Collect(ch chan<- prometheus.Metric) {
s := p.rdb.PoolStats()
ch <- prometheus.MustNewConstMetric(redisConns, prometheus.GaugeValue, float64(s.TotalConns), "total")
ch <- prometheus.MustNewConstMetric(redisConns, prometheus.GaugeValue, float64(s.IdleConns), "idle")
ch <- prometheus.MustNewConstMetric(redisHits, prometheus.CounterValue, float64(s.Hits))
ch <- prometheus.MustNewConstMetric(redisMisses, prometheus.CounterValue, float64(s.Misses))
ch <- prometheus.MustNewConstMetric(redisTimeouts, prometheus.CounterValue, float64(s.Timeouts))
ch <- prometheus.MustNewConstMetric(redisStale, prometheus.CounterValue, float64(s.StaleConns))
}