/
monitor_proxy.go
170 lines (128 loc) · 3.99 KB
/
monitor_proxy.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package http
import (
"net/http"
"time"
"github.com/jukylin/esim/config"
"github.com/jukylin/esim/log"
"github.com/jukylin/esim/opentracing"
"github.com/opentracing-contrib/go-stdlib/nethttp"
opentracing2 "github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
)
// MonitorProxy wraps a RoundTripper.
type MonitorProxy struct {
nextTransport http.RoundTripper
logger log.Logger
conf config.Config
// use nethttp.Tracer
tracer opentracing2.Tracer
afterEvents []afterEvents
name string
}
type afterEvents func(beginTime, endTime time.Time,
res *http.Request, resp *http.Response)
type MonitorProxyOption func(c *MonitorProxy)
type MonitorProxyOptions struct{}
func NewMonitorProxy(options ...MonitorProxyOption) *MonitorProxy {
MonitorProxy := &MonitorProxy{}
for _, option := range options {
option(MonitorProxy)
}
if MonitorProxy.conf == nil {
MonitorProxy.conf = config.NewNullConfig()
}
if MonitorProxy.logger == nil {
MonitorProxy.logger = log.NewLogger()
}
if MonitorProxy.tracer == nil {
MonitorProxy.tracer = opentracing.NewTracer("http",
MonitorProxy.logger)
}
MonitorProxy.registerAfterEvent()
MonitorProxy.name = "monitor_proxy"
return MonitorProxy
}
func (MonitorProxyOptions) WithConf(conf config.Config) MonitorProxyOption {
return func(pt *MonitorProxy) {
pt.conf = conf
}
}
func (MonitorProxyOptions) WithLogger(logger log.Logger) MonitorProxyOption {
return func(pt *MonitorProxy) {
pt.logger = logger
}
}
// Use nethttp.Tracer.
func (MonitorProxyOptions) WithTracer(tracer opentracing2.Tracer) MonitorProxyOption {
return func(c *MonitorProxy) {
c.tracer = tracer
}
}
func (mp *MonitorProxy) NextProxy(tripper interface{}) {
mp.nextTransport = tripper.(http.RoundTripper)
}
func (mp *MonitorProxy) ProxyName() string {
return mp.name
}
// RoundTrip implements the RoundTripper interface.
func (mp *MonitorProxy) RoundTrip(req *http.Request) (*http.Response, error) {
if mp.nextTransport == nil {
mp.nextTransport = http.DefaultTransport
}
mp.logger.Debugc(req.Context(), "Url : %s", req.URL)
beginTime := time.Now()
var resp *http.Response
var err error
if mp.conf.GetBool("http_client_trace") {
tracerReq, ht := nethttp.TraceRequest(mp.tracer, req)
transport := nethttp.Transport{}
transport.RoundTripper = mp.nextTransport
resp, err = transport.RoundTrip(tracerReq)
ht.Finish()
} else {
resp, err = mp.nextTransport.RoundTrip(req)
}
if err != nil {
return resp, err
}
mp.after(beginTime, req, resp)
return resp, nil
}
func (mp *MonitorProxy) registerAfterEvent() {
if mp.conf.GetBool("http_client_check_slow") {
mp.afterEvents = append(mp.afterEvents, mp.slowHTTPRequest)
}
if mp.conf.GetBool("http_client_metrics") {
mp.afterEvents = append(mp.afterEvents, mp.httpClientMetrice)
}
if mp.conf.GetBool("debug") {
mp.afterEvents = append(mp.afterEvents, mp.debugHTTP)
}
}
func (mp *MonitorProxy) after(beginTime time.Time, res *http.Request, resp *http.Response) {
endTime := time.Now()
for _, event := range mp.afterEvents {
event(beginTime, endTime, res, resp)
}
}
func (mp *MonitorProxy) slowHTTPRequest(beginTime, endTime time.Time,
res *http.Request, resp *http.Response) {
httpClientSlowTime := mp.conf.GetInt64("http_client_slow_time")
if httpClientSlowTime != 0 {
if endTime.Sub(beginTime) > time.Duration(httpClientSlowTime)*time.Millisecond {
mp.logger.Warnf("slow http request [%s] :%s", endTime.Sub(beginTime).String(),
res.RequestURI)
}
}
}
func (mp *MonitorProxy) httpClientMetrice(beginTime, endTime time.Time,
res *http.Request, resp *http.Response) {
lab := prometheus.Labels{"url": res.URL.String(), "method": res.Method}
httpTotal.With(lab).Inc()
httpDuration.With(lab).Observe(endTime.Sub(beginTime).Seconds())
}
func (mp *MonitorProxy) debugHTTP(beginTime, endTime time.Time,
req *http.Request, resp *http.Response) {
mp.logger.Debugf("http [%d] [%s] %s : %s", resp.StatusCode, endTime.Sub(beginTime).String(),
req.Method, req.URL.String())
}