Skip to content

Commit

Permalink
Fix user agent to add business metrics at the end instead of prepend …
Browse files Browse the repository at this point in the history
…them (#2916)

* Fix user agent to add business metrics at the end instead of prepend them
  • Loading branch information
Madrigal authored Nov 29, 2024
1 parent ba4965d commit 2835f7b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .changelog/c482c2c837a74c7e9082800b55a8bbf7.json
Original file line number Diff line number Diff line change
@@ -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": [
"."
]
}
10 changes: 9 additions & 1 deletion aws/middleware/user_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -107,6 +110,7 @@ type RequestUserAgent struct {
func NewRequestUserAgent() *RequestUserAgent {
userAgent, sdkAgent := smithyhttp.NewUserAgentBuilder(), smithyhttp.NewUserAgentBuilder()
addProductName(userAgent)
addUserAgentMetadata(userAgent)
addProductName(sdkAgent)

r := &RequestUserAgent{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions aws/middleware/user_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand Down Expand Up @@ -251,15 +252,15 @@ func TestAddUserAgentFeature(t *testing.T) {
Features: []UserAgentFeature{
UserAgentFeatureWaiter,
},
Expect: "m/B " + expectedAgent,
Expect: expectedAgent + " " + "m/B",
},
"two": {
Features: []UserAgentFeature{
UserAgentFeatureRetryModeAdaptive, // ensure stable order, and idempotent
UserAgentFeatureRetryModeAdaptive,
UserAgentFeatureWaiter,
},
Expect: "m/B,F " + expectedAgent,
Expect: expectedAgent + " " + "m/B,F",
},
}

Expand Down Expand Up @@ -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])
}
})
}
Expand Down

0 comments on commit 2835f7b

Please sign in to comment.