(req *http.Request)
| 42 | var _ http.RoundTripper = (*RecordingTransport)(nil) |
| 43 | |
| 44 | func (t *RecordingTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 45 | if req == nil { |
| 46 | panic("chatdebug: nil request") |
| 47 | } |
| 48 | |
| 49 | base := t.Base |
| 50 | if base == nil { |
| 51 | base = http.DefaultTransport |
| 52 | } |
| 53 | |
| 54 | sink := attemptSinkFromContext(req.Context()) |
| 55 | if sink == nil { |
| 56 | return base.RoundTrip(req) |
| 57 | } |
| 58 | |
| 59 | requestHeaders := RedactHeaders(req.Header) |
| 60 | |
| 61 | // Capture method and URL/path from the request. |
| 62 | method := req.Method |
| 63 | reqURL := "" |
| 64 | reqPath := "" |
| 65 | if req.URL != nil { |
| 66 | reqURL = redactURL(req.URL) |
| 67 | reqPath = req.URL.Path |
| 68 | } |
| 69 | |
| 70 | requestBody, err := captureRequestBody(req) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | attemptNumber := sink.nextAttemptNumber() |
| 75 | |
| 76 | startedAt := time.Now() |
| 77 | resp, err := base.RoundTrip(req) |
| 78 | finishedAt := time.Now() |
| 79 | durationMs := finishedAt.Sub(startedAt).Milliseconds() |
| 80 | if err != nil { |
| 81 | sink.record(Attempt{ |
| 82 | Number: attemptNumber, |
| 83 | Status: attemptStatusFailed, |
| 84 | Method: method, |
| 85 | URL: reqURL, |
| 86 | Path: reqPath, |
| 87 | StartedAt: startedAt.UTC().Format(time.RFC3339Nano), |
| 88 | FinishedAt: finishedAt.UTC().Format(time.RFC3339Nano), |
| 89 | RequestHeaders: requestHeaders, |
| 90 | RequestBody: requestBody, |
| 91 | Error: sanitizeErrorString(err.Error()), |
| 92 | DurationMs: durationMs, |
| 93 | }) |
| 94 | return nil, err |
| 95 | } |
| 96 | |
| 97 | respHeaders := RedactHeaders(resp.Header) |
| 98 | resp.Body = &recordingBody{ |
| 99 | inner: resp.Body, |
| 100 | sink: sink, |
| 101 | startedAt: startedAt, |
nothing calls this directly
no test coverage detected