/
latency_metrics.go
117 lines (109 loc) · 3.21 KB
/
latency_metrics.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
package latencymetrics
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"os"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
)
type latencyMetric interface {
TimeLatency()
}
//URLMetric struct for urlmetric
type URLMetric struct {
url string
dns time.Duration
connect time.Duration
sslshake time.Duration
ttfb time.Duration
rtt time.Duration
}
// NewLatencyMetricObject Creates URLMetric Object and returns
func NewLatencyMetricObject(urlStr string) *URLMetric {
parsedURL, parseErr := url.Parse(urlStr)
logger := log.NewLogfmtLogger(os.Stderr)
if parseErr != nil {
level.Error(logger).Log("msg", "Invalid Url", "err", parseErr)
} else {
if parsedURL.Scheme == "" {
level.Info(logger).Log("msg", "url scheme provided is Empty Falling back to http")
urlStr = "http://" + urlStr
}
}
return &URLMetric{url: urlStr}
}
func (um *URLMetric) String() string {
return fmt.Sprintf("url: %s\nDns: %v\nConnect: %v\nSSL Handshake : %v\nTTFB : %v\nRTT: %v", um.url, um.dns, um.connect, um.sslshake, um.ttfb, um.rtt)
}
// TimeLatency collects latency metrics and updats URLMetric
func (um *URLMetric) TimeLatency(e *Exporter) error {
var start, dns, connect, sslshake time.Time
um.dns = time.Duration(0 * time.Second)
req, err := http.NewRequest("GET", um.url, nil)
if err != nil {
level.Error(e.logger).Log("msg", "Error Creating New Request", "err", err)
}
trace := &httptrace.ClientTrace{
DNSStart: func(dsi httptrace.DNSStartInfo) {
dns = time.Now()
},
DNSDone: func(ddi httptrace.DNSDoneInfo) {
um.dns = time.Since(dns)
},
TLSHandshakeStart: func() {
sslshake = time.Now()
},
TLSHandshakeDone: func(cs tls.ConnectionState, err error) {
if err != nil {
level.Error(e.logger).Log("msg", "Error TLS Handshake", "err", err)
um.sslshake = time.Duration(0 * time.Second)
}
um.sslshake = time.Since(sslshake)
},
ConnectStart: func(network, addr string) {
connect = time.Now()
},
ConnectDone: func(network, addr string, err error) {
if err != nil {
level.Error(e.logger).Log("msg", "Error Conection Time", "err", err)
um.connect = time.Duration(0 * time.Second)
}
um.connect = time.Since(connect)
},
GotFirstResponseByte: func() {
um.ttfb = time.Since(start)
},
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
req = req.WithContext(ctx)
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
start = time.Now()
// customTransport can be updated for differrent Values as per requirement
var customTransport http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 2 * time.Second,
KeepAlive: 2 * time.Second,
DualStack: true,
}).DialContext,
DisableKeepAlives: true,
ForceAttemptHTTP2: true,
MaxIdleConns: 5,
IdleConnTimeout: 5 * time.Second,
TLSHandshakeTimeout: 1 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
if _, err := customTransport.RoundTrip(req); err != nil {
level.Error(e.logger).Log("msg", "Error Completeing RTTs", req.URL, "err", err)
return err
}
um.rtt = time.Since(start)
return err
}