(tracer *tracing.Tracer, reg prometheus.Registerer, handlerName string, handler http.Handler)
| 226 | } |
| 227 | |
| 228 | func 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 | |
| 283 | func instrumentRoundTripper(reg prometheus.Registerer, clientName string, rt http.RoundTripper) http.RoundTripper { |
| 284 | reg = prometheus.WrapRegistererWith(prometheus.Labels{"client": clientName}, reg) |
no test coverage detected