gitlab.com/gitlab-org/labkit@v1.21.0/metrics/http_round_tripper/http_round_tripper.go (about) 1 package http_round_tripper 2 3 import ( 4 "net/http" 5 6 "github.com/prometheus/client_golang/prometheus" 7 "github.com/prometheus/client_golang/prometheus/promhttp" 8 ) 9 10 // Metric names for the recorded metrics. 11 // These are the conventional names prometheus uses for these metrics. 12 const ( 13 inFlightRequestsMetricName = "in_flight_requests" 14 requestsTotalMetricName = "requests_total" 15 requestDurationSecondsMetricName = "request_duration_seconds" 16 ) 17 18 // Factory creates middleware instances. Created by NewFactory. 19 type Factory func(next http.RoundTripper, opts ...Option) http.RoundTripper 20 21 // NewFactory will create a function for creating metric middlewares. 22 // The resulting function can be called multiple times to obtain multiple middleware 23 // instances. 24 // Each instance can be configured with different options that will be applied to the 25 // same underlying metrics. 26 func NewFactory(opts ...FactoryOption) Factory { 27 config := applyFactoryOptions(opts) 28 29 inFlightRequests := prometheus.NewGauge(prometheus.GaugeOpts{ 30 Namespace: config.namespace, 31 Subsystem: config.subsystem, 32 Name: inFlightRequestsMetricName, 33 Help: "A gauge of requests currently being handled.", 34 }) 35 36 requestsTotal := prometheus.NewCounterVec( 37 prometheus.CounterOpts{ 38 Namespace: config.namespace, 39 Subsystem: config.subsystem, 40 Name: requestsTotalMetricName, 41 Help: "A counter for total number of requests.", 42 }, 43 config.labels, 44 ) 45 46 requestDurationSeconds := prometheus.NewHistogramVec( 47 prometheus.HistogramOpts{ 48 Namespace: config.namespace, 49 Subsystem: config.subsystem, 50 Name: requestDurationSecondsMetricName, 51 Help: "A histogram of latencies for requests.", 52 Buckets: config.requestDurationBuckets, 53 }, 54 config.labels, 55 ) 56 57 prometheus.MustRegister(inFlightRequests, requestsTotal, requestDurationSeconds) 58 59 return func(next http.RoundTripper, opts ...Option) http.RoundTripper { 60 config := applyOptions(opts) 61 62 rt := next 63 64 rt = promhttp.InstrumentRoundTripperCounter(requestsTotal.MustCurryWith(config.labelValues), rt) 65 rt = promhttp.InstrumentRoundTripperDuration(requestDurationSeconds.MustCurryWith(config.labelValues), rt) 66 rt = promhttp.InstrumentRoundTripperInFlight(inFlightRequests, rt) 67 68 return rt 69 } 70 }