-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer.go
59 lines (50 loc) · 1.21 KB
/
tracer.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
package keepalivetrace
import (
"math/rand"
"net/http"
"net/http/httptrace"
"github.com/prometheus/client_golang/prometheus"
)
type (
// Tracer for keep-alive connection
// SampleRate 0..100 percent of traced requests
Tracer struct {
SampleRate int
trace *httptrace.ClientTrace
}
)
// NewPrometheusTracer tracer for prometheus
func NewPrometheusTracer(name string, rate int, vec *prometheus.CounterVec) *Tracer {
const (
newconn = "new"
reusedconn = "reused"
)
hooks := &httptrace.ClientTrace{
GotConn: func(con httptrace.GotConnInfo) {
if con.Reused {
vec.WithLabelValues(name, reusedconn).Inc()
} else {
vec.WithLabelValues(name, newconn).Inc()
}
},
}
return &Tracer{SampleRate: rate, trace: hooks}
}
// WithRequest wrap HTTP request
func (tracer Tracer) WithRequest(r *http.Request) *http.Request {
if !tracer.IsSampled() {
return r
}
ctx := r.Context()
ctx = httptrace.WithClientTrace(ctx, tracer.trace)
return r.WithContext(ctx)
}
// IsSampled checks that request must be traced
func (tracer Tracer) IsSampled() bool {
if tracer.SampleRate == 100 {
return true
} else if tracer.SampleRate == 0 {
return false
}
return 100-tracer.SampleRate < rand.Intn(100)
}