Monitor pools queues and runtime health
Monitor pools queues and runtime health
Saturation occurs when demand approaches a resource limit. Monitor queue depth and capacity alongside arrival rate, processing time and rejected work. For a database pool, DB.Stats reports open, idle and in-use connections as well as cumulative waits. Runtime metrics offer Go-specific observations, including goroutine counts. A single snapshot rarely explains latency; compare trends with traffic and deployment events.
package main
import ( "fmt")
func main() { q := make(chan int, 2) q <- 1 q <- 2 accepted := false select { case q <- 3: accepted = true default: } fmt.Println(len(q), cap(q), accepted) <-q fmt.Println(len(q))}The channel is a bounded queue and select with default rejects immediately when no send can proceed. len is an instantaneous observation, not a reservation or permission to send; do not check len then assume the next send cannot block. Record rejected jobs separately from successful work.
Expected output:
2 2 false1Read a typed runtime sample
package main
import ( "fmt" "runtime/metrics")
func main() { s := []metrics.Sample{{Name: "/sched/goroutines:goroutines"}} metrics.Read(s) if s[0].Value.Kind() != metrics.KindUint64 { fmt.Println("unsupported") return } fmt.Println(s[0].Value.Uint64() > 0)}Check metric kind before reading it; unavailable names return KindBad. Print a stable invariant rather than hardcoding a goroutine count. A growing count may signal a leak, but verify stacks and workload before concluding. For cumulative pool wait counts compute differences across comparable snapshots and handle process reset.
Expected output:
trueTry it locally
Given two synthetic DBStats snapshots with WaitCount 9 and 14, print the delta. Treat a decrease as an unknown interval after reset.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Reliability and observability 06: Monitor pools queues and runtime health
Check the lesson objective before moving on.
Can len(channel) guarantee that a later send will not block?
What should a reader check before Value.Uint64?