/
stats.go
171 lines (147 loc) · 4.2 KB
/
stats.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
171
package lfsapi
import (
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/http/httptrace"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/git-lfs/git-lfs/tools"
)
type httpTransfer struct {
// members managed via sync/atomic must be aligned at the top of this
// structure (see: https://github.com/git-lfs/git-lfs/pull/2880).
RequestBodySize int64
Start int64
ResponseStart int64
ConnStart int64
ConnEnd int64
DNSStart int64
DNSEnd int64
TLSStart int64
TLSEnd int64
URL string
Method string
Key string
}
type statsContextKey string
const transferKey = statsContextKey("transfer")
func (c *Client) LogHTTPStats(w io.WriteCloser) {
fmt.Fprintf(w, "concurrent=%d time=%d version=%s\n", c.ConcurrentTransfers, time.Now().Unix(), UserAgent)
c.httpLogger = newSyncLogger(w)
}
// LogStats is intended to be called after all HTTP operations for the
// commmand have finished. It dumps k/v logs, one line per httpTransfer into
// a log file with the current timestamp.
//
// DEPRECATED: Call LogHTTPStats() before the first HTTP request.
func (c *Client) LogStats(out io.Writer) {}
// LogRequest tells the client to log the request's stats to the http log
// after the response body has been read.
func (c *Client) LogRequest(r *http.Request, reqKey string) *http.Request {
if c.httpLogger == nil {
return r
}
t := &httpTransfer{
URL: strings.SplitN(r.URL.String(), "?", 2)[0],
Method: r.Method,
Key: reqKey,
}
ctx := httptrace.WithClientTrace(r.Context(), &httptrace.ClientTrace{
GetConn: func(_ string) {
atomic.CompareAndSwapInt64(&t.Start, 0, time.Now().UnixNano())
},
DNSStart: func(_ httptrace.DNSStartInfo) {
atomic.CompareAndSwapInt64(&t.DNSStart, 0, time.Now().UnixNano())
},
DNSDone: func(_ httptrace.DNSDoneInfo) {
atomic.CompareAndSwapInt64(&t.DNSEnd, 0, time.Now().UnixNano())
},
ConnectStart: func(_, _ string) {
atomic.CompareAndSwapInt64(&t.ConnStart, 0, time.Now().UnixNano())
},
ConnectDone: func(_, _ string, _ error) {
atomic.CompareAndSwapInt64(&t.ConnEnd, 0, time.Now().UnixNano())
},
TLSHandshakeStart: func() {
atomic.CompareAndSwapInt64(&t.TLSStart, 0, time.Now().UnixNano())
},
TLSHandshakeDone: func(_ tls.ConnectionState, _ error) {
atomic.CompareAndSwapInt64(&t.TLSEnd, 0, time.Now().UnixNano())
},
GotFirstResponseByte: func() {
atomic.CompareAndSwapInt64(&t.ResponseStart, 0, time.Now().UnixNano())
},
})
return r.WithContext(context.WithValue(ctx, transferKey, t))
}
// LogResponse sends the current response stats to the http log.
//
// DEPRECATED: Use LogRequest() instead.
func (c *Client) LogResponse(key string, res *http.Response) {}
func newSyncLogger(w io.WriteCloser) *syncLogger {
ch := make(chan string, 100)
wg := &sync.WaitGroup{}
wg.Add(1)
go func(c chan string, w io.Writer, wg *sync.WaitGroup) {
for l := range c {
w.Write([]byte(l))
wg.Done()
}
}(ch, w, wg)
return &syncLogger{w: w, ch: ch, wg: wg}
}
type syncLogger struct {
w io.WriteCloser
ch chan string
wg *sync.WaitGroup
}
func (l *syncLogger) LogRequest(req *http.Request, bodySize int64) {
if l == nil {
return
}
if v := req.Context().Value(transferKey); v != nil {
l.logTransfer(v.(*httpTransfer), "request", fmt.Sprintf(" body=%d", bodySize))
}
}
func (l *syncLogger) LogResponse(req *http.Request, status int, bodySize int64) {
if l == nil {
return
}
if v := req.Context().Value(transferKey); v != nil {
t := v.(*httpTransfer)
now := time.Now().UnixNano()
l.logTransfer(t, "response",
fmt.Sprintf(" status=%d body=%d conntime=%d dnstime=%d tlstime=%d restime=%d time=%d",
status,
bodySize,
tools.MaxInt64(t.ConnEnd-t.ConnStart, 0),
tools.MaxInt64(t.DNSEnd-t.DNSStart, 0),
tools.MaxInt64(t.TLSEnd-t.TLSStart, 0),
tools.MaxInt64(now-t.ResponseStart, 0),
tools.MaxInt64(now-t.Start, 0),
))
}
}
func (l *syncLogger) logTransfer(t *httpTransfer, event, extra string) {
l.wg.Add(1)
l.ch <- fmt.Sprintf("key=%s event=%s url=%s method=%s%s\n",
t.Key,
event,
t.URL,
t.Method,
extra,
)
}
func (l *syncLogger) Close() error {
if l == nil {
return nil
}
l.wg.Done()
l.wg.Wait()
return l.w.Close()
}