Skip to content

Commit

Permalink
feat: transport inject request function
Browse files Browse the repository at this point in the history
Signed-off-by: Eray Ates <[email protected]>
  • Loading branch information
rytsh committed Oct 21, 2024
1 parent 7d0249e commit 5bf34d8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}
8 changes: 8 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package klient

import (
"context"
"net/http"
"net/url"
)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5bf34d8

Please sign in to comment.