/
impl.go
120 lines (103 loc) · 2.78 KB
/
impl.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
116
117
118
119
120
package orcclient
import (
"fmt"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
// func (c *orcClient) CloseIdleConnections() {
// c.httpClient.CloseIdleConnections()
// }
func (c *orcClient) Do(req *http.Request) (*http.Response, error) {
t0 := time.Now()
targetCanonicalHost, err := c.checkRequest(req)
if err != nil {
logrus.WithFields(logrus.Fields{
"client": c.clientName,
"error": err,
}).Errorf("Rejected outgoing request")
metricRequestsRejected.With(prometheus.Labels{
"client": c.clientName,
"reason": checkFailedStatsReason(err),
}).Inc()
return nil, err
}
if err := c.m.addAuth(req, targetCanonicalHost); err != nil {
logrus.WithFields(logrus.Fields{
"client": c.clientName,
"error": err,
}).Errorf("Rejected outgoing request: failed to add outer-layer auth")
metricRequestsRejected.With(prometheus.Labels{
"client": c.clientName,
"reason": "failed-to-add-auth",
}).Inc()
return nil, fmt.Errorf("Failed to add auth: %v", err)
}
labels := prometheus.Labels{
"client": c.clientName,
"method": req.Method,
"target": targetCanonicalHost,
}
metricRequestsBegun.With(labels).Inc()
labels["ok"] = "false"
labels["code"] = ""
var finalResponse *http.Response
var finalError error
defer func() {
labels["ok"] = fmt.Sprintf("%v", err == nil)
if finalResponse != nil {
labels["code"] = fmt.Sprintf("%d", finalResponse.StatusCode)
}
duration := time.Since(t0)
logrus.WithFields(logrus.Fields{
"client": c.clientName,
"error": err,
"target": targetCanonicalHost,
"scheme": req.URL.Scheme,
"path": req.URL.Path,
"duration": duration,
}).Infof("Finished outgoing request")
metricRequestsFinished.With(labels).Inc()
metricRequestsFinishedLatency.With(labels).Observe(duration.Seconds())
}()
finalResponse, finalError = c.httpClient.Do(req)
return finalResponse, finalError
}
type badOrcRequest struct {
statsReason string
}
func (e badOrcRequest) Error() string {
return fmt.Sprintf("Bad outgoing request: %v", e.statsReason)
}
func checkFailedStatsReason(err error) string {
if cast, ok := err.(badOrcRequest); ok {
return cast.statsReason
}
if err == nil {
return "success"
}
return "unknown"
}
func (c *orcClient) checkRequest(req *http.Request) (string, error) {
if req == nil {
return "", badOrcRequest{"nil"}
}
if req.Method == "" {
return "", badOrcRequest{"empty-method"}
}
if req.URL == nil {
return "", badOrcRequest{"nil-url"}
}
targetHost := req.URL.Host
if req.URL.Scheme != "https" {
return "", badOrcRequest{"not-https"}
}
if req.URL.User != nil {
return "", badOrcRequest{"extra-userinfo"}
}
if !c.m.isHostAllowed(targetHost) {
return "", badOrcRequest{"host-not-allowed"}
}
return targetHost, nil
}