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

Adding endpoint and account ID business metrics values to user agent … #5699

Merged
merged 4 commits into from
Nov 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import java.util.EnumMap;
import java.util.Map;
import java.util.Optional;
import javax.lang.model.element.Modifier;
import software.amazon.awssdk.awscore.AwsExecutionAttribute;
import software.amazon.awssdk.awscore.client.config.AwsClientOption;
import software.amazon.awssdk.awscore.endpoints.AccountIdEndpointMode;
import software.amazon.awssdk.awscore.endpoints.AccountIdEndpointModeResolver;
import software.amazon.awssdk.awscore.internal.useragent.BusinessMetricsUtils;
import software.amazon.awssdk.codegen.internal.Utils;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.internal.LocalParameter;
Expand All @@ -39,6 +42,9 @@
import software.amazon.awssdk.core.SelectedAuthScheme;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;
import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId;
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
import software.amazon.awssdk.identity.spi.Identity;
import software.amazon.awssdk.utils.CompletableFutureUtils;
Expand Down Expand Up @@ -163,11 +169,51 @@ private Map<BuiltInParameter, LocalParameter> builtInsForClientBuilder(Map<Strin
return actualParams;
}

public Optional<MethodSpec> accountIdFromIdentityMethod() {
public void addAccountIdMethodsIfPresent(TypeSpec.Builder b) {
if (!hasAccountIdEndpointModeBuiltIn()) {
return Optional.empty();
return;
}

b.addMethod(resolveAndRecordAccountIdFromIdentityMethod());
b.addMethod(accountIdFromIdentityMethod());
b.addMethod(recordAccountIdEndpointModeMethod());
}

public MethodSpec recordAccountIdEndpointModeMethod() {
MethodSpec.Builder builder = MethodSpec.methodBuilder("recordAccountIdEndpointMode")
.addModifiers(PRIVATE, STATIC)
.addParameter(ExecutionAttributes.class, "executionAttributes")
.returns(String.class);
builder.addStatement("$T mode = executionAttributes.getAttribute($T.AWS_AUTH_ACCOUNT_ID_ENDPOINT_MODE)",
AccountIdEndpointMode.class, AwsExecutionAttribute.class);

builder.addStatement("$T.resolveAccountIdEndpointModeMetric(mode)"
+ ".ifPresent(m -> executionAttributes.getAttribute($T.BUSINESS_METRICS).addMetric(m))",
BusinessMetricsUtils.class, SdkInternalExecutionAttribute.class);

builder.addStatement("return mode.name().toLowerCase()");

return builder.build();
}

public MethodSpec resolveAndRecordAccountIdFromIdentityMethod() {
MethodSpec.Builder builder = MethodSpec.methodBuilder("resolveAndRecordAccountIdFromIdentity")
.addModifiers(PRIVATE, STATIC)
.addParameter(ExecutionAttributes.class, "executionAttributes")
.returns(String.class);
builder.addStatement("$T accountId = accountIdFromIdentity(executionAttributes.getAttribute($T.SELECTED_AUTH_SCHEME))",
String.class, SdkInternalExecutionAttribute.class);

builder.addStatement("executionAttributes.getAttribute($T.BUSINESS_METRICS).addMetric($T.RESOLVED_ACCOUNT_ID.value())",
SdkInternalExecutionAttribute.class, BusinessMetricFeatureId.class);

builder.addStatement("return accountId");

return builder.build();
}


public MethodSpec accountIdFromIdentityMethod() {
ParameterizedTypeName paramType = ParameterizedTypeName.get(ClassName.get(SelectedAuthScheme.class),
TypeVariableName.get("T"));

Expand All @@ -184,6 +230,6 @@ public Optional<MethodSpec> accountIdFromIdentityMethod() {
builder.addStatement("accountId = (($T) identity).accountId().orElse(null)", AwsCredentialsIdentity.class);
builder.endControlFlow();
builder.addStatement("return accountId");
return Optional.of(builder.build());
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public TypeSpec poetSpec() {
b.addMethod(signerProviderMethod());
}

endpointParamsKnowledgeIndex.accountIdFromIdentityMethod().ifPresent(b::addMethod);
endpointParamsKnowledgeIndex.addAccountIdMethodsIfPresent(b);
return b.build();
}

Expand Down Expand Up @@ -303,13 +303,10 @@ private MethodSpec ruleParams() {
b.addStatement(endpointProviderUtilsSetter("endpointBuiltIn", setter));
break;
case AWS_AUTH_ACCOUNT_ID:
b.addStatement("builder.$N(accountIdFromIdentity(executionAttributes.getAttribute($T.SELECTED_AUTH_SCHEME)))",
setter, SdkInternalExecutionAttribute.class);
b.addStatement("builder.$N(resolveAndRecordAccountIdFromIdentity(executionAttributes))", setter);
break;
case AWS_AUTH_ACCOUNT_ID_ENDPOINT_MODE:
b.addStatement("builder.$N(executionAttributes.getAttribute($T.$N).name().toLowerCase())",
setter, AwsExecutionAttribute.class,
model.getNamingStrategy().getEnumValueName(m.getBuiltInEnum().name()));
b.addStatement("builder.$N(recordAccountIdEndpointMode(executionAttributes))", setter);
break;
case AWS_S3_USE_GLOBAL_ENDPOINT:
b.addStatement("builder.$N(executionAttributes.getAttribute($T.$N))",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute;
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;
import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId;
import software.amazon.awssdk.endpoints.Endpoint;
import software.amazon.awssdk.http.SdkHttpRequest;
import software.amazon.awssdk.regions.Region;
Expand Down Expand Up @@ -43,6 +44,8 @@ public final class AwsEndpointProviderUtils {
*/
public static String endpointBuiltIn(ExecutionAttributes executionAttributes) {
if (endpointIsOverridden(executionAttributes)) {
executionAttributes.getOptionalAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).ifPresent(
metric -> metric.addMetric(BusinessMetricFeatureId.ENDPOINT_OVERRIDE.value()));
return invokeSafely(() -> {
URI endpointOverride = executionAttributes.getAttribute(SdkInternalExecutionAttribute.CLIENT_ENDPOINT_PROVIDER)
.clientEndpoint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import software.amazon.awssdk.auth.signer.Aws4Signer;
import software.amazon.awssdk.auth.signer.SignerLoader;
import software.amazon.awssdk.awscore.AwsExecutionAttribute;
import software.amazon.awssdk.awscore.endpoints.AccountIdEndpointMode;
import software.amazon.awssdk.awscore.endpoints.AwsEndpointAttribute;
import software.amazon.awssdk.awscore.endpoints.authscheme.EndpointAuthScheme;
import software.amazon.awssdk.awscore.endpoints.authscheme.SigV4AuthScheme;
import software.amazon.awssdk.awscore.endpoints.authscheme.SigV4aAuthScheme;
import software.amazon.awssdk.awscore.internal.useragent.BusinessMetricsUtils;
import software.amazon.awssdk.awscore.util.SignerOverrideUtils;
import software.amazon.awssdk.core.SdkRequest;
import software.amazon.awssdk.core.SelectedAuthScheme;
Expand All @@ -25,6 +27,7 @@
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;
import software.amazon.awssdk.core.metrics.CoreMetric;
import software.amazon.awssdk.core.signer.Signer;
import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId;
import software.amazon.awssdk.endpoints.Endpoint;
import software.amazon.awssdk.http.SdkHttpRequest;
import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner;
Expand Down Expand Up @@ -118,10 +121,8 @@ public static QueryEndpointParams ruleParams(SdkRequest request, ExecutionAttrib
builder.region(AwsEndpointProviderUtils.regionBuiltIn(executionAttributes));
builder.useDualStackEndpoint(AwsEndpointProviderUtils.dualStackEnabledBuiltIn(executionAttributes));
builder.useFipsEndpoint(AwsEndpointProviderUtils.fipsEnabledBuiltIn(executionAttributes));
builder.accountId(accountIdFromIdentity(executionAttributes
.getAttribute(SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME)));
builder.accountIdEndpointMode(executionAttributes.getAttribute(AwsExecutionAttribute.AWS_AUTH_ACCOUNT_ID_ENDPOINT_MODE)
.name().toLowerCase());
builder.accountId(resolveAndRecordAccountIdFromIdentity(executionAttributes));
builder.accountIdEndpointMode(recordAccountIdEndpointMode(executionAttributes));
setClientContextParams(builder, executionAttributes);
setContextParams(builder, executionAttributes.getAttribute(AwsExecutionAttribute.OPERATION_NAME), request);
setStaticContextParams(builder, executionAttributes.getAttribute(AwsExecutionAttribute.OPERATION_NAME));
Expand Down Expand Up @@ -249,6 +250,14 @@ private Supplier<Signer> signerProvider(EndpointAuthScheme authScheme) {
throw SdkClientException.create("Don't know how to create signer for auth scheme: " + authScheme.name());
}

private static String resolveAndRecordAccountIdFromIdentity(ExecutionAttributes executionAttributes) {
String accountId = accountIdFromIdentity(executionAttributes
.getAttribute(SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME));
executionAttributes.getAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).addMetric(
BusinessMetricFeatureId.RESOLVED_ACCOUNT_ID.value());
return accountId;
}

private static <T extends Identity> String accountIdFromIdentity(SelectedAuthScheme<T> selectedAuthScheme) {
T identity = CompletableFutureUtils.joinLikeSync(selectedAuthScheme.identity());
String accountId = null;
Expand All @@ -257,4 +266,11 @@ private static <T extends Identity> String accountIdFromIdentity(SelectedAuthSch
}
return accountId;
}

private static String recordAccountIdEndpointMode(ExecutionAttributes executionAttributes) {
AccountIdEndpointMode mode = executionAttributes.getAttribute(AwsExecutionAttribute.AWS_AUTH_ACCOUNT_ID_ENDPOINT_MODE);
BusinessMetricsUtils.resolveAccountIdEndpointModeMetric(mode).ifPresent(
m -> executionAttributes.getAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).addMetric(m));
return mode.name().toLowerCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.awscore.AwsExecutionAttribute;
import software.amazon.awssdk.awscore.endpoints.AccountIdEndpointMode;
import software.amazon.awssdk.awscore.endpoints.AwsEndpointAttribute;
import software.amazon.awssdk.awscore.endpoints.authscheme.EndpointAuthScheme;
import software.amazon.awssdk.awscore.endpoints.authscheme.SigV4AuthScheme;
import software.amazon.awssdk.awscore.endpoints.authscheme.SigV4aAuthScheme;
import software.amazon.awssdk.awscore.internal.useragent.BusinessMetricsUtils;
import software.amazon.awssdk.core.SdkRequest;
import software.amazon.awssdk.core.SelectedAuthScheme;
import software.amazon.awssdk.core.exception.SdkClientException;
Expand All @@ -20,6 +22,7 @@
import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute;
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;
import software.amazon.awssdk.core.metrics.CoreMetric;
import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId;
import software.amazon.awssdk.endpoints.Endpoint;
import software.amazon.awssdk.http.SdkHttpRequest;
import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner;
Expand Down Expand Up @@ -101,10 +104,8 @@ public static QueryEndpointParams ruleParams(SdkRequest request, ExecutionAttrib
builder.region(AwsEndpointProviderUtils.regionBuiltIn(executionAttributes));
builder.useDualStackEndpoint(AwsEndpointProviderUtils.dualStackEnabledBuiltIn(executionAttributes));
builder.useFipsEndpoint(AwsEndpointProviderUtils.fipsEnabledBuiltIn(executionAttributes));
builder.accountId(accountIdFromIdentity(executionAttributes
.getAttribute(SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME)));
builder.accountIdEndpointMode(executionAttributes.getAttribute(AwsExecutionAttribute.AWS_AUTH_ACCOUNT_ID_ENDPOINT_MODE)
.name().toLowerCase());
builder.accountId(resolveAndRecordAccountIdFromIdentity(executionAttributes));
builder.accountIdEndpointMode(recordAccountIdEndpointMode(executionAttributes));
setClientContextParams(builder, executionAttributes);
setContextParams(builder, executionAttributes.getAttribute(AwsExecutionAttribute.OPERATION_NAME), request);
setStaticContextParams(builder, executionAttributes.getAttribute(AwsExecutionAttribute.OPERATION_NAME));
Expand Down Expand Up @@ -223,6 +224,14 @@ private static Optional<String> hostPrefix(String operationName, SdkRequest requ
}
}

private static String resolveAndRecordAccountIdFromIdentity(ExecutionAttributes executionAttributes) {
String accountId = accountIdFromIdentity(executionAttributes
.getAttribute(SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME));
executionAttributes.getAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).addMetric(
BusinessMetricFeatureId.RESOLVED_ACCOUNT_ID.value());
return accountId;
}

private static <T extends Identity> String accountIdFromIdentity(SelectedAuthScheme<T> selectedAuthScheme) {
T identity = CompletableFutureUtils.joinLikeSync(selectedAuthScheme.identity());
String accountId = null;
Expand All @@ -231,4 +240,11 @@ private static <T extends Identity> String accountIdFromIdentity(SelectedAuthSch
}
return accountId;
}

private static String recordAccountIdEndpointMode(ExecutionAttributes executionAttributes) {
AccountIdEndpointMode mode = executionAttributes.getAttribute(AwsExecutionAttribute.AWS_AUTH_ACCOUNT_ID_ENDPOINT_MODE);
BusinessMetricsUtils.resolveAccountIdEndpointModeMetric(mode).ifPresent(
m -> executionAttributes.getAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS).addMetric(m));
return mode.name().toLowerCase();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.awscore.internal.useragent;

import java.util.Optional;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.awscore.endpoints.AccountIdEndpointMode;
import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId;

@SdkInternalApi
public final class BusinessMetricsUtils {
private BusinessMetricsUtils() {
}

public static Optional<String> resolveAccountIdEndpointModeMetric(AccountIdEndpointMode accountIdEndpointMode) {
if (accountIdEndpointMode == AccountIdEndpointMode.PREFERRED) {
return Optional.of(BusinessMetricFeatureId.ACCOUNT_ID_MODE_PREFERRED.value());
}
if (accountIdEndpointMode == AccountIdEndpointMode.REQUIRED) {
return Optional.of(BusinessMetricFeatureId.ACCOUNT_ID_MODE_REQUIRED.value());
}
if (accountIdEndpointMode == AccountIdEndpointMode.DISABLED) {
return Optional.of(BusinessMetricFeatureId.ACCOUNT_ID_MODE_DISABLED.value());
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* An enum class representing a short form of identity providers to record in the UA string.
*
* Unimplemented metrics: I,J,K,M-c,e-[latest]
* Unimplemented metrics: I,J,K,M,O,S,U-c,e-[latest]
* Unsupported metrics (these will never be added): A,H
*/
@SdkProtectedApi
Expand All @@ -35,6 +35,11 @@ public enum BusinessMetricFeatureId {
RETRY_MODE_ADAPTIVE("F"),
S3_TRANSFER("G"),
GZIP_REQUEST_COMPRESSION("L"), //TODO(metrics): Not working, compression happens after header
ENDPOINT_OVERRIDE("N"),
ACCOUNT_ID_MODE_PREFERRED("P"),
ACCOUNT_ID_MODE_DISABLED("Q"),
ACCOUNT_ID_MODE_REQUIRED("R"),
RESOLVED_ACCOUNT_ID("T"),
DDB_MAPPER("d"),
UNKNOWN("Unknown");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@
import org.junit.Test;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.ClientEndpointProvider;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;
import software.amazon.awssdk.core.useragent.BusinessMetricCollection;
import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.endpoints.internal.AwsEndpointProviderUtils;
import software.amazon.awssdk.services.dynamodb.paginators.QueryPublisher;

public class PaginatorInUserAgentTest {
Expand Down Expand Up @@ -78,6 +83,19 @@ public void syncPaginator_shouldHavePaginatorUserAgent() throws IOException {
matching(METRIC_SEARCH_PATTERN.apply(BusinessMetricFeatureId.PAGINATOR.value()))));
}

@Test
public void syncPaginator_shuldHavePaginatorUserAgent() throws IOException {
ExecutionAttributes executionAttributes = new ExecutionAttributes();
BusinessMetricCollection newmetrics = new BusinessMetricCollection();
newmetrics.addMetric("R");

ClientEndpointProvider wohoo = ClientEndpointProvider.forEndpointOverride(URI.create("http://wohoo"));
executionAttributes.putAttribute(SdkInternalExecutionAttribute.BUSINESS_METRICS, newmetrics);
executionAttributes.putAttribute(SdkInternalExecutionAttribute.CLIENT_ENDPOINT_PROVIDER, wohoo);
String s = AwsEndpointProviderUtils.endpointBuiltIn(executionAttributes);
System.out.println(s);
}

@Test
public void asyncPaginator_shouldHavePaginatorUserAgent() throws IOException {
stubFor(any(urlEqualTo("/"))
Expand Down
Loading
Loading