-
Notifications
You must be signed in to change notification settings - Fork 422
/
Copy pathround_tripper.go
50 lines (41 loc) · 1.16 KB
/
round_tripper.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
package client
import (
"crypto/tls"
"net/http"
"strconv"
"github.com/grafana/grafana-operator/v5/embeds"
"github.com/prometheus/client_golang/prometheus"
)
type instrumentedRoundTripper struct {
relatedResource string
wrapped http.RoundTripper
metric *prometheus.CounterVec
}
func NewInstrumentedRoundTripper(relatedResource string, metric *prometheus.CounterVec, useProxy bool, tlsConfig *tls.Config) http.RoundTripper {
transport := http.DefaultTransport.(*http.Transport).Clone() //nolint:errcheck
transport.DisableKeepAlives = true
transport.MaxIdleConnsPerHost = -1
if tlsConfig != nil {
transport.TLSClientConfig = tlsConfig
}
if !useProxy {
transport.Proxy = nil
}
return &instrumentedRoundTripper{
relatedResource: relatedResource,
wrapped: transport,
metric: metric,
}
}
func (in *instrumentedRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Add("user-agent", "grafana-operator/"+embeds.Version)
resp, err := in.wrapped.RoundTrip(r)
if resp != nil {
in.metric.WithLabelValues(
in.relatedResource,
r.Method,
strconv.Itoa(resp.StatusCode)).
Inc()
}
return resp, err
}