Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consistent userAgent for AWS SDK and HTTP #291

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading