| 1 |
// Copyright 2017 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 promhttp |
| 15 |
|
| 16 |
import ( |
| 17 |
"crypto/tls" |
| 18 |
"net/http" |
| 19 |
"net/http/httptrace" |
| 20 |
"time" |
| 21 |
|
| 22 |
"github.com/prometheus/client_golang/prometheus" |
| 23 |
) |
| 24 |
|
| 25 |
// The RoundTripperFunc type is an adapter to allow the use of ordinary |
| 26 |
// functions as RoundTrippers. If f is a function with the appropriate |
| 27 |
// signature, RountTripperFunc(f) is a RoundTripper that calls f. |
| 28 |
type RoundTripperFunc func(req *http.Request) (*http.Response, error) |
| 29 |
|
| 30 |
// RoundTrip implements the RoundTripper interface. |
| 31 |
func (rt RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { |
| 32 |
return rt(r) |
| 33 |
} |
| 34 |
|
| 35 |
// InstrumentRoundTripperInFlight is a middleware that wraps the provided |
| 36 |
// http.RoundTripper. It sets the provided prometheus.Gauge to the number of |
| 37 |
// requests currently handled by the wrapped http.RoundTripper. |
| 38 |
// |
| 39 |
// See the example for ExampleInstrumentRoundTripperDuration for example usage. |
| 40 |
func InstrumentRoundTripperInFlight(gauge prometheus.Gauge, next http.RoundTripper) RoundTripperFunc { |
| 41 |
return func(r *http.Request) (*http.Response, error) { |
| 42 |
gauge.Inc() |
| 43 |
defer gauge.Dec() |
| 44 |
return next.RoundTrip(r) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// InstrumentRoundTripperCounter is a middleware that wraps the provided |
| 49 |
// http.RoundTripper to observe the request result with the provided CounterVec. |
| 50 |
// The CounterVec must have zero, one, or two non-const non-curried labels. For |
| 51 |
// those, the only allowed label names are "code" and "method". The function |
| 52 |
// panics otherwise. For the "method" label a predefined default label value set |
| 53 |
// is used to filter given values. Values besides predefined values will count |
| 54 |
// as `unknown` method.`WithExtraMethods` can be used to add more |
| 55 |
// methods to the set. Partitioning of the CounterVec happens by HTTP status code |
| 56 |
// and/or HTTP method if the respective instance label names are present in the |
| 57 |
// CounterVec. For unpartitioned counting, use a CounterVec with zero labels. |
| 58 |
// |
| 59 |
// If the wrapped RoundTripper panics or returns a non-nil error, the Counter |
| 60 |
// is not incremented. |
| 61 |
// |
| 62 |
// Use with WithExemplarFromContext to instrument the exemplars on the counter of requests. |
| 63 |
// |
| 64 |
// See the example for ExampleInstrumentRoundTripperDuration for example usage. |
| 65 |
func InstrumentRoundTripperCounter(counter *prometheus.CounterVec, next http.RoundTripper, opts ...Option) RoundTripperFunc { |
| 66 |
rtOpts := defaultOptions() |
| 67 |
for _, o := range opts { |
| 68 |
o.apply(rtOpts) |
| 69 |
} |
| 70 |
|
| 71 |
// Curry the counter with dynamic labels before checking the remaining labels. |
| 72 |
code, method := checkLabels(counter.MustCurryWith(rtOpts.emptyDynamicLabels())) |
| 73 |
|
| 74 |
return func(r *http.Request) (*http.Response, error) { |
| 75 |
resp, err := next.RoundTrip(r) |
| 76 |
if err == nil { |
| 77 |
l := labels(code, method, r.Method, resp.StatusCode, rtOpts.extraMethods...) |
| 78 |
for label, resolve := range rtOpts.extraLabelsFromCtx { |
| 79 |
l[label] = resolve(resp.Request.Context()) |
| 80 |
} |
| 81 |
addWithExemplar(counter.With(l), 1, rtOpts.getExemplarFn(r.Context())) |
| 82 |
} |
| 83 |
return resp, err |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// InstrumentRoundTripperDuration is a middleware that wraps the provided |
| 88 |
// http.RoundTripper to observe the request duration with the provided |
| 89 |
// ObserverVec. The ObserverVec must have zero, one, or two non-const |
| 90 |
// non-curried labels. For those, the only allowed label names are "code" and |
| 91 |
// "method". The function panics otherwise. For the "method" label a predefined |
| 92 |
// default label value set is used to filter given values. Values besides |
| 93 |
// predefined values will count as `unknown` method. `WithExtraMethods` |
| 94 |
// can be used to add more methods to the set. The Observe method of the Observer |
| 95 |
// in the ObserverVec is called with the request duration in |
| 96 |
// seconds. Partitioning happens by HTTP status code and/or HTTP method if the |
| 97 |
// respective instance label names are present in the ObserverVec. For |
| 98 |
// unpartitioned observations, use an ObserverVec with zero labels. Note that |
| 99 |
// partitioning of Histograms is expensive and should be used judiciously. |
| 100 |
// |
| 101 |
// If the wrapped RoundTripper panics or returns a non-nil error, no values are |
| 102 |
// reported. |
| 103 |
// |
| 104 |
// Use with WithExemplarFromContext to instrument the exemplars on the duration histograms. |
| 105 |
// |
| 106 |
// Note that this method is only guaranteed to never observe negative durations |
| 107 |
// if used with Go1.9+. |
| 108 |
func InstrumentRoundTripperDuration(obs prometheus.ObserverVec, next http.RoundTripper, opts ...Option) RoundTripperFunc { |
| 109 |
rtOpts := defaultOptions() |
| 110 |
for _, o := range opts { |
| 111 |
o.apply(rtOpts) |
| 112 |
} |
| 113 |
|
| 114 |
// Curry the observer with dynamic labels before checking the remaining labels. |
| 115 |
code, method := checkLabels(obs.MustCurryWith(rtOpts.emptyDynamicLabels())) |
| 116 |
|
| 117 |
return func(r *http.Request) (*http.Response, error) { |
| 118 |
start := time.Now() |
| 119 |
resp, err := next.RoundTrip(r) |
| 120 |
if err == nil { |
| 121 |
l := labels(code, method, r.Method, resp.StatusCode, rtOpts.extraMethods...) |
| 122 |
for label, resolve := range rtOpts.extraLabelsFromCtx { |
| 123 |
l[label] = resolve(resp.Request.Context()) |
| 124 |
} |
| 125 |
observeWithExemplar(obs.With(l), time.Since(start).Seconds(), rtOpts.getExemplarFn(r.Context())) |
| 126 |
} |
| 127 |
return resp, err |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
// InstrumentTrace is used to offer flexibility in instrumenting the available |
| 132 |
// httptrace.ClientTrace hook functions. Each function is passed a float64 |
| 133 |
// representing the time in seconds since the start of the http request. A user |
| 134 |
// may choose to use separately buckets Histograms, or implement custom |
| 135 |
// instance labels on a per function basis. |
| 136 |
type InstrumentTrace struct { |
| 137 |
GotConn func(float64) |
| 138 |
PutIdleConn func(float64) |
| 139 |
GotFirstResponseByte func(float64) |
| 140 |
Got100Continue func(float64) |
| 141 |
DNSStart func(float64) |
| 142 |
DNSDone func(float64) |
| 143 |
ConnectStart func(float64) |
| 144 |
ConnectDone func(float64) |
| 145 |
TLSHandshakeStart func(float64) |
| 146 |
TLSHandshakeDone func(float64) |
| 147 |
WroteHeaders func(float64) |
| 148 |
Wait100Continue func(float64) |
| 149 |
WroteRequest func(float64) |
| 150 |
} |
| 151 |
|
| 152 |
// InstrumentRoundTripperTrace is a middleware that wraps the provided |
| 153 |
// RoundTripper and reports times to hook functions provided in the |
| 154 |
// InstrumentTrace struct. Hook functions that are not present in the provided |
| 155 |
// InstrumentTrace struct are ignored. Times reported to the hook functions are |
| 156 |
// time since the start of the request. Only with Go1.9+, those times are |
| 157 |
// guaranteed to never be negative. (Earlier Go versions are not using a |
| 158 |
// monotonic clock.) Note that partitioning of Histograms is expensive and |
| 159 |
// should be used judiciously. |
| 160 |
// |
| 161 |
// For hook functions that receive an error as an argument, no observations are |
| 162 |
// made in the event of a non-nil error value. |
| 163 |
// |
| 164 |
// See the example for ExampleInstrumentRoundTripperDuration for example usage. |
| 165 |
func InstrumentRoundTripperTrace(it *InstrumentTrace, next http.RoundTripper) RoundTripperFunc { |
| 166 |
return func(r *http.Request) (*http.Response, error) { |
| 167 |
start := time.Now() |
| 168 |
|
| 169 |
trace := &httptrace.ClientTrace{ |
| 170 |
GotConn: func(_ httptrace.GotConnInfo) { |
| 171 |
if it.GotConn != nil { |
| 172 |
it.GotConn(time.Since(start).Seconds()) |
| 173 |
} |
| 174 |
}, |
| 175 |
PutIdleConn: func(err error) { |
| 176 |
if err != nil { |
| 177 |
return |
| 178 |
} |
| 179 |
if it.PutIdleConn != nil { |
| 180 |
it.PutIdleConn(time.Since(start).Seconds()) |
| 181 |
} |
| 182 |
}, |
| 183 |
DNSStart: func(_ httptrace.DNSStartInfo) { |
| 184 |
if it.DNSStart != nil { |
| 185 |
it.DNSStart(time.Since(start).Seconds()) |
| 186 |
} |
| 187 |
}, |
| 188 |
DNSDone: func(_ httptrace.DNSDoneInfo) { |
| 189 |
if it.DNSDone != nil { |
| 190 |
it.DNSDone(time.Since(start).Seconds()) |
| 191 |
} |
| 192 |
}, |
| 193 |
ConnectStart: func(_, _ string) { |
| 194 |
if it.ConnectStart != nil { |
| 195 |
it.ConnectStart(time.Since(start).Seconds()) |
| 196 |
} |
| 197 |
}, |
| 198 |
ConnectDone: func(_, _ string, err error) { |
| 199 |
if err != nil { |
| 200 |
return |
| 201 |
} |
| 202 |
if it.ConnectDone != nil { |
| 203 |
it.ConnectDone(time.Since(start).Seconds()) |
| 204 |
} |
| 205 |
}, |
| 206 |
GotFirstResponseByte: func() { |
| 207 |
if it.GotFirstResponseByte != nil { |
| 208 |
it.GotFirstResponseByte(time.Since(start).Seconds()) |
| 209 |
} |
| 210 |
}, |
| 211 |
Got100Continue: func() { |
| 212 |
if it.Got100Continue != nil { |
| 213 |
it.Got100Continue(time.Since(start).Seconds()) |
| 214 |
} |
| 215 |
}, |
| 216 |
TLSHandshakeStart: func() { |
| 217 |
if it.TLSHandshakeStart != nil { |
| 218 |
it.TLSHandshakeStart(time.Since(start).Seconds()) |
| 219 |
} |
| 220 |
}, |
| 221 |
TLSHandshakeDone: func(_ tls.ConnectionState, err error) { |
| 222 |
if err != nil { |
| 223 |
return |
| 224 |
} |
| 225 |
if it.TLSHandshakeDone != nil { |
| 226 |
it.TLSHandshakeDone(time.Since(start).Seconds()) |
| 227 |
} |
| 228 |
}, |
| 229 |
WroteHeaders: func() { |
| 230 |
if it.WroteHeaders != nil { |
| 231 |
it.WroteHeaders(time.Since(start).Seconds()) |
| 232 |
} |
| 233 |
}, |
| 234 |
Wait100Continue: func() { |
| 235 |
if it.Wait100Continue != nil { |
| 236 |
it.Wait100Continue(time.Since(start).Seconds()) |
| 237 |
} |
| 238 |
}, |
| 239 |
WroteRequest: func(_ httptrace.WroteRequestInfo) { |
| 240 |
if it.WroteRequest != nil { |
| 241 |
it.WroteRequest(time.Since(start).Seconds()) |
| 242 |
} |
| 243 |
}, |
| 244 |
} |
| 245 |
r = r.WithContext(httptrace.WithClientTrace(r.Context(), trace)) |
| 246 |
|
| 247 |
return next.RoundTrip(r) |
| 248 |
} |
| 249 |
} |