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 user agent to add business metrics at the end instead of prepend them #2916

Merged
merged 2 commits into from
Nov 29, 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
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