(w http.ResponseWriter, r *http.Request)
| 41 | ) |
| 42 | |
| 43 | func 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 | |
| 67 | func ExampleTimer_complex() { |
| 68 | http.HandleFunc("/api", handler) |
nothing calls this directly
no test coverage detected