Skip to content

Commit aab475e

Browse files
EDsCODEclaude
andauthored
Fix control plane metrics instrumentation gaps (#384)
1. Add duckgres_hot_idle_workers gauge — hot-idle workers were invisible to metrics (not counted by observeWarmPoolLifecycleGauges) 2. Add connections_open to control plane — connectionsGauge was only incremented in standalone mode (server.go), not in the control plane's handleConnection. Export Inc/Dec helpers from server pkg. 3. Update org_sessions_active on session create/destroy — was only updated on admin API pull (AllOrgStats), not on every connection. Now incremented after CreateSession and decremented in defer after DestroySession. 4. Add observeOrgSessionsActive stub for non-kubernetes builds. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 4567e39 commit aab475e

4 files changed

Lines changed: 32 additions & 2 deletions

File tree

controlplane/control.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ func sessionCreationErrorResponse(err error) (code string, message string) {
603603
func (cp *ControlPlane) handleConnection(conn net.Conn) {
604604
remoteAddr := conn.RemoteAddr()
605605
slog.Info("Connection accepted.", "remote_addr", remoteAddr)
606+
server.IncrementOpenConnections()
607+
defer server.DecrementOpenConnections()
606608

607609
releaseRateLimit, msg := server.BeginRateLimitedAuthAttempt(cp.rateLimiter, remoteAddr)
608610
if msg != "" {
@@ -864,7 +866,15 @@ func (cp *ControlPlane) handleConnection(conn net.Conn) {
864866
_ = writer.Flush()
865867
return
866868
}
867-
defer sessions.DestroySession(pid)
869+
if orgID != "" {
870+
observeOrgSessionsActive(orgID, sessions.SessionCount())
871+
}
872+
defer func() {
873+
sessions.DestroySession(pid)
874+
if orgID != "" {
875+
observeOrgSessionsActive(orgID, sessions.SessionCount())
876+
}
877+
}()
868878

869879
// Register the TCP connection so OnWorkerCrash can close it to unblock
870880
// the message loop if the backing worker dies.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:build !kubernetes
2+
3+
package controlplane
4+
5+
func observeOrgSessionsActive(string, int) {}

controlplane/warm_pool_metrics.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ var hotWorkersGauge = promauto.NewGauge(prometheus.GaugeOpts{
3131
Help: "Number of activated, tenant-bound workers serving sessions",
3232
})
3333

34+
var hotIdleWorkersGauge = promauto.NewGauge(prometheus.GaugeOpts{
35+
Name: "duckgres_hot_idle_workers",
36+
Help: "Number of activated workers retaining org assignment between sessions",
37+
})
38+
3439
var drainingWorkersGauge = promauto.NewGauge(prometheus.GaugeOpts{
3540
Name: "duckgres_draining_workers",
3641
Help: "Number of workers currently draining sessions before retirement",
@@ -76,7 +81,7 @@ const (
7681
// observeWarmPoolLifecycleGauges recalculates all lifecycle gauges from the
7782
// current worker map. Must be called with p.mu held (at least RLock).
7883
func observeWarmPoolLifecycleGauges(workers map[int]*ManagedWorker) {
79-
var idle, reserved, activating, hot, draining int
84+
var idle, reserved, activating, hot, hotIdle, draining int
8085
for _, w := range workers {
8186
select {
8287
case <-w.done:
@@ -92,6 +97,8 @@ func observeWarmPoolLifecycleGauges(workers map[int]*ManagedWorker) {
9297
activating++
9398
case WorkerLifecycleHot:
9499
hot++
100+
case WorkerLifecycleHotIdle:
101+
hotIdle++
95102
case WorkerLifecycleDraining:
96103
draining++
97104
}
@@ -100,6 +107,7 @@ func observeWarmPoolLifecycleGauges(workers map[int]*ManagedWorker) {
100107
reservedWorkersGauge.Set(float64(reserved))
101108
activatingWorkersGauge.Set(float64(activating))
102109
hotWorkersGauge.Set(float64(hot))
110+
hotIdleWorkersGauge.Set(float64(hotIdle))
103111
drainingWorkersGauge.Set(float64(draining))
104112
}
105113

server/server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ var connectionsGauge = promauto.NewGauge(prometheus.GaugeOpts{
5353
Help: "Number of currently open client connections",
5454
})
5555

56+
// IncrementOpenConnections increments the open connections gauge.
57+
// Used by the control plane which handles connections separately from the standalone server.
58+
func IncrementOpenConnections() { connectionsGauge.Inc() }
59+
60+
// DecrementOpenConnections decrements the open connections gauge.
61+
func DecrementOpenConnections() { connectionsGauge.Dec() }
62+
5663
var queryDurationHistogram = promauto.NewHistogram(prometheus.HistogramOpts{
5764
Name: "duckgres_query_duration_seconds",
5865
Help: "Query execution duration in seconds",

0 commit comments

Comments
 (0)