Choose counters gauges and histograms
Choose counters gauges and histograms
Metrics summarize many events numerically. A counter grows until reset (such as on restart); a gauge can rise and fall, for example active work. A histogram counts observations in size ranges and also tracks count and sum. Use consistent units, such as seconds for durations. These local data structures teach the model, not an exporter protocol.
package main
import ( "fmt" "sync/atomic")
func main() { var total atomic.Uint64 var active atomic.Int64 active.Add(1) total.Add(1) active.Add(-1) fmt.Println(total.Load(), active.Load())}The total records an accepted operation once while active represents current work. Use defer for paired decrements across early returns. Individual atomic loads are safe but two separate loads are not one coherent snapshot.
Expected output:
1 0Count observations within cumulative boundaries
package main
import ( "fmt")
func main() { bounds := []int{100, 500} counts := make([]int, len(bounds)) sum := 0 values := []int{50, 100, 200, 700} for _, v := range values { sum += v for i, b := range bounds { if v <= b { counts[i]++ } } } fmt.Println(counts, len(values), sum)}Here durations are integer milliseconds and buckets are cumulative and inclusive. The total count includes values above the largest boundary (the implicit +Inf bucket). Real Prometheus classic histogram durations normally use seconds. A mean from sum/count is not a percentile; bucket counts only approximate quantiles. Synchronize shared observations.
Expected output:
[2 3] 4 1050Try it locally
For observations 20, 200, 201 ms calculate count <=200, total count and sum.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Reliability and observability 02: Choose counters gauges and histograms
Check the lesson objective before moving on.
Which metric fits current queued jobs?
What does the total histogram count include?