Skip to content

Commit 6f7f0e7

Browse files
authored
Set the RESOLVED_ACCOUNT_ID (T) user-agent metric only when accountID is actually resolved from credentials (#6472)
1 parent 0d5b796 commit 6f7f0e7

File tree

6 files changed

+56
-12
lines changed

6 files changed

+56
-12
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Set the RESOLVED_ACCOUNT_ID (T) user-agent metric only when accountID is actually resolved from credentials."
6+
}

codegen/src/main/java/software/amazon/awssdk/codegen/poet/rules/EndpointParamsKnowledgeIndex.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,11 @@ public MethodSpec resolveAndRecordAccountIdFromIdentityMethod() {
204204
builder.addStatement("$T accountId = accountIdFromIdentity(executionAttributes.getAttribute($T.SELECTED_AUTH_SCHEME))",
205205
String.class, SdkInternalExecutionAttribute.class);
206206

207-
builder.addStatement("executionAttributes.getAttribute($T.BUSINESS_METRICS).addMetric($T.RESOLVED_ACCOUNT_ID.value())",
208-
SdkInternalExecutionAttribute.class, BusinessMetricFeatureId.class);
207+
builder
208+
.beginControlFlow("if (accountId != null)")
209+
.addStatement("executionAttributes.getAttribute($T.BUSINESS_METRICS).addMetric($T.RESOLVED_ACCOUNT_ID.value())",
210+
SdkInternalExecutionAttribute.class, BusinessMetricFeatureId.class)
211+
.endControlFlow();
209212

210213
builder.addStatement("return accountId");
211214

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/rules/endpoint-resolve-interceptor-preSra.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,10 @@ private Supplier<Signer> signerProvider(EndpointAuthScheme authScheme) {
266266
private static String resolveAndRecordAccountIdFromIdentity(ExecutionAttributes executionAttributes) {
267267
String accountId = accountIdFromIdentity(executionAttributes
268268
.getAttribute(SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME));
269-
executionAttributes.getAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).addMetric(
270-
BusinessMetricFeatureId.RESOLVED_ACCOUNT_ID.value());
269+
if (accountId != null) {
270+
executionAttributes.getAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).addMetric(
271+
BusinessMetricFeatureId.RESOLVED_ACCOUNT_ID.value());
272+
}
271273
return accountId;
272274
}
273275

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/rules/endpoint-resolve-interceptor-with-endpointsbasedauth.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,10 @@ private static Optional<String> hostPrefix(String operationName, SdkRequest requ
242242
private static String resolveAndRecordAccountIdFromIdentity(ExecutionAttributes executionAttributes) {
243243
String accountId = accountIdFromIdentity(executionAttributes
244244
.getAttribute(SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME));
245-
executionAttributes.getAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).addMetric(
246-
BusinessMetricFeatureId.RESOLVED_ACCOUNT_ID.value());
245+
if (accountId != null) {
246+
executionAttributes.getAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).addMetric(
247+
BusinessMetricFeatureId.RESOLVED_ACCOUNT_ID.value());
248+
}
247249
return accountId;
248250
}
249251

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/rules/endpoint-resolve-interceptor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,10 @@ private static Optional<String> hostPrefix(String operationName, SdkRequest requ
240240
private static String resolveAndRecordAccountIdFromIdentity(ExecutionAttributes executionAttributes) {
241241
String accountId = accountIdFromIdentity(executionAttributes
242242
.getAttribute(SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME));
243-
executionAttributes.getAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).addMetric(
244-
BusinessMetricFeatureId.RESOLVED_ACCOUNT_ID.value());
243+
if (accountId != null) {
244+
executionAttributes.getAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).addMetric(
245+
BusinessMetricFeatureId.RESOLVED_ACCOUNT_ID.value());
246+
}
245247
return accountId;
246248
}
247249

test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/BusinessMetricsUserAgentTest.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.net.URI;
2323
import java.util.Arrays;
24+
import java.util.Collections;
2425
import java.util.List;
2526
import java.util.Map;
2627
import java.util.concurrent.CompletableFuture;
@@ -63,6 +64,12 @@ class BusinessMetricsUserAgentTest {
6364
private static final String USER_AGENT_HEADER_NAME = "User-Agent";
6465
private static final StaticCredentialsProvider CREDENTIALS_PROVIDER =
6566
StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid"));
67+
private static final StaticCredentialsProvider CREDENTIALS_PROVIDER_WITH_ACCOUNTID =
68+
StaticCredentialsProvider.create(
69+
AwsBasicCredentials.builder()
70+
.accessKeyId("akid").secretAccessKey("skid")
71+
.accountId("012345678901")
72+
.build());
6673

6774
@BeforeEach
6875
public void setup() {
@@ -76,10 +83,10 @@ public void cleanup() {
7683

7784
private static Stream<Arguments> inputValues() {
7885
return Stream.of(
79-
Arguments.of("Default values", null, Arrays.asList("D", "N", "P", "T")),
80-
Arguments.of("Account ID preferred mode ", AccountIdEndpointMode.PREFERRED, Arrays.asList("P", "T")),
81-
Arguments.of("Account ID disabled mode ", AccountIdEndpointMode.DISABLED, Arrays.asList("Q", "T")),
82-
Arguments.of("Account ID required mode ", AccountIdEndpointMode.REQUIRED, Arrays.asList("R", "T"))
86+
Arguments.of("Default values", null, Arrays.asList("D", "N", "P")),
87+
Arguments.of("Account ID preferred mode ", AccountIdEndpointMode.PREFERRED, Collections.singletonList("P")),
88+
Arguments.of("Account ID disabled mode ", AccountIdEndpointMode.DISABLED, Collections.singletonList("Q")),
89+
Arguments.of("Account ID required mode ", AccountIdEndpointMode.REQUIRED, Collections.singletonList("R"))
8390
);
8491
}
8592

@@ -101,6 +108,28 @@ void validate_metricsString_forDifferentConfigValues(String description,
101108
expectedMetrics.forEach(expectedMetric -> assertThat(userAgent).matches(METRIC_SEARCH_PATTERN.apply(expectedMetric)));
102109
}
103110

111+
@Test
112+
void when_accountIdNotResolved_noMetricIsAdded() {
113+
RestJsonEndpointProvidersAsyncClientBuilder clientBuilder = asyncClientBuilderForEndpointProvider();
114+
clientBuilder.credentialsProvider(CREDENTIALS_PROVIDER);
115+
116+
assertThatThrownBy(() -> clientBuilder.build().operationWithNoInputOrOutput(r -> {}).join()).hasMessageContaining("stop");
117+
118+
String userAgent = assertAndGetUserAgentString();
119+
assertThat(userAgent).doesNotMatch(METRIC_SEARCH_PATTERN.apply("T"));
120+
}
121+
122+
@Test
123+
void when_accountIdResolved_correctMetricIsAdded() {
124+
RestJsonEndpointProvidersAsyncClientBuilder clientBuilder = asyncClientBuilderForEndpointProvider();
125+
clientBuilder.credentialsProvider(CREDENTIALS_PROVIDER_WITH_ACCOUNTID);
126+
127+
assertThatThrownBy(() -> clientBuilder.build().operationWithNoInputOrOutput(r -> {}).join()).hasMessageContaining("stop");
128+
129+
String userAgent = assertAndGetUserAgentString();
130+
assertThat(userAgent).matches(METRIC_SEARCH_PATTERN.apply("T"));
131+
}
132+
104133
@Test
105134
void when_waiterIsUsed_correctMetricIsAdded() throws ExecutionException, InterruptedException {
106135
RestJsonWithWaitersAsyncClient asyncClient =

0 commit comments

Comments
 (0)