MCPcopy
hub / github.com/prometheus/client_golang / handler

Function handler

prometheus/example_timer_complex_test.go:43–65  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

41)
42
43func handler(w http.ResponseWriter, r *http.Request) {
44 status := http.StatusOK
45 // The ObserverFunc gets called by the deferred ObserveDuration and
46 // decides which Histogram's Observe method is called.
47 timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
48 switch {
49 case status >= 500: // Server error.
50 apiRequestDuration.WithLabelValues("5xx").Observe(v)
51 case status >= 400: // Client error.
52 apiRequestDuration.WithLabelValues("4xx").Observe(v)
53 case status >= 300: // Redirection.
54 apiRequestDuration.WithLabelValues("3xx").Observe(v)
55 case status >= 200: // Success.
56 apiRequestDuration.WithLabelValues("2xx").Observe(v)
57 default: // Informational.
58 apiRequestDuration.WithLabelValues("1xx").Observe(v)
59 }
60 }))
61 defer timer.ObserveDuration()
62
63 // Handle the request. Set status accordingly.
64 // ...
65}
66
67func ExampleTimer_complex() {
68 http.HandleFunc("/api", handler)

Callers

nothing calls this directly

Calls 5

ObserveDurationMethod · 0.95
NewTimerFunction · 0.92
ObserverFuncFuncType · 0.92
ObserveMethod · 0.65
WithLabelValuesMethod · 0.65

Tested by

no test coverage detected