Skip to content

Commit

Permalink
fix: consistent userAgent for AWS SDK and HTTP (#291)
Browse files Browse the repository at this point in the history
We should set user agent as part of our tracing package, rather than
relying on each top level entry point for a handler to set it up. We
will rely on the OTEL service name, which is consistently set as part of
each lambda config.
  • Loading branch information
jta authored May 31, 2024
1 parent a6f9f34 commit fafdf55
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 0 additions & 2 deletions cmd/forwarder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,13 @@ 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
11 changes: 11 additions & 0 deletions tracing/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@ 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) {
awsCfg, err := config.LoadDefaultConfig(ctx)
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
}
9 changes: 6 additions & 3 deletions tracing/http.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit fafdf55

Please sign in to comment.