From 2835f7bb8d4925acb36a5ab7813cca6ac8977cb7 Mon Sep 17 00:00:00 2001 From: Luis Madrigal <599908+Madrigal@users.noreply.github.com> Date: Fri, 29 Nov 2024 16:57:37 -0500 Subject: [PATCH] Fix user agent to add business metrics at the end instead of prepend them (#2916) * Fix user agent to add business metrics at the end instead of prepend them --- .changelog/c482c2c837a74c7e9082800b55a8bbf7.json | 8 ++++++++ aws/middleware/user_agent.go | 10 +++++++++- aws/middleware/user_agent_test.go | 7 ++++--- 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 .changelog/c482c2c837a74c7e9082800b55a8bbf7.json diff --git a/.changelog/c482c2c837a74c7e9082800b55a8bbf7.json b/.changelog/c482c2c837a74c7e9082800b55a8bbf7.json new file mode 100644 index 00000000000..4b2c993b330 --- /dev/null +++ b/.changelog/c482c2c837a74c7e9082800b55a8bbf7.json @@ -0,0 +1,8 @@ +{ + "id": "c482c2c8-37a7-4c7e-9082-800b55a8bbf7", + "type": "bugfix", + "description": "Fix user agent to add business metrics at the end instead of prepend them", + "modules": [ + "." + ] +} \ No newline at end of file diff --git a/aws/middleware/user_agent.go b/aws/middleware/user_agent.go index 67aaa02265d..ab4e619073a 100644 --- a/aws/middleware/user_agent.go +++ b/aws/middleware/user_agent.go @@ -34,6 +34,9 @@ const ( FeatureMetadata2 ) +// Hardcoded value to specify which version of the user agent we're using +const uaMetadata = "ua/2.1" + func (k SDKAgentKeyType) string() string { switch k { case APIMetadata: @@ -107,6 +110,7 @@ type RequestUserAgent struct { func NewRequestUserAgent() *RequestUserAgent { userAgent, sdkAgent := smithyhttp.NewUserAgentBuilder(), smithyhttp.NewUserAgentBuilder() addProductName(userAgent) + addUserAgentMetadata(userAgent) addProductName(sdkAgent) r := &RequestUserAgent{ @@ -134,6 +138,10 @@ func addProductName(builder *smithyhttp.UserAgentBuilder) { builder.AddKeyValue(aws.SDKName, aws.SDKVersion) } +func addUserAgentMetadata(builder *smithyhttp.UserAgentBuilder) { + builder.AddKey(uaMetadata) +} + // AddUserAgentKey retrieves a requestUserAgent from the provided stack, or initializes one. func AddUserAgentKey(key string) func(*middleware.Stack) error { return func(stack *middleware.Stack) error { @@ -258,10 +266,10 @@ func (u *RequestUserAgent) HandleBuild(ctx context.Context, in middleware.BuildI func (u *RequestUserAgent) addHTTPUserAgent(request *smithyhttp.Request) { const userAgent = "User-Agent" - updateHTTPHeader(request, userAgent, u.userAgent.Build()) if len(u.features) > 0 { updateHTTPHeader(request, userAgent, buildFeatureMetrics(u.features)) } + updateHTTPHeader(request, userAgent, u.userAgent.Build()) } func (u *RequestUserAgent) addHTTPSDKAgent(request *smithyhttp.Request) { diff --git a/aws/middleware/user_agent_test.go b/aws/middleware/user_agent_test.go index 12f7bcb43ae..3cbfa7dffc5 100644 --- a/aws/middleware/user_agent_test.go +++ b/aws/middleware/user_agent_test.go @@ -16,6 +16,7 @@ import ( ) var expectedAgent = aws.SDKName + "/" + aws.SDKVersion + + " ua/2.1" + " os/" + getNormalizedOSName() + " lang/go#" + strings.Map(rules, languageVersion) + // normalize as the user-agent builder will " md/GOOS#" + runtime.GOOS + @@ -251,7 +252,7 @@ func TestAddUserAgentFeature(t *testing.T) { Features: []UserAgentFeature{ UserAgentFeatureWaiter, }, - Expect: "m/B " + expectedAgent, + Expect: expectedAgent + " " + "m/B", }, "two": { Features: []UserAgentFeature{ @@ -259,7 +260,7 @@ func TestAddUserAgentFeature(t *testing.T) { UserAgentFeatureRetryModeAdaptive, UserAgentFeatureWaiter, }, - Expect: "m/B,F " + expectedAgent, + Expect: expectedAgent + " " + "m/B,F", }, } @@ -403,7 +404,7 @@ func TestAddSDKAgentKeyValue(t *testing.T) { t.Fatalf("expect User-Agent to be present") } if ua[0] != c.Expect { - t.Errorf("User-Agent: %q != %q", c.Expect, ua[0]) + t.Errorf("User-Agent: expected %q != actual %q", c.Expect, ua[0]) } }) }