|
| 1 | +package apitoolkit |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | + "go.opentelemetry.io/otel" |
| 11 | + "go.opentelemetry.io/otel/trace" |
| 12 | +) |
| 13 | + |
| 14 | +type roundTripper struct { |
| 15 | + base http.RoundTripper |
| 16 | + ctx context.Context |
| 17 | + cfg *roundTripperConfig |
| 18 | +} |
| 19 | + |
| 20 | +func (rt *roundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) { |
| 21 | + defer func() { |
| 22 | + if err != nil { |
| 23 | + ReportError(rt.ctx, err) |
| 24 | + } |
| 25 | + }() |
| 26 | + |
| 27 | + tracer := otel.GetTracerProvider().Tracer("") |
| 28 | + _, span := tracer.Start(rt.ctx, "apitoolkit-http-span", trace.WithSpanKind(trace.SpanKindClient)) |
| 29 | + |
| 30 | + // Capture the request body |
| 31 | + reqBodyBytes := []byte{} |
| 32 | + if req.Body != nil { |
| 33 | + reqBodyBytes, _ = io.ReadAll(req.Body) |
| 34 | + req.Body = io.NopCloser(bytes.NewBuffer(reqBodyBytes)) |
| 35 | + } |
| 36 | + |
| 37 | + // Add a header to all outgoing requests "X-APITOOLKIT-TRACE-PARENT-ID" |
| 38 | + res, err = rt.base.RoundTrip(req) |
| 39 | + var errorList []ATError |
| 40 | + if err != nil { |
| 41 | + // Add the error for the given request payload |
| 42 | + errorList = append(errorList, BuildError(err)) |
| 43 | + } |
| 44 | + |
| 45 | + var payload Payload |
| 46 | + var parentMsgIDPtr *uuid.UUID |
| 47 | + parentMsgID, ok := rt.ctx.Value(CurrentRequestMessageID).(uuid.UUID) |
| 48 | + if ok { |
| 49 | + parentMsgIDPtr = &parentMsgID |
| 50 | + } |
| 51 | + |
| 52 | + // Capture the response body |
| 53 | + conf := roundTripperConfigToConfig(rt.cfg) |
| 54 | + if res != nil { |
| 55 | + respBodyBytes, _ := io.ReadAll(res.Body) |
| 56 | + res.Body = io.NopCloser(bytes.NewBuffer(respBodyBytes)) |
| 57 | + payload = BuildPayload( |
| 58 | + GoOutgoing, |
| 59 | + req, res.StatusCode, reqBodyBytes, |
| 60 | + respBodyBytes, res.Header, nil, |
| 61 | + req.URL.Path, |
| 62 | + rt.cfg.RedactHeaders, rt.cfg.RedactRequestBody, rt.cfg.RedactResponseBody, |
| 63 | + errorList, |
| 64 | + uuid.Nil, |
| 65 | + parentMsgIDPtr, |
| 66 | + conf, |
| 67 | + ) |
| 68 | + CreateSpan(payload, conf, span) |
| 69 | + |
| 70 | + } else { |
| 71 | + payload = BuildPayload( |
| 72 | + GoOutgoing, |
| 73 | + req, 503, reqBodyBytes, |
| 74 | + nil, nil, nil, |
| 75 | + req.URL.Path, |
| 76 | + rt.cfg.RedactHeaders, rt.cfg.RedactRequestBody, rt.cfg.RedactResponseBody, |
| 77 | + errorList, |
| 78 | + uuid.Nil, |
| 79 | + parentMsgIDPtr, |
| 80 | + conf, |
| 81 | + ) |
| 82 | + CreateSpan(payload, conf, span) |
| 83 | + |
| 84 | + } |
| 85 | + return res, err |
| 86 | +} |
| 87 | + |
| 88 | +func HTTPClient(ctx context.Context, opts ...RoundTripperOption) *http.Client { |
| 89 | + // Run the roundTripperConfig to extract out a httpClient Transport |
| 90 | + cfg := new(roundTripperConfig) |
| 91 | + for _, opt := range opts { |
| 92 | + opt(cfg) |
| 93 | + } |
| 94 | + |
| 95 | + httpClientV := *http.DefaultClient |
| 96 | + httpClient := &httpClientV |
| 97 | + if cfg.HTTPClient != nil { |
| 98 | + // Use httpClient supplied by user. |
| 99 | + v := *cfg.HTTPClient |
| 100 | + httpClient = &v |
| 101 | + } |
| 102 | + |
| 103 | + httpClient.Transport = WrapRoundTripper( |
| 104 | + ctx, httpClient.Transport, |
| 105 | + opts..., |
| 106 | + ) |
| 107 | + return httpClient |
| 108 | +} |
| 109 | + |
| 110 | +type roundTripperConfig struct { |
| 111 | + HTTPClient *http.Client |
| 112 | + RedactHeaders []string |
| 113 | + RedactRequestBody []string |
| 114 | + RedactResponseBody []string |
| 115 | +} |
| 116 | + |
| 117 | +type RoundTripperOption func(*roundTripperConfig) |
| 118 | + |
| 119 | +// WithHTTPClient allows you supply your own custom http client |
| 120 | +func WithHTTPClient(httpClient *http.Client) RoundTripperOption { |
| 121 | + return func(rc *roundTripperConfig) { |
| 122 | + rc.HTTPClient = httpClient |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func WithRedactHeaders(headers ...string) RoundTripperOption { |
| 127 | + return func(rc *roundTripperConfig) { |
| 128 | + rc.RedactHeaders = headers |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func WithRedactRequestBody(fields ...string) RoundTripperOption { |
| 133 | + return func(rc *roundTripperConfig) { |
| 134 | + rc.RedactRequestBody = fields |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func WithRedactResponseBody(fields ...string) RoundTripperOption { |
| 139 | + return func(rc *roundTripperConfig) { |
| 140 | + rc.RedactResponseBody = fields |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// WrapRoundTripper returns a new RoundTripper which traces all requests sent |
| 145 | +// over the transport. |
| 146 | +func WrapRoundTripper(ctx context.Context, rt http.RoundTripper, opts ...RoundTripperOption) http.RoundTripper { |
| 147 | + cfg := new(roundTripperConfig) |
| 148 | + for _, opt := range opts { |
| 149 | + opt(cfg) |
| 150 | + } |
| 151 | + |
| 152 | + // If no rt is passed in, then use the default standard library transport |
| 153 | + if rt == nil { |
| 154 | + rt = http.DefaultTransport |
| 155 | + } |
| 156 | + return &roundTripper{ |
| 157 | + base: rt, |
| 158 | + ctx: ctx, |
| 159 | + cfg: cfg, |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func roundTripperConfigToConfig(cfg *roundTripperConfig) Config { |
| 164 | + return Config{ |
| 165 | + RedactHeaders: cfg.RedactHeaders, |
| 166 | + RedactRequestBody: cfg.RedactRequestBody, |
| 167 | + RedactResponseBody: cfg.RedactResponseBody, |
| 168 | + CaptureRequestBody: true, |
| 169 | + CaptureResponseBody: true, |
| 170 | + } |
| 171 | +} |
0 commit comments