diff --git a/cmd/forwarder/main.go b/cmd/forwarder/main.go index 9ccfbeb1..9c8613c9 100644 --- a/cmd/forwarder/main.go +++ b/cmd/forwarder/main.go @@ -114,7 +114,6 @@ 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, @@ -122,7 +121,6 @@ func realInit() (err error) { HTTPClient: tracing.NewHTTPClient(&tracing.HTTPClientConfig{ TracerProvider: tracerProvider, Logger: &logger, - UserAgent: &userAgent, }), }) if err != nil { diff --git a/tracing/aws.go b/tracing/aws.go index e1563177..fcf92d77 100644 --- a/tracing/aws.go +++ b/tracing/aws.go @@ -3,11 +3,15 @@ package tracing import ( "context" "fmt" + "os" "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/aws/middleware" "github.com/aws/aws-sdk-go-v2/config" "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws" "go.opentelemetry.io/otel/trace" + + "github.com/observeinc/aws-sam-apps/version" ) func AWSLoadDefaultConfig(ctx context.Context, tracerProvider trace.TracerProvider) (aws.Config, error) { @@ -15,6 +19,13 @@ func AWSLoadDefaultConfig(ctx context.Context, tracerProvider trace.TracerProvid if err != nil { return awsCfg, fmt.Errorf("failed to load AWS configuration: %w", err) } + + if serviceName := os.Getenv("OTEL_SERVICE_NAME"); serviceName != "" { + awsCfg.APIOptions = append(awsCfg.APIOptions, + middleware.AddUserAgentKeyValue(serviceName, version.Version), + ) + } + otelaws.AppendMiddlewares(&awsCfg.APIOptions, otelaws.WithTracerProvider(tracerProvider)) return awsCfg, nil } diff --git a/tracing/http.go b/tracing/http.go index 2abf71fa..7371e7e9 100644 --- a/tracing/http.go +++ b/tracing/http.go @@ -1,13 +1,17 @@ package tracing import ( + "fmt" "net/http" + "os" "time" "github.com/go-logr/logr" "github.com/hashicorp/go-retryablehttp" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/otel/trace" + + "github.com/observeinc/aws-sam-apps/version" ) // leveledLogger provides an adapter between logr.Logger and retryablehttp.LeveledLogger. @@ -36,7 +40,6 @@ 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 } @@ -72,10 +75,10 @@ func NewHTTPClient(cfg *HTTPClientConfig) *http.Client { client.Logger = &leveledLogger{logger} var transport http.RoundTripper = &retryablehttp.RoundTripper{Client: client} - if cfg.UserAgent != nil { + if serviceName := os.Getenv("OTEL_SERVICE_NAME"); serviceName != "" { transport = &addUserAgent{ RoundTripper: transport, - UserAgent: *cfg.UserAgent, + UserAgent: fmt.Sprintf("%s/%s", serviceName, version.Version), } } return &http.Client{