Skip to content

BuildableClient use transport http.RoundTripper instead of transport *http.Transport #2405

@KafuuEriri

Description

@KafuuEriri

Describe the feature

github.com/aws/[email protected]/aws/transport/http

type BuildableClient struct {
	transport *http.Transport
	dialer    *net.Dialer

	initOnce sync.Once

	clientTimeout time.Duration
	client        *http.Client
}

use transport http.RoundTripper instead of transport *http.Transport can better support custom configuration on the client

Use Case

I would like to us tracking
eg.

type tracerTransport struct {
	*http.Transport

	debug       bool
	tracer      trace.Tracer
	propagation propagation.TextMapPropagator
}

func (r *tracerTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
	attrs := make([]attribute.KeyValue, 0)
	ctx, span := r.tracer.Start(req.Context(),
		fmt.Sprintf("%s %s%s", req.Method, req.Host, req.URL.Path),
		trace.WithSpanKind(trace.SpanKindClient))
	r.propagation.Inject(ctx, propagation.HeaderCarrier(req.Header))
	defer span.End()

	attrs = append(attrs, peerAttr(req.RemoteAddr)...)
	attrs = append(attrs, semconv.HTTPClientAttributesFromHTTPRequest(req)...)
	attrs = append(attrs, semconv.HTTPTargetKey.String(req.URL.Path))

	data, _ := io.ReadAll(req.Body)
	if len(data) > 0 {
		attrs = append(attrs, attribute.String("http.request.body", string(data)))
	}

	req = req.WithContext(ctx)
	req.ContentLength = int64(len(data))
	r.propagation.Inject(ctx, propagation.HeaderCarrier(req.Header))
	resp, err = r.Transport.RoundTrip(req)
	if err != nil {
		span.RecordError(err, trace.WithTimestamp(time.Now()))
		return
	}

	if resp.Body != nil {
		respDump, _ := httputil.DumpResponse(resp, r.debug)
		if len(respDump) > 0 {
			attrs = append(attrs, attribute.String("http.response.data", string(respDump)))
		}
	}

	attrs = append(attrs, semconv.HTTPStatusCodeKey.Int(resp.StatusCode))
	span.SetAttributes(attrs...)

	return resp, nil
}
httpClient = awshttp.NewBuildableClient().WithTransportOptions(func(transport *http. RoundTripper) {
			transport = createTransport(o.timeout)
		})

Proposed Solution

I tried using a custom client, but it may cause errors 'net/http: http: ContentLength=222 with Body length 0' when rewriting httpclient due to some middleware.But the official client doesn't have this problem

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

AWS Go SDK V2 Module Versions Used

github.com/aws/aws-sdk-go-v2 v1.22.2

Go version used

1.21

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestA feature should be added or improved.needs-triageThis issue or PR still needs to be triaged.response-requestedWaiting on additional info and feedback. Will move to "closing-soon" in 7 days.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions