| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/tls" |
| 6 | "net/http" |
| 7 | "net/http/httptrace" |
| 8 | ) |
| 9 | |
| 10 | func (s *requestObservationState) request(req *http.Request) { |
| 11 | if s == nil || req == nil || req.URL == nil { |
| 12 | return |
| 13 | } |
| 14 | s.update("request_built", func(v *RequestObservation) { |
| 15 | v.Method, v.Host, v.RequestPath = req.Method, req.URL.Host, req.URL.EscapedPath() |
| 16 | if len(v.Host) > 255 { |
| 17 | v.Host = "" |
| 18 | } |
| 19 | if len(v.RequestPath) > 512 { |
| 20 | v.RequestPath = "" |
| 21 | } |
| 22 | v.RequestBytes = req.ContentLength |
| 23 | }) |
| 24 | } |
| 25 | |
| 26 | func (s *requestObservationState) traceContext(ctx context.Context) context.Context { |
| 27 | return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{ |
| 28 | DNSStart: func(httptrace.DNSStartInfo) { s.update("dns_started", nil) }, |
| 29 | ConnectStart: func(network, addr string) { |
| 30 | s.update("connect_started", func(v *RequestObservation) { |
| 31 | v.Network = network |
| 32 | if len(addr) <= 255 { |
| 33 | v.DialAddress = addr |
| 34 | } |
| 35 | }) |
| 36 | }, |
| 37 | TLSHandshakeStart: func() { s.update("tls_started", nil) }, |
| 38 | TLSHandshakeDone: func(state tls.ConnectionState, err error) { |
| 39 | if err == nil { |
| 40 | s.update("tls_complete", func(v *RequestObservation) { |
| 41 | if state.NegotiatedProtocol == "h2" { |
| 42 | v.HTTPProtocol = "HTTP/2.0" |
| 43 | } |
| 44 | if state.NegotiatedProtocol == "http/1.1" { |
| 45 | v.HTTPProtocol = "HTTP/1.1" |
| 46 | } |
| 47 | }) |
| 48 | } |
| 49 | }, |
| 50 | GotConn: func(info httptrace.GotConnInfo) { |
| 51 | s.update("connection_acquired", func(v *RequestObservation) { |
| 52 | v.ConnectedAt, v.ConnectionReused = v.ObservedAt, info.Reused |
| 53 | if info.Conn != nil && info.Conn.RemoteAddr() != nil { |
| 54 | if addr := info.Conn.RemoteAddr().String(); len(addr) <= 255 { |
| 55 | v.RemoteAddress = addr |
| 56 | } |
| 57 | } |
| 58 | if conn, ok := info.Conn.(interface{ ConnectionState() tls.ConnectionState }); ok { |
| 59 | if conn.ConnectionState().NegotiatedProtocol == "h2" { |
| 60 | v.HTTPProtocol = "HTTP/2.0" |
| 61 | } |
| 62 | } |
| 63 | }) |
| 64 | }, |
| 65 | WroteRequest: func(info httptrace.WroteRequestInfo) { |
| 66 | if info.Err == nil { |
| 67 | s.update("request_written", func(v *RequestObservation) { v.WrittenAt = v.ObservedAt }) |
| 68 | } |
| 69 | }, |
| 70 | }) |
| 71 | } |
| 72 |