forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
38 lines (32 loc) · 792 Bytes
/
http.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
package proxy
import (
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"time"
"github.com/eBay/fabio/metrics"
)
func newHTTPProxy(t *url.URL, tr http.RoundTripper, flush time.Duration) http.Handler {
rp := httputil.NewSingleHostReverseProxy(t)
rp.Transport = tr
rp.FlushInterval = flush
rp.Transport = &meteredRoundTripper{tr}
return rp
}
type meteredRoundTripper struct {
tr http.RoundTripper
}
func (m *meteredRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
start := time.Now()
resp, err := m.tr.RoundTrip(r)
if resp != nil {
metrics.DefaultRegistry.GetTimer(name(resp.StatusCode)).UpdateSince(start)
}
return resp, err
}
func name(code int) string {
b := []byte("http.status.")
b = strconv.AppendInt(b, int64(code), 10)
return string(b)
}