| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/tls" |
| 6 | "net/http" |
| 7 | "net/http/httptrace" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func (s *requestObservationState) request(req *http.Request) { |
| 12 | if s == nil || req == nil || req.URL == nil { |
| 13 | return |
| 14 | } |
| 15 | s.updateBeforeHeaders("request_built", func(v *RequestObservation) { |
| 16 | v.Method, v.Host, v.RequestPath = req.Method, req.URL.Host, req.URL.EscapedPath() |
| 17 | if len(v.Host) > 255 { |
| 18 | v.Host = "" |
| 19 | } |
| 20 | if len(v.RequestPath) > 512 { |
| 21 | v.RequestPath = "" |
| 22 | } |
| 23 | v.RequestBytes = req.ContentLength |
| 24 | }) |
| 25 | } |
| 26 | |
| 27 | func (s *requestObservationState) traceContext(ctx context.Context) context.Context { |
| 28 | return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{ |
| 29 | GetConn: func(string) { |
| 30 | s.updateBeforeHeaders("connection_wait", func(v *RequestObservation) { |
| 31 | // Redirects and Transport retries start another send stage. A prior |
| 32 | // write is not evidence that this stage's body has been sent. |
| 33 | v.WrittenAt = time.Time{} |
| 34 | v.ErrorCode, v.ErrorPhase = "", "" |
| 35 | }) |
| 36 | }, |
| 37 | DNSStart: func(httptrace.DNSStartInfo) { s.updateBeforeHeaders("dns_started", nil) }, |
| 38 | DNSDone: func(info httptrace.DNSDoneInfo) { s.transportFailure(info.Err, "dns_failed") }, |
| 39 | ConnectStart: func(network, addr string) { |
| 40 | s.updateBeforeHeaders("connect_started", func(v *RequestObservation) { |
| 41 | v.Network = network |
| 42 | if len(addr) <= 255 { |
| 43 | v.DialAddress = addr |
| 44 | } |
| 45 | }) |
| 46 | }, |
| 47 | TLSHandshakeStart: func() { s.updateBeforeHeaders("tls_started", nil) }, |
| 48 | ConnectDone: func(_, _ string, err error) { s.transportFailure(err, "connect_failed") }, |
| 49 | TLSHandshakeDone: func(state tls.ConnectionState, err error) { |
| 50 | if err != nil { |
| 51 | s.transportFailure(err, "tls_failed") |
| 52 | } |
| 53 | if err == nil { |
| 54 | s.updateBeforeHeaders("tls_complete", func(v *RequestObservation) { |
| 55 | if state.NegotiatedProtocol == "h2" { |
| 56 | v.HTTPProtocol = "HTTP/2.0" |
| 57 | } |
| 58 | if state.NegotiatedProtocol == "http/1.1" { |
| 59 | v.HTTPProtocol = "HTTP/1.1" |
| 60 | } |
| 61 | }) |
| 62 | } |
| 63 | }, |
| 64 | GotConn: func(info httptrace.GotConnInfo) { |
| 65 | s.updateBeforeHeaders("connection_acquired", func(v *RequestObservation) { |
| 66 | // A successful connection supersedes failed DNS/dial candidates. |
| 67 | v.ErrorCode, v.ErrorPhase = "", "" |
| 68 | v.ConnectedAt, v.ConnectionReused = v.ObservedAt, info.Reused |
| 69 | if info.Conn != nil && info.Conn.RemoteAddr() != nil { |
| 70 | if addr := info.Conn.RemoteAddr().String(); len(addr) <= 255 { |
| 71 | v.RemoteAddress = addr |
| 72 | } |
| 73 | } |
| 74 | if conn, ok := info.Conn.(interface{ ConnectionState() tls.ConnectionState }); ok { |
| 75 | if conn.ConnectionState().NegotiatedProtocol == "h2" { |
| 76 | v.HTTPProtocol = "HTTP/2.0" |
| 77 | } |
| 78 | } |
| 79 | }) |
| 80 | }, |
| 81 | WroteRequest: func(info httptrace.WroteRequestInfo) { |
| 82 | if info.Err == nil { |
| 83 | s.updateBeforeHeaders("request_written", func(v *RequestObservation) { |
| 84 | v.WrittenAt = v.ObservedAt |
| 85 | v.ErrorCode, v.ErrorPhase = "", "" |
| 86 | }) |
| 87 | } else { |
| 88 | s.transportFailure(info.Err, "request_write_failed") |
| 89 | } |
| 90 | }, |
| 91 | }) |
| 92 | } |
| 93 |