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

Function instrumentHandlerFunc

tutorials/whatsup/reference/main.go:228–281  ·  view source on GitHub ↗
(tracer *tracing.Tracer, reg prometheus.Registerer, handlerName string, handler http.Handler)

Source from the content-addressed store, hash-verified

226}
227
228func instrumentHandlerFunc(tracer *tracing.Tracer, reg prometheus.Registerer, handlerName string, handler http.Handler) (string, http.HandlerFunc) {
229 reg = prometheus.WrapRegistererWith(prometheus.Labels{"handler": handlerName}, reg)
230
231 requestDuration := promauto.With(reg).NewHistogramVec(
232 prometheus.HistogramOpts{
233 Name: "http_request_duration_seconds",
234 Help: "Tracks the latencies for HTTP requests.",
235 Buckets: []float64{0.1, 0.3, 0.6, 1, 3, 6, 9, 20},
236 },
237 []string{"method", "code"},
238 )
239 requestSize := promauto.With(reg).NewSummaryVec(
240 prometheus.SummaryOpts{
241 Name: "http_request_size_bytes",
242 Help: "Tracks the size of HTTP requests.",
243 },
244 []string{"method", "code"},
245 )
246 requestsTotal := promauto.With(reg).NewCounterVec(
247 prometheus.CounterOpts{
248 Name: "http_requests_total",
249 Help: "Tracks the number of HTTP requests.",
250 }, []string{"method", "code"},
251 )
252 responseSize := promauto.With(reg).NewSummaryVec(
253 prometheus.SummaryOpts{
254 Name: "http_response_size_bytes",
255 Help: "Tracks the size of HTTP responses.",
256 },
257 []string{"method", "code"},
258 )
259
260 base := promhttp.InstrumentHandlerRequestSize(
261 requestSize,
262 promhttp.InstrumentHandlerCounter(
263 requestsTotal,
264 promhttp.InstrumentHandlerResponseSize(
265 responseSize,
266 promhttp.InstrumentHandlerDuration(
267 requestDuration,
268 http.HandlerFunc(func(writer http.ResponseWriter, r *http.Request) {
269 handler.ServeHTTP(writer, r)
270 }),
271 promhttp.WithExemplarFromContext(getExemplarFn),
272 ),
273 ),
274 promhttp.WithExemplarFromContext(getExemplarFn),
275 ),
276 )
277
278 // Wrap with tracing. This will be visited as a first middleware.
279 base = tracinghttp.NewMiddleware(tracer).WrapHandler(handlerName, base)
280 return handlerName, base.ServeHTTP
281}
282
283func instrumentRoundTripper(reg prometheus.Registerer, clientName string, rt http.RoundTripper) http.RoundTripper {
284 reg = prometheus.WrapRegistererWith(prometheus.Labels{"client": clientName}, reg)

Callers 1

runMainFunction · 0.70

Calls 12

WrapRegistererWithFunction · 0.92
WithFunction · 0.92
InstrumentHandlerCounterFunction · 0.92
WithExemplarFromContextFunction · 0.92
WrapHandlerMethod · 0.65
NewHistogramVecMethod · 0.45
NewSummaryVecMethod · 0.45
NewCounterVecMethod · 0.45
ServeHTTPMethod · 0.45

Tested by

no test coverage detected