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

Update user agent construction and deprecate old SdkUserAgent class #5596

Merged
merged 7 commits into from
Sep 20, 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
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-e846f47.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Refactoring the user agent string format to be more consistent across SDKs"
}
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
<Class name="software.amazon.awssdk.codegen.model.service.Waiters"/>
<Class name="software.amazon.awssdk.regions.EndpointTag"/>
<Class name="software.amazon.awssdk.regions.Region"/>
<Class name="software.amazon.awssdk.core.internal.useragent.DefaultSystemUserAgent"/>
<Class name="software.amazon.awssdk.core.util.DefaultSdkAutoConstructList"/>
<Class name="software.amazon.awssdk.core.util.DefaultSdkAutoConstructMap"/>
<Class name="software.amazon.awssdk.http.nio.netty.internal.http2.FlushOnReadHandler"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.net.URI;
import java.time.Duration;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.util.SdkUserAgent;
import software.amazon.awssdk.core.util.SystemUserAgent;
import software.amazon.awssdk.http.SdkHttpFullRequest;
import software.amazon.awssdk.http.SdkHttpMethod;

Expand Down Expand Up @@ -69,7 +69,7 @@ public SdkHttpFullRequest createDataRequest(String path, String token, Duration

private SdkHttpFullRequest.Builder defaulttHttpBuilder() {
return SdkHttpFullRequest.builder()
.putHeader(USER_AGENT, SdkUserAgent.create().userAgent())
.putHeader(USER_AGENT, SystemUserAgent.getOrCreate().userAgentString())
.putHeader(ACCEPT, "*/*")
.putHeader(CONNECTION, "keep-alive");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
import static software.amazon.awssdk.core.client.config.SdkClientOption.RETRY_STRATEGY;
import static software.amazon.awssdk.core.client.config.SdkClientOption.SCHEDULED_EXECUTOR_SERVICE;
import static software.amazon.awssdk.core.client.config.SdkClientOption.SYNC_HTTP_CLIENT;
import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.HTTP;
import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.INTERNAL_METADATA_MARKER;
import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.IO;
import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.RETRY_MODE;
import static software.amazon.awssdk.utils.CollectionUtils.mergeLists;
import static software.amazon.awssdk.utils.Validate.paramNotNull;

Expand All @@ -71,6 +75,7 @@
import software.amazon.awssdk.annotations.SdkProtectedApi;
import software.amazon.awssdk.annotations.SdkTestInternalApi;
import software.amazon.awssdk.core.ClientEndpointProvider;
import software.amazon.awssdk.core.ClientType;
import software.amazon.awssdk.core.CompressionConfiguration;
import software.amazon.awssdk.core.SdkPlugin;
import software.amazon.awssdk.core.SdkSystemSetting;
Expand All @@ -82,13 +87,14 @@
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.core.internal.http.loader.DefaultSdkAsyncHttpClientBuilder;
import software.amazon.awssdk.core.internal.http.loader.DefaultSdkHttpClientBuilder;
import software.amazon.awssdk.core.internal.http.pipeline.stages.ApplyUserAgentStage;
import software.amazon.awssdk.core.internal.http.pipeline.stages.CompressRequestStage;
import software.amazon.awssdk.core.internal.interceptor.HttpChecksumValidationInterceptor;
import software.amazon.awssdk.core.internal.retry.SdkDefaultRetryStrategy;
import software.amazon.awssdk.core.internal.useragent.SdkClientUserAgentProperties;
import software.amazon.awssdk.core.internal.useragent.SdkUserAgentBuilder;
import software.amazon.awssdk.core.retry.RetryMode;
import software.amazon.awssdk.core.retry.RetryPolicy;
import software.amazon.awssdk.core.util.SdkUserAgent;
import software.amazon.awssdk.core.util.SystemUserAgent;
import software.amazon.awssdk.http.ExecutableHttpRequest;
import software.amazon.awssdk.http.HttpExecuteRequest;
import software.amazon.awssdk.http.SdkHttpClient;
Expand All @@ -108,8 +114,10 @@
import software.amazon.awssdk.utils.Either;
import software.amazon.awssdk.utils.Lazy;
import software.amazon.awssdk.utils.OptionalUtils;
import software.amazon.awssdk.utils.StringUtils;
import software.amazon.awssdk.utils.ThreadFactoryBuilder;
import software.amazon.awssdk.utils.Validate;
import software.amazon.awssdk.utils.http.SdkHttpUtils;

/**
* An SDK-internal implementation of the methods in {@link SdkClientBuilder}, {@link SdkAsyncClientBuilder} and
Expand Down Expand Up @@ -283,7 +291,7 @@ private SdkClientConfiguration mergeGlobalDefaults(SdkClientConfiguration config
.lazyOption(PROFILE_FILE, conf -> conf.get(PROFILE_FILE_SUPPLIER).get())
.option(PROFILE_NAME,
ProfileFileSystemSetting.AWS_PROFILE.getStringValueOrThrow())
.option(USER_AGENT_PREFIX, SdkUserAgent.create().userAgent())
.option(USER_AGENT_PREFIX, "")
.option(USER_AGENT_SUFFIX, "")
.option(CRC32_FROM_COMPRESSED_DATA_ENABLED, false)
.option(CONFIGURED_COMPRESSION_CONFIGURATION,
Expand Down Expand Up @@ -384,7 +392,8 @@ protected SdkClientConfiguration invokePlugins(SdkClientConfiguration config) {
return config;
}

private String resolveRetryMode(RetryPolicy retryPolicy, RetryStrategy retryStrategy) {
//TODO (useragent): Refactor this as part of moving value to business metrics (UA 2.1)
private static String resolveRetryMode(RetryPolicy retryPolicy, RetryStrategy retryStrategy) {
if (retryPolicy != null) {
return retryPolicy.retryMode().toString();
}
Expand All @@ -401,13 +410,32 @@ private String resolveRetryMode(RetryPolicy retryPolicy, RetryStrategy retryStra
}

private String resolveClientUserAgent(LazyValueSource config) {
String retryMode = resolveRetryMode(config.get(RETRY_POLICY), config.get(RETRY_STRATEGY));
return ApplyUserAgentStage.resolveClientUserAgent(config.get(USER_AGENT_PREFIX),
config.get(INTERNAL_USER_AGENT),
config.get(CLIENT_TYPE),
config.get(SYNC_HTTP_CLIENT),
config.get(ASYNC_HTTP_CLIENT),
retryMode);
SdkClientUserAgentProperties clientProperties = new SdkClientUserAgentProperties();

ClientType clientType = config.get(CLIENT_TYPE);
ClientType resolvedClientType = clientType == null ? ClientType.UNKNOWN : config.get(CLIENT_TYPE);

clientProperties.putProperty(RETRY_MODE, StringUtils.lowerCase(resolveRetryMode(config.get(RETRY_POLICY),
config.get(RETRY_STRATEGY))));
clientProperties.putProperty(INTERNAL_METADATA_MARKER, StringUtils.trimToEmpty(config.get(INTERNAL_USER_AGENT)));
clientProperties.putProperty(IO, StringUtils.lowerCase(resolvedClientType.name()));
clientProperties.putProperty(HTTP, SdkHttpUtils.urlEncode(clientName(resolvedClientType,
config.get(SYNC_HTTP_CLIENT),
config.get(ASYNC_HTTP_CLIENT))));

return SdkUserAgentBuilder.buildClientUserAgentString(SystemUserAgent.getOrCreate(), clientProperties);
}

private static String clientName(ClientType clientType, SdkHttpClient syncHttpClient, SdkAsyncHttpClient asyncHttpClient) {
if (clientType == SYNC) {
return syncHttpClient == null ? "null" : syncHttpClient.clientName();
}

if (clientType == ASYNC) {
return asyncHttpClient == null ? "null" : asyncHttpClient.clientName();
}

return ClientType.UNKNOWN.name();
}

private RetryStrategy resolveRetryStrategy(LazyValueSource config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import software.amazon.awssdk.core.ServiceConfiguration;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.core.internal.useragent.SdkClientUserAgentProperties;
import software.amazon.awssdk.core.retry.RetryMode;
import software.amazon.awssdk.core.retry.RetryPolicy;
import software.amazon.awssdk.endpoints.EndpointProvider;
Expand Down Expand Up @@ -266,6 +267,8 @@ public final class SdkClientOption<T> extends ClientOption<T> {

/**
* A user agent prefix that is specific to the client (agnostic of the request).
*
* Not currently in use, since the introduction of {@link SdkClientUserAgentProperties}
*/
public static final SdkClientOption<String> CLIENT_USER_AGENT = new SdkClientOption<>(String.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ private static void validateClientOptions(SdkClientConfiguration c) {
require("overrideConfiguration.additionalHttpHeaders", c.option(SdkClientOption.ADDITIONAL_HTTP_HEADERS));
require("overrideConfiguration.executionInterceptors", c.option(SdkClientOption.EXECUTION_INTERCEPTORS));
require("overrideConfiguration.retryStrategy", c.option(SdkClientOption.RETRY_STRATEGY));

require("overrideConfiguration.advancedOption[USER_AGENT_PREFIX]",
c.option(SdkAdvancedClientOption.USER_AGENT_PREFIX));
require("overrideConfiguration.advancedOption[USER_AGENT_SUFFIX]",
Expand Down
Loading
Loading