/
instrumented.go
70 lines (59 loc) · 1.95 KB
/
instrumented.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package roundtripper
import (
"github.com/clambin/go-common/http/metrics"
"net/http"
"time"
)
var _ http.RoundTripper = &roundTripMeasurer{}
type roundTripMeasurer struct {
next http.RoundTripper
metrics metrics.RequestMetrics
}
// WithRequestMetrics creates a [http.RoundTripper] that measures requests count and duration.
// The caller must register the metrics with a Prometheus registry.
func WithRequestMetrics(m metrics.RequestMetrics) Option {
return WithInstrumentedRoundTripper(m)
}
// WithInstrumentedRoundTripper creates a [http.RoundTripper] that records requests metrics to the provided [metrics.RequestMetrics].
// The caller must register the metrics with a Prometheus registry.
//
// deprecated: use WithRequestMetrics instead.
func WithInstrumentedRoundTripper(m metrics.RequestMetrics) Option {
return func(next http.RoundTripper) http.RoundTripper {
return &roundTripMeasurer{
next: next,
metrics: m,
}
}
}
func (i *roundTripMeasurer) RoundTrip(req *http.Request) (*http.Response, error) {
start := time.Now()
resp, err := i.next.RoundTrip(req)
var statusCode int
if err == nil {
statusCode = resp.StatusCode
}
i.metrics.Measure(req, statusCode, time.Since(start))
return resp, err
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
var _ http.RoundTripper = &inflightMeasurer{}
type inflightMeasurer struct {
metrics metrics.InFlightMetrics
next http.RoundTripper
}
// WithInflightMetrics creates a [http.RoundTripper] that measures outstanding requests.
// The caller must register the metrics with a Prometheus registry.
func WithInflightMetrics(m metrics.InFlightMetrics) Option {
return func(next http.RoundTripper) http.RoundTripper {
return &inflightMeasurer{
next: next,
metrics: m,
}
}
}
func (i inflightMeasurer) RoundTrip(request *http.Request) (*http.Response, error) {
i.metrics.Inc()
defer i.metrics.Dec()
return i.next.RoundTrip(request)
}