| 1 |
// Copyright 2022 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package httpmiddleware is adapted from |
| 15 |
// https://github.com/bwplotka/correlator/tree/main/examples/observability/ping/pkg/httpinstrumentation |
| 16 |
package httpmiddleware |
| 17 |
|
| 18 |
import ( |
| 19 |
"net/http" |
| 20 |
|
| 21 |
"github.com/prometheus/client_golang/prometheus" |
| 22 |
"github.com/prometheus/client_golang/prometheus/promauto" |
| 23 |
"github.com/prometheus/client_golang/prometheus/promhttp" |
| 24 |
) |
| 25 |
|
| 26 |
type Middleware interface { |
| 27 |
// WrapHandler wraps the given HTTP handler for instrumentation. |
| 28 |
WrapHandler(handlerName string, handler http.Handler) http.HandlerFunc |
| 29 |
} |
| 30 |
|
| 31 |
type middleware struct { |
| 32 |
buckets []float64 |
| 33 |
registry prometheus.Registerer |
| 34 |
} |
| 35 |
|
| 36 |
// WrapHandler wraps the given HTTP handler for instrumentation: |
| 37 |
// It registers four metric collectors (if not already done) and reports HTTP |
| 38 |
// metrics to the (newly or already) registered collectors. |
| 39 |
// Each has a constant label named "handler" with the provided handlerName as |
| 40 |
// value. |
| 41 |
func (m *middleware) WrapHandler(handlerName string, handler http.Handler) http.HandlerFunc { |
| 42 |
reg := prometheus.WrapRegistererWith(prometheus.Labels{"handler": handlerName}, m.registry) |
| 43 |
|
| 44 |
requestsTotal := promauto.With(reg).NewCounterVec( |
| 45 |
prometheus.CounterOpts{ |
| 46 |
Name: "http_requests_total", |
| 47 |
Help: "Tracks the number of HTTP requests.", |
| 48 |
}, []string{"method", "code"}, |
| 49 |
) |
| 50 |
requestDuration := promauto.With(reg).NewHistogramVec( |
| 51 |
prometheus.HistogramOpts{ |
| 52 |
Name: "http_request_duration_seconds", |
| 53 |
Help: "Tracks the latencies for HTTP requests.", |
| 54 |
Buckets: m.buckets, |
| 55 |
}, |
| 56 |
[]string{"method", "code"}, |
| 57 |
) |
| 58 |
requestSize := promauto.With(reg).NewSummaryVec( |
| 59 |
prometheus.SummaryOpts{ |
| 60 |
Name: "http_request_size_bytes", |
| 61 |
Help: "Tracks the size of HTTP requests.", |
| 62 |
}, |
| 63 |
[]string{"method", "code"}, |
| 64 |
) |
| 65 |
responseSize := promauto.With(reg).NewSummaryVec( |
| 66 |
prometheus.SummaryOpts{ |
| 67 |
Name: "http_response_size_bytes", |
| 68 |
Help: "Tracks the size of HTTP responses.", |
| 69 |
}, |
| 70 |
[]string{"method", "code"}, |
| 71 |
) |
| 72 |
|
| 73 |
// Wraps the provided http.Handler to observe the request result with the provided metrics. |
| 74 |
base := promhttp.InstrumentHandlerCounter( |
| 75 |
requestsTotal, |
| 76 |
promhttp.InstrumentHandlerDuration( |
| 77 |
requestDuration, |
| 78 |
promhttp.InstrumentHandlerRequestSize( |
| 79 |
requestSize, |
| 80 |
promhttp.InstrumentHandlerResponseSize( |
| 81 |
responseSize, |
| 82 |
handler, |
| 83 |
), |
| 84 |
), |
| 85 |
), |
| 86 |
) |
| 87 |
|
| 88 |
return base.ServeHTTP |
| 89 |
} |
| 90 |
|
| 91 |
// New returns a Middleware interface. |
| 92 |
func New(registry prometheus.Registerer, buckets []float64) Middleware { |
| 93 |
if buckets == nil { |
| 94 |
buckets = prometheus.ExponentialBuckets(0.1, 1.5, 5) |
| 95 |
} |
| 96 |
|
| 97 |
return &middleware{ |
| 98 |
buckets: buckets, |
| 99 |
registry: registry, |
| 100 |
} |
| 101 |
} |