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

Function InstrumentMetricHandler

prometheus/promhttp/http.go:286–321  ·  view source on GitHub ↗

InstrumentMetricHandler is usually used with an http.Handler returned by the HandlerFor function. It instruments the provided http.Handler with two metrics: A counter vector "promhttp_metric_handler_requests_total" to count scrapes partitioned by HTTP status code, and a gauge "promhttp_metric_handle

(reg prometheus.Registerer, handler http.Handler)

Source from the content-addressed store, hash-verified

284// "scrape_duration_seconds" gauge created by the Prometheus server upon each
285// scrape.
286func InstrumentMetricHandler(reg prometheus.Registerer, handler http.Handler) http.Handler {
287 cnt := prometheus.NewCounterVec(
288 prometheus.CounterOpts{
289 Name: "promhttp_metric_handler_requests_total",
290 Help: "Total number of scrapes by HTTP status code.",
291 },
292 []string{"code"},
293 )
294 // Initialize the most likely HTTP status codes.
295 cnt.WithLabelValues("200")
296 cnt.WithLabelValues("500")
297 cnt.WithLabelValues("503")
298 if err := reg.Register(cnt); err != nil {
299 are := &prometheus.AlreadyRegisteredError{}
300 if errors.As(err, are) {
301 cnt = are.ExistingCollector.(*prometheus.CounterVec)
302 } else {
303 panic(err)
304 }
305 }
306
307 gge := prometheus.NewGauge(prometheus.GaugeOpts{
308 Name: "promhttp_metric_handler_requests_in_flight",
309 Help: "Current number of scrapes being served.",
310 })
311 if err := reg.Register(gge); err != nil {
312 are := &prometheus.AlreadyRegisteredError{}
313 if errors.As(err, are) {
314 gge = are.ExistingCollector.(prometheus.Gauge)
315 } else {
316 panic(err)
317 }
318 }
319
320 return InstrumentHandlerCounter(cnt, InstrumentHandlerInFlight(gge, handler))
321}
322
323// HandlerErrorHandling defines how a Handler serving metrics will handle
324// errors.

Callers 3

HandlerFunction · 0.85

Calls 6

WithLabelValuesMethod · 0.95
NewCounterVecFunction · 0.92
NewGaugeFunction · 0.92
InstrumentHandlerCounterFunction · 0.85
RegisterMethod · 0.65