Skip to content

Commit

Permalink
fix(forwarder): add user agent to HTTP requests (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
jta authored May 13, 2024
1 parent 2074e08 commit fd13844
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/forwarder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ func realInit() (err error) {

var s3Client forwarder.S3Client = awsS3Client
if strings.HasPrefix(env.DestinationURI, "https") {
userAgent := fmt.Sprintf("forwarder/%s", version.Version)
logger.V(4).Info("loading http client")
s3Client, err = s3http.New(&s3http.Config{
DestinationURI: env.DestinationURI,
GetObjectAPIClient: awsS3Client,
HTTPClient: tracing.NewHTTPClient(&tracing.HTTPClientConfig{
TracerProvider: tracerProvider,
Logger: &logger,
UserAgent: &userAgent,
}),
})
if err != nil {
Expand Down
20 changes: 19 additions & 1 deletion tracing/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type HTTPClientConfig struct {
RetryWaitMax *time.Duration // Maximumum time to wait on retry
RetryMax *int // Maximum number of retries
HTTPClient *http.Client
UserAgent *string
Logger *logr.Logger
TracerProvider *trace.TracerProvider
}
Expand Down Expand Up @@ -70,8 +71,25 @@ func NewHTTPClient(cfg *HTTPClientConfig) *http.Client {

client.Logger = &leveledLogger{logger}

transport := &retryablehttp.RoundTripper{Client: client}
var transport http.RoundTripper = &retryablehttp.RoundTripper{Client: client}
if cfg.UserAgent != nil {
transport = &addUserAgent{
RoundTripper: transport,
UserAgent: *cfg.UserAgent,
}
}
return &http.Client{
Transport: otelhttp.NewTransport(transport, otelhttp.WithTracerProvider(cfg.TracerProvider)),
}
}

type addUserAgent struct {
UserAgent string
http.RoundTripper
}

func (rt *addUserAgent) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("User-Agent", rt.UserAgent)
//nolint:wrapcheck
return rt.RoundTripper.RoundTrip(req)
}

0 comments on commit fd13844

Please sign in to comment.