| 1 | package lfshttp |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/tls" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "net/http" |
| 9 | "net/http/httptrace" |
| 10 | "strings" |
| 11 | "sync" |
| 12 | "sync/atomic" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | type httpTransfer struct { |
| 17 | // members managed via sync/atomic must be aligned at the top of this |
| 18 | // structure (see: https://github.com/git-lfs/git-lfs/pull/2880). |
| 19 | |
| 20 | RequestBodySize int64 |
| 21 | Start int64 |
| 22 | ResponseStart int64 |
| 23 | ConnStart int64 |
| 24 | ConnEnd int64 |
| 25 | DNSStart int64 |
| 26 | DNSEnd int64 |
| 27 | TLSStart int64 |
| 28 | TLSEnd int64 |
| 29 | URL string |
| 30 | Method string |
| 31 | Key string |
| 32 | } |
| 33 | |
| 34 | type statsContextKey string |
| 35 | |
| 36 | const transferKey = statsContextKey("transfer") |
| 37 | |
| 38 | func (c *Client) LogHTTPStats(w io.WriteCloser) { |
| 39 | fmt.Fprintf(w, "concurrent=%d time=%d version=%s\n", c.ConcurrentTransfers, time.Now().Unix(), UserAgent) |
| 40 | c.httpLogger = newSyncLogger(w) |
| 41 | } |
| 42 | |
| 43 | // LogStats is intended to be called after all HTTP operations for the |
| 44 | // command have finished. It dumps k/v logs, one line per httpTransfer into |
| 45 | // a log file with the current timestamp. |
| 46 | // |
| 47 | // DEPRECATED: Call LogHTTPStats() before the first HTTP request. |
| 48 | func (c *Client) LogStats(out io.Writer) {} |
| 49 | |
| 50 | // LogRequest tells the client to log the request's stats to the http log |
| 51 | // after the response body has been read. |
| 52 | func (c *Client) LogRequest(r *http.Request, reqKey string) *http.Request { |
| 53 | if c.httpLogger == nil { |
| 54 | return r |
| 55 | } |
| 56 | |
| 57 | t := &httpTransfer{ |
| 58 | URL: strings.SplitN(r.URL.String(), "?", 2)[0], |
| 59 | Method: r.Method, |
| 60 | Key: reqKey, |
| 61 | } |
| 62 | |
| 63 | ctx := httptrace.WithClientTrace(r.Context(), &httptrace.ClientTrace{ |
| 64 | GetConn: func(_ string) { |
| 65 | atomic.CompareAndSwapInt64(&t.Start, 0, time.Now().UnixNano()) |
| 66 | }, |
| 67 | DNSStart: func(_ httptrace.DNSStartInfo) { |
| 68 | atomic.CompareAndSwapInt64(&t.DNSStart, 0, time.Now().UnixNano()) |
| 69 | }, |
| 70 | DNSDone: func(_ httptrace.DNSDoneInfo) { |
| 71 | atomic.CompareAndSwapInt64(&t.DNSEnd, 0, time.Now().UnixNano()) |
| 72 | }, |
| 73 | ConnectStart: func(_, _ string) { |
| 74 | atomic.CompareAndSwapInt64(&t.ConnStart, 0, time.Now().UnixNano()) |
| 75 | }, |
| 76 | ConnectDone: func(_, _ string, _ error) { |
| 77 | atomic.CompareAndSwapInt64(&t.ConnEnd, 0, time.Now().UnixNano()) |
| 78 | }, |
| 79 | TLSHandshakeStart: func() { |
| 80 | atomic.CompareAndSwapInt64(&t.TLSStart, 0, time.Now().UnixNano()) |
| 81 | }, |
| 82 | TLSHandshakeDone: func(_ tls.ConnectionState, _ error) { |
| 83 | atomic.CompareAndSwapInt64(&t.TLSEnd, 0, time.Now().UnixNano()) |
| 84 | }, |
| 85 | GotFirstResponseByte: func() { |
| 86 | atomic.CompareAndSwapInt64(&t.ResponseStart, 0, time.Now().UnixNano()) |
| 87 | }, |
| 88 | }) |
| 89 | |
| 90 | return r.WithContext(context.WithValue(ctx, transferKey, t)) |
| 91 | } |
| 92 | |
| 93 | // LogResponse sends the current response stats to the http log. |
| 94 | // |
| 95 | // DEPRECATED: Use LogRequest() instead. |
| 96 | func (c *Client) LogResponse(key string, res *http.Response) {} |
| 97 | |
| 98 | func newSyncLogger(w io.WriteCloser) *syncLogger { |
| 99 | ch := make(chan string, 100) |
| 100 | wg := &sync.WaitGroup{} |
| 101 | wg.Add(1) |
| 102 | |
| 103 | go func(c chan string, w io.Writer, wg *sync.WaitGroup) { |
| 104 | for l := range c { |
| 105 | w.Write([]byte(l)) |
| 106 | wg.Done() |
| 107 | } |
| 108 | }(ch, w, wg) |
| 109 | |
| 110 | return &syncLogger{w: w, ch: ch, wg: wg} |
| 111 | } |
| 112 | |
| 113 | type syncLogger struct { |
| 114 | w io.WriteCloser |
| 115 | ch chan string |
| 116 | wg *sync.WaitGroup |
| 117 | } |
| 118 | |
| 119 | func (l *syncLogger) LogRequest(req *http.Request, bodySize int64) { |
| 120 | if l == nil { |
| 121 | return |
| 122 | } |
| 123 | |
| 124 | if v := req.Context().Value(transferKey); v != nil { |
| 125 | l.logTransfer(v.(*httpTransfer), "request", fmt.Sprintf(" body=%d", bodySize)) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func (l *syncLogger) LogResponse(req *http.Request, status int, bodySize int64) { |
| 130 | if l == nil { |
| 131 | return |
| 132 | } |
| 133 | |
| 134 | if v := req.Context().Value(transferKey); v != nil { |
| 135 | t := v.(*httpTransfer) |
| 136 | now := time.Now().UnixNano() |
| 137 | l.logTransfer(t, "response", |
| 138 | fmt.Sprintf(" status=%d body=%d conntime=%d dnstime=%d tlstime=%d restime=%d time=%d", |
| 139 | status, |
| 140 | bodySize, |
| 141 | max(t.ConnEnd-t.ConnStart, 0), |
| 142 | max(t.DNSEnd-t.DNSStart, 0), |
| 143 | max(t.TLSEnd-t.TLSStart, 0), |
| 144 | max(now-t.ResponseStart, 0), |
| 145 | max(now-t.Start, 0), |
| 146 | )) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func (l *syncLogger) logTransfer(t *httpTransfer, event, extra string) { |
| 151 | l.wg.Add(1) |
| 152 | l.ch <- fmt.Sprintf("key=%s event=%s url=%s method=%s%s\n", |
| 153 | t.Key, |
| 154 | event, |
| 155 | t.URL, |
| 156 | t.Method, |
| 157 | extra, |
| 158 | ) |
| 159 | } |
| 160 | |
| 161 | func (l *syncLogger) Close() error { |
| 162 | if l == nil { |
| 163 | return nil |
| 164 | } |
| 165 | |
| 166 | l.wg.Done() |
| 167 | l.wg.Wait() |
| 168 | return l.w.Close() |
| 169 | } |