Skip to content

Commit 2835f7b

Browse files
authored
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
1 parent ba4965d commit 2835f7b

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "c482c2c8-37a7-4c7e-9082-800b55a8bbf7",
3+
"type": "bugfix",
4+
"description": "Fix user agent to add business metrics at the end instead of prepend them",
5+
"modules": [
6+
"."
7+
]
8+
}

aws/middleware/user_agent.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const (
3434
FeatureMetadata2
3535
)
3636

37+
// Hardcoded value to specify which version of the user agent we're using
38+
const uaMetadata = "ua/2.1"
39+
3740
func (k SDKAgentKeyType) string() string {
3841
switch k {
3942
case APIMetadata:
@@ -107,6 +110,7 @@ type RequestUserAgent struct {
107110
func NewRequestUserAgent() *RequestUserAgent {
108111
userAgent, sdkAgent := smithyhttp.NewUserAgentBuilder(), smithyhttp.NewUserAgentBuilder()
109112
addProductName(userAgent)
113+
addUserAgentMetadata(userAgent)
110114
addProductName(sdkAgent)
111115

112116
r := &RequestUserAgent{
@@ -134,6 +138,10 @@ func addProductName(builder *smithyhttp.UserAgentBuilder) {
134138
builder.AddKeyValue(aws.SDKName, aws.SDKVersion)
135139
}
136140

141+
func addUserAgentMetadata(builder *smithyhttp.UserAgentBuilder) {
142+
builder.AddKey(uaMetadata)
143+
}
144+
137145
// AddUserAgentKey retrieves a requestUserAgent from the provided stack, or initializes one.
138146
func AddUserAgentKey(key string) func(*middleware.Stack) error {
139147
return func(stack *middleware.Stack) error {
@@ -258,10 +266,10 @@ func (u *RequestUserAgent) HandleBuild(ctx context.Context, in middleware.BuildI
258266

259267
func (u *RequestUserAgent) addHTTPUserAgent(request *smithyhttp.Request) {
260268
const userAgent = "User-Agent"
261-
updateHTTPHeader(request, userAgent, u.userAgent.Build())
262269
if len(u.features) > 0 {
263270
updateHTTPHeader(request, userAgent, buildFeatureMetrics(u.features))
264271
}
272+
updateHTTPHeader(request, userAgent, u.userAgent.Build())
265273
}
266274

267275
func (u *RequestUserAgent) addHTTPSDKAgent(request *smithyhttp.Request) {

aws/middleware/user_agent_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
)
1717

1818
var expectedAgent = aws.SDKName + "/" + aws.SDKVersion +
19+
" ua/2.1" +
1920
" os/" + getNormalizedOSName() +
2021
" lang/go#" + strings.Map(rules, languageVersion) + // normalize as the user-agent builder will
2122
" md/GOOS#" + runtime.GOOS +
@@ -251,15 +252,15 @@ func TestAddUserAgentFeature(t *testing.T) {
251252
Features: []UserAgentFeature{
252253
UserAgentFeatureWaiter,
253254
},
254-
Expect: "m/B " + expectedAgent,
255+
Expect: expectedAgent + " " + "m/B",
255256
},
256257
"two": {
257258
Features: []UserAgentFeature{
258259
UserAgentFeatureRetryModeAdaptive, // ensure stable order, and idempotent
259260
UserAgentFeatureRetryModeAdaptive,
260261
UserAgentFeatureWaiter,
261262
},
262-
Expect: "m/B,F " + expectedAgent,
263+
Expect: expectedAgent + " " + "m/B,F",
263264
},
264265
}
265266

@@ -403,7 +404,7 @@ func TestAddSDKAgentKeyValue(t *testing.T) {
403404
t.Fatalf("expect User-Agent to be present")
404405
}
405406
if ua[0] != c.Expect {
406-
t.Errorf("User-Agent: %q != %q", c.Expect, ua[0])
407+
t.Errorf("User-Agent: expected %q != actual %q", c.Expect, ua[0])
407408
}
408409
})
409410
}

0 commit comments

Comments
 (0)