mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 05:52:51 +00:00
# Which Problems Are Solved ZITADEL opens its Postgres connection pool via `pgxpool.NewWithConfig` (`internal/database/postgres/pg.go`) but never reads `pgxpool.Pool.Stat()`, so operators have no way to observe pool saturation (acquired/idle/total conns vs. `MaxConns`) or connection churn (acquire waits, canceled/empty acquires, idle/lifetime destroys, new connections) through ZITADEL's existing metrics pipeline. # How the Problems Are Solved - Adds `RegisterCounterObserver` (backed by an OTel `Int64ObservableCounter`) to the `Metrics` interface, its implementation, and the test mock (`backend/v3/instrumentation/metric.go`, `backend/v3/instrumentation/metrics/metric.go`, `backend/v3/instrumentation/metrics/mock.go`), mirroring the existing `RegisterValueObserver`/`RegisterUpDownSumObserver` — there was previously no observable instrument for cumulative counters. - Adds `database.RegisterPoolMetrics(ctx, pool)` (`internal/database/metrics.go`), which registers 5 observable gauges (`acquired_conns`, `constructing_conns`, `idle_conns`, `max_conns`, `total_conns`) and 7 observable counters (`acquire_count`, `acquire_duration_milliseconds`, `canceled_acquire_count`, `empty_acquire_count`, `max_idle_destroy_count`, `max_lifetime_destroy_count`, `new_conns_count`) sourced directly from `pgxpool.Pool.Stat()`, published under `zitadel.database.pool.*` metric names. - Wires the registration call into `cmd/start/start.go` right after the pool is created, so the running server process exposes these series on `/metrics`. # Additional Changes - Adds unit tests covering the new `RegisterCounterObserver` method (`backend/v3/instrumentation/metric_test.go`) and the pool metrics registration (`internal/database/metrics_test.go`). # Additional Context - Scope is intentionally limited to `cmd/start` (the long-running process that serves `/metrics`); one-shot commands (`setup`, `mirror`, `initialise`, `key`) are left untouched since an exporter running for a single command invocation wouldn't produce useful series. - https://pkg.go.dev/github.com/jackc/pgx/v5/pgxpool#Stat
100 lines
2.7 KiB
Go
100 lines
2.7 KiB
Go
package metrics
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/metric"
|
|
)
|
|
|
|
// MockMetrics implements the metrics.Metrics interface for testing
|
|
type MockMetrics struct {
|
|
mu sync.RWMutex
|
|
histogramValues map[string][]float64
|
|
counterValues map[string]int64
|
|
histogramLabels map[string][]map[string]attribute.Value
|
|
counterLabels map[string][]map[string]attribute.Value
|
|
}
|
|
|
|
var _ Metrics = new(MockMetrics)
|
|
|
|
// NewMockMetrics creates a new Metrics instance for testing
|
|
func NewMockMetrics() *MockMetrics {
|
|
return &MockMetrics{
|
|
histogramValues: make(map[string][]float64),
|
|
counterValues: make(map[string]int64),
|
|
histogramLabels: make(map[string][]map[string]attribute.Value),
|
|
counterLabels: make(map[string][]map[string]attribute.Value),
|
|
}
|
|
}
|
|
|
|
func (m *MockMetrics) GetExporter() http.Handler {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) GetMetricsProvider() metric.MeterProvider {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) RegisterCounter(name, description string) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) AddCount(ctx context.Context, name string, value int64, labels map[string]attribute.Value) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.counterValues[name] += value
|
|
m.counterLabels[name] = append(m.counterLabels[name], labels)
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) AddHistogramMeasurement(ctx context.Context, name string, value float64, labels map[string]attribute.Value) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.histogramValues[name] = append(m.histogramValues[name], value)
|
|
m.histogramLabels[name] = append(m.histogramLabels[name], labels)
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) RegisterUpDownSumObserver(name, description string, callbackFunc metric.Int64Callback) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) RegisterValueObserver(name, description string, callbackFunc metric.Int64Callback) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) RegisterCounterObserver(name, description string, callbackFunc metric.Int64Callback) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) RegisterHistogram(name, description, unit string, buckets []float64) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *MockMetrics) GetHistogramValues(name string) []float64 {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return m.histogramValues[name]
|
|
}
|
|
|
|
func (m *MockMetrics) GetHistogramLabels(name string) []map[string]attribute.Value {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return m.histogramLabels[name]
|
|
}
|
|
|
|
func (m *MockMetrics) GetCounterValue(name string) int64 {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return m.counterValues[name]
|
|
}
|
|
|
|
func (m *MockMetrics) GetCounterLabels(name string) []map[string]attribute.Value {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return m.counterLabels[name]
|
|
}
|