2017-05-09 19:46:09 +03:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package promhttp
|
|
|
|
|
|
|
|
import (
|
2019-04-29 00:26:36 +03:00
|
|
|
"crypto/tls"
|
2017-05-09 19:46:09 +03:00
|
|
|
"net/http"
|
2019-04-29 00:26:36 +03:00
|
|
|
"net/http/httptrace"
|
2017-05-09 19:46:09 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// The RoundTripperFunc type is an adapter to allow the use of ordinary
|
|
|
|
// functions as RoundTrippers. If f is a function with the appropriate
|
|
|
|
// signature, RountTripperFunc(f) is a RoundTripper that calls f.
|
|
|
|
type RoundTripperFunc func(req *http.Request) (*http.Response, error)
|
|
|
|
|
|
|
|
// RoundTrip implements the RoundTripper interface.
|
|
|
|
func (rt RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
|
|
return rt(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstrumentRoundTripperInFlight is a middleware that wraps the provided
|
|
|
|
// http.RoundTripper. It sets the provided prometheus.Gauge to the number of
|
|
|
|
// requests currently handled by the wrapped http.RoundTripper.
|
|
|
|
//
|
|
|
|
// See the example for ExampleInstrumentRoundTripperDuration for example usage.
|
|
|
|
func InstrumentRoundTripperInFlight(gauge prometheus.Gauge, next http.RoundTripper) RoundTripperFunc {
|
2022-08-02 20:33:08 +03:00
|
|
|
return func(r *http.Request) (*http.Response, error) {
|
2017-05-09 19:46:09 +03:00
|
|
|
gauge.Inc()
|
|
|
|
defer gauge.Dec()
|
|
|
|
return next.RoundTrip(r)
|
2022-08-02 20:33:08 +03:00
|
|
|
}
|
2017-05-09 19:46:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// InstrumentRoundTripperCounter is a middleware that wraps the provided
|
|
|
|
// http.RoundTripper to observe the request result with the provided CounterVec.
|
2017-12-23 03:06:17 +03:00
|
|
|
// The CounterVec must have zero, one, or two non-const non-curried labels. For
|
|
|
|
// those, the only allowed label names are "code" and "method". The function
|
2022-01-18 12:19:28 +03:00
|
|
|
// panics otherwise. For the "method" label a predefined default label value set
|
|
|
|
// is used to filter given values. Values besides predefined values will count
|
|
|
|
// as `unknown` method.`WithExtraMethods` can be used to add more
|
|
|
|
// methods to the set. Partitioning of the CounterVec happens by HTTP status code
|
2017-12-23 03:06:17 +03:00
|
|
|
// and/or HTTP method if the respective instance label names are present in the
|
|
|
|
// CounterVec. For unpartitioned counting, use a CounterVec with zero labels.
|
2017-05-09 19:46:09 +03:00
|
|
|
//
|
|
|
|
// If the wrapped RoundTripper panics or returns a non-nil error, the Counter
|
|
|
|
// is not incremented.
|
|
|
|
//
|
2022-08-02 20:33:08 +03:00
|
|
|
// Use with WithExemplarFromContext to instrument the exemplars on the counter of requests.
|
|
|
|
//
|
2017-05-09 19:46:09 +03:00
|
|
|
// See the example for ExampleInstrumentRoundTripperDuration for example usage.
|
2022-01-18 12:19:28 +03:00
|
|
|
func InstrumentRoundTripperCounter(counter *prometheus.CounterVec, next http.RoundTripper, opts ...Option) RoundTripperFunc {
|
2022-08-02 20:33:08 +03:00
|
|
|
rtOpts := defaultOptions()
|
2022-01-18 12:19:28 +03:00
|
|
|
for _, o := range opts {
|
2022-08-02 20:33:08 +03:00
|
|
|
o.apply(rtOpts)
|
2022-01-18 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
2017-05-09 19:46:09 +03:00
|
|
|
code, method := checkLabels(counter)
|
|
|
|
|
2022-08-02 20:33:08 +03:00
|
|
|
return func(r *http.Request) (*http.Response, error) {
|
2017-05-09 19:46:09 +03:00
|
|
|
resp, err := next.RoundTrip(r)
|
|
|
|
if err == nil {
|
2022-08-02 20:33:08 +03:00
|
|
|
exemplarAdd(
|
|
|
|
counter.With(labels(code, method, r.Method, resp.StatusCode, rtOpts.extraMethods...)),
|
|
|
|
1,
|
|
|
|
rtOpts.getExemplarFn(r.Context()),
|
|
|
|
)
|
2022-01-18 12:19:28 +03:00
|
|
|
counter.With(labels(code, method, r.Method, resp.StatusCode, rtOpts.extraMethods...)).Inc()
|
2017-05-09 19:46:09 +03:00
|
|
|
}
|
|
|
|
return resp, err
|
2022-08-02 20:33:08 +03:00
|
|
|
}
|
2017-05-09 19:46:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// InstrumentRoundTripperDuration is a middleware that wraps the provided
|
2018-01-11 18:59:30 +03:00
|
|
|
// http.RoundTripper to observe the request duration with the provided
|
|
|
|
// ObserverVec. The ObserverVec must have zero, one, or two non-const
|
|
|
|
// non-curried labels. For those, the only allowed label names are "code" and
|
2022-01-18 12:19:28 +03:00
|
|
|
// "method". The function panics otherwise. For the "method" label a predefined
|
|
|
|
// default label value set is used to filter given values. Values besides
|
|
|
|
// predefined values will count as `unknown` method. `WithExtraMethods`
|
|
|
|
// can be used to add more methods to the set. The Observe method of the Observer
|
2018-01-11 18:59:30 +03:00
|
|
|
// in the ObserverVec is called with the request duration in
|
|
|
|
// seconds. Partitioning happens by HTTP status code and/or HTTP method if the
|
|
|
|
// respective instance label names are present in the ObserverVec. For
|
|
|
|
// unpartitioned observations, use an ObserverVec with zero labels. Note that
|
|
|
|
// partitioning of Histograms is expensive and should be used judiciously.
|
2017-05-09 19:46:09 +03:00
|
|
|
//
|
|
|
|
// If the wrapped RoundTripper panics or returns a non-nil error, no values are
|
|
|
|
// reported.
|
2017-06-29 17:07:12 +03:00
|
|
|
//
|
2022-08-02 20:33:08 +03:00
|
|
|
// Use with WithExemplarFromContext to instrument the exemplars on the duration histograms.
|
|
|
|
//
|
2017-06-29 17:07:12 +03:00
|
|
|
// Note that this method is only guaranteed to never observe negative durations
|
|
|
|
// if used with Go1.9+.
|
2022-01-18 12:19:28 +03:00
|
|
|
func InstrumentRoundTripperDuration(obs prometheus.ObserverVec, next http.RoundTripper, opts ...Option) RoundTripperFunc {
|
2022-08-02 20:33:08 +03:00
|
|
|
rtOpts := defaultOptions()
|
2022-01-18 12:19:28 +03:00
|
|
|
for _, o := range opts {
|
2022-08-02 20:33:08 +03:00
|
|
|
o.apply(rtOpts)
|
2022-01-18 12:19:28 +03:00
|
|
|
}
|
|
|
|
|
2017-05-09 19:46:09 +03:00
|
|
|
code, method := checkLabels(obs)
|
|
|
|
|
2022-08-02 20:33:08 +03:00
|
|
|
return func(r *http.Request) (*http.Response, error) {
|
2017-05-09 19:46:09 +03:00
|
|
|
start := time.Now()
|
|
|
|
resp, err := next.RoundTrip(r)
|
|
|
|
if err == nil {
|
2022-08-02 20:33:08 +03:00
|
|
|
exemplarObserve(
|
|
|
|
obs.With(labels(code, method, r.Method, resp.StatusCode, rtOpts.extraMethods...)),
|
|
|
|
time.Since(start).Seconds(),
|
|
|
|
rtOpts.getExemplarFn(r.Context()),
|
|
|
|
)
|
2017-05-09 19:46:09 +03:00
|
|
|
}
|
|
|
|
return resp, err
|
2022-08-02 20:33:08 +03:00
|
|
|
}
|
2017-05-09 19:46:09 +03:00
|
|
|
}
|
2019-04-29 00:26:36 +03:00
|
|
|
|
|
|
|
// InstrumentTrace is used to offer flexibility in instrumenting the available
|
|
|
|
// httptrace.ClientTrace hook functions. Each function is passed a float64
|
|
|
|
// representing the time in seconds since the start of the http request. A user
|
|
|
|
// may choose to use separately buckets Histograms, or implement custom
|
|
|
|
// instance labels on a per function basis.
|
|
|
|
type InstrumentTrace struct {
|
|
|
|
GotConn func(float64)
|
|
|
|
PutIdleConn func(float64)
|
|
|
|
GotFirstResponseByte func(float64)
|
|
|
|
Got100Continue func(float64)
|
|
|
|
DNSStart func(float64)
|
|
|
|
DNSDone func(float64)
|
|
|
|
ConnectStart func(float64)
|
|
|
|
ConnectDone func(float64)
|
|
|
|
TLSHandshakeStart func(float64)
|
|
|
|
TLSHandshakeDone func(float64)
|
|
|
|
WroteHeaders func(float64)
|
|
|
|
Wait100Continue func(float64)
|
|
|
|
WroteRequest func(float64)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstrumentRoundTripperTrace is a middleware that wraps the provided
|
|
|
|
// RoundTripper and reports times to hook functions provided in the
|
|
|
|
// InstrumentTrace struct. Hook functions that are not present in the provided
|
|
|
|
// InstrumentTrace struct are ignored. Times reported to the hook functions are
|
|
|
|
// time since the start of the request. Only with Go1.9+, those times are
|
|
|
|
// guaranteed to never be negative. (Earlier Go versions are not using a
|
|
|
|
// monotonic clock.) Note that partitioning of Histograms is expensive and
|
|
|
|
// should be used judiciously.
|
|
|
|
//
|
|
|
|
// For hook functions that receive an error as an argument, no observations are
|
|
|
|
// made in the event of a non-nil error value.
|
|
|
|
//
|
|
|
|
// See the example for ExampleInstrumentRoundTripperDuration for example usage.
|
|
|
|
func InstrumentRoundTripperTrace(it *InstrumentTrace, next http.RoundTripper) RoundTripperFunc {
|
2022-08-02 20:33:08 +03:00
|
|
|
return func(r *http.Request) (*http.Response, error) {
|
2019-04-29 00:26:36 +03:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
trace := &httptrace.ClientTrace{
|
|
|
|
GotConn: func(_ httptrace.GotConnInfo) {
|
|
|
|
if it.GotConn != nil {
|
|
|
|
it.GotConn(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
PutIdleConn: func(err error) {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if it.PutIdleConn != nil {
|
|
|
|
it.PutIdleConn(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
DNSStart: func(_ httptrace.DNSStartInfo) {
|
|
|
|
if it.DNSStart != nil {
|
|
|
|
it.DNSStart(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
DNSDone: func(_ httptrace.DNSDoneInfo) {
|
|
|
|
if it.DNSDone != nil {
|
|
|
|
it.DNSDone(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ConnectStart: func(_, _ string) {
|
|
|
|
if it.ConnectStart != nil {
|
|
|
|
it.ConnectStart(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ConnectDone: func(_, _ string, err error) {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if it.ConnectDone != nil {
|
|
|
|
it.ConnectDone(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
GotFirstResponseByte: func() {
|
|
|
|
if it.GotFirstResponseByte != nil {
|
|
|
|
it.GotFirstResponseByte(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Got100Continue: func() {
|
|
|
|
if it.Got100Continue != nil {
|
|
|
|
it.Got100Continue(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
TLSHandshakeStart: func() {
|
|
|
|
if it.TLSHandshakeStart != nil {
|
|
|
|
it.TLSHandshakeStart(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
TLSHandshakeDone: func(_ tls.ConnectionState, err error) {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if it.TLSHandshakeDone != nil {
|
|
|
|
it.TLSHandshakeDone(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
WroteHeaders: func() {
|
|
|
|
if it.WroteHeaders != nil {
|
|
|
|
it.WroteHeaders(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Wait100Continue: func() {
|
|
|
|
if it.Wait100Continue != nil {
|
|
|
|
it.Wait100Continue(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
WroteRequest: func(_ httptrace.WroteRequestInfo) {
|
|
|
|
if it.WroteRequest != nil {
|
|
|
|
it.WroteRequest(time.Since(start).Seconds())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2019-05-16 12:03:34 +03:00
|
|
|
r = r.WithContext(httptrace.WithClientTrace(r.Context(), trace))
|
2019-04-29 00:26:36 +03:00
|
|
|
|
|
|
|
return next.RoundTrip(r)
|
2022-08-02 20:33:08 +03:00
|
|
|
}
|
2019-04-29 00:26:36 +03:00
|
|
|
}
|