diff --git a/client.go b/client.go index 34f2e07..3d22064 100644 --- a/client.go +++ b/client.go @@ -195,6 +195,7 @@ func New(opts ...OptionClientFn) (*Client, error) { Base: client.Transport, Header: o.Header, BaseURL: baseURL, + Inject: o.Inject, } if len(o.RoundTripperList) > 0 { diff --git a/option.go b/option.go index c7e56cf..7c63a6c 100644 --- a/option.go +++ b/option.go @@ -65,6 +65,9 @@ type optionClientValue struct { // Proxy for http(s) requests. Proxy string + + // Inject extra content to request (e.g. tracing propagation). + Inject func(ctx context.Context, req *http.Request) } // OptionClientFn is a function that configures the client. @@ -258,3 +261,14 @@ func WithProxy(proxy string) OptionClientFn { options.Proxy = proxy } } + +// WithInject configures the client to use the provided inject function. +// +// func(ctx context.Context, req *http.Request) { +// otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(req.Header)) +// } +func WithInject(inject func(ctx context.Context, req *http.Request)) OptionClientFn { + return func(options *optionClientValue) { + options.Inject = inject + } +} diff --git a/transport.go b/transport.go index 52f6122..76bbaad 100644 --- a/transport.go +++ b/transport.go @@ -1,6 +1,7 @@ package klient import ( + "context" "net/http" "net/url" ) @@ -15,6 +16,9 @@ type TransportKlient struct { Header http.Header // BaseURL is the base URL for relative requests. BaseURL *url.URL + + // Inject extra content to request (e.g. tracing propagation). + Inject func(ctx context.Context, req *http.Request) } var _ http.RoundTripper = (*TransportKlient)(nil) @@ -51,6 +55,10 @@ func (t *TransportKlient) SetHeader(req *http.Request) { req.Header[k] = v } } + + if t.Inject != nil { + t.Inject(ctx, req) + } } // RoundTrip authorizes and authenticates the request with an