Metrics
- Go course
- Advanced
- Lesson 60/68
- 4 min
Learn the Prometheus-style metric concept.
Metrics
Metrics track a system's numeric state over time. There are three basic types: counter (only goes up: total requests), gauge (goes up/down: active connections) and histogram (distribution of values: response times).
istekSayisi.Inc()
aktifBaglanti.Set(12)
sure.Observe(0.043)Systems such as Prometheus scrape these values from a /metrics endpoint at regular intervals.
Why a histogram?
Average response time is misleading: if 99 requests take 10 ms and one takes 5 seconds, the average looks like 60 ms but one user has a terrible experience. A histogram puts durations into buckets and lets you compute percentiles like p95 and p99. The standard library's expvar offers simple counters; production usually uses the Prometheus client or OpenTelemetry.
Practice expvar counter
Create a counter with expvar.NewInt("istekler"), call Add(1) five times in a loop and print Value().
Starter code
package main
import (
"expvar"
"fmt"
)
func main() {
// buraya yaz
}Sample solution
package main
import (
"expvar"
"fmt"
)
func main() {
istekler := expvar.NewInt("istekler")
for i := 0; i < 5; i++ {
istekler.Add(1)
}
fmt.Println(istekler.Value())
}Expected output
5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which metric type tracks an in-memory queue length?
- Gauge Correct answer
- Counter
- Histogram
- Log
Explanation: Queue length can go up and down; a counter only increases. Instantaneous values are measured with a gauge.
2 Increment the counter
var istekler = expvar.NewInt("istekler")
istekler.____(1)
var istekler = expvar.NewInt("istekler")
istekler.Add(1)
Explanation: An expvar.Int counter is increased with Add; the value is published at /debug/vars.
3 Measurement and type
| Term | Meaning |
|---|---|
Total requests |
Counter |
Active connections |
Gauge |
Response-time distribution |
Histogram |
p99 |
Percentile |