-
Notifications
You must be signed in to change notification settings - Fork 20
/
httpClient.go
115 lines (98 loc) · 2.62 KB
/
httpClient.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"errors"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/go-kit/kit/metrics"
)
var (
errNilHistogram = errors.New("histogram cannot be nil")
)
type httpClient interface {
Do(*http.Request) (*http.Response, error)
}
func nopHTTPClient(next httpClient) httpClient {
return next
}
// DoerFunc implements HTTPClient
type doerFunc func(*http.Request) (*http.Response, error)
func (d doerFunc) Do(req *http.Request) (*http.Response, error) {
return d(req)
}
type metricWrapper struct {
now func() time.Time
queryLatency metrics.Histogram
}
func newMetricWrapper(now func() time.Time, queryLatency metrics.Histogram) (*metricWrapper, error) {
if now == nil {
now = time.Now
}
if queryLatency == nil {
return nil, errNilHistogram
}
return &metricWrapper{
now: now,
queryLatency: queryLatency,
}, nil
}
func (m *metricWrapper) roundTripper(next httpClient) httpClient {
return doerFunc(func(req *http.Request) (*http.Response, error) {
startTime := m.now()
resp, err := next.Do(req)
endTime := m.now()
code := genericDoReason
reason := noErrReason
if err != nil {
reason = getDoErrReason(err)
if resp != nil {
code = strconv.Itoa(resp.StatusCode)
}
} else {
code = strconv.Itoa(resp.StatusCode)
}
// find time difference, add to metric
m.queryLatency.With(urlLabel, req.URL.String(), reasonLabel, reason, codeLabel, code).Observe(endTime.Sub(startTime).Seconds())
return resp, err
})
}
func getDoErrReason(err error) string {
var d *net.DNSError
if err == nil {
return noErrReason
} else if errors.Is(err, context.DeadlineExceeded) {
return deadlineExceededReason
} else if errors.Is(err, context.Canceled) {
return contextCanceledReason
} else if errors.Is(err, &net.AddrError{}) {
return addressErrReason
} else if errors.Is(err, &net.ParseError{}) {
return parseAddrErrReason
} else if errors.Is(err, net.InvalidAddrError("")) {
return invalidAddrReason
} else if errors.As(err, &d) {
if d.IsNotFound {
return hostNotFoundReason
}
return dnsErrReason
} else if errors.Is(err, net.ErrClosed) {
return connClosedReason
} else if errors.Is(err, &net.OpError{}) {
return opErrReason
} else if errors.Is(err, net.UnknownNetworkError("")) {
return networkErrReason
}
// nolint: errorlint
if err, ok := err.(*url.Error); ok {
if strings.TrimSpace(strings.ToLower(err.Unwrap().Error())) == "eof" {
return connectionUnexpectedlyClosedEOFReason
}
}
return genericDoReason
}