Skip to content

Commit

Permalink
Update user agent construction and deprecate old SdkUserAgent class
Browse files Browse the repository at this point in the history
  • Loading branch information
cenedhryn committed Sep 13, 2024
1 parent a7ddf54 commit ea8c30c
Show file tree
Hide file tree
Showing 19 changed files with 1,155 additions and 317 deletions.
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 @@ -18,8 +18,6 @@
import static software.amazon.awssdk.core.ClientType.ASYNC;
import static software.amazon.awssdk.core.ClientType.SYNC;
import static software.amazon.awssdk.core.client.config.SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR;
import static software.amazon.awssdk.core.client.config.SdkAdvancedClientOption.USER_AGENT_PREFIX;
import static software.amazon.awssdk.core.client.config.SdkAdvancedClientOption.USER_AGENT_SUFFIX;
import static software.amazon.awssdk.core.client.config.SdkClientOption.ADDITIONAL_HTTP_HEADERS;
import static software.amazon.awssdk.core.client.config.SdkClientOption.ASYNC_HTTP_CLIENT;
import static software.amazon.awssdk.core.client.config.SdkClientOption.CLIENT_TYPE;
Expand Down Expand Up @@ -48,6 +46,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 +73,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 +85,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 +112,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,8 +289,6 @@ 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_SUFFIX, "")
.option(CRC32_FROM_COMPRESSED_DATA_ENABLED, false)
.option(CONFIGURED_COMPRESSION_CONFIGURATION,
CompressionConfiguration.builder().build()));
Expand Down Expand Up @@ -384,7 +388,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 +406,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.putAttribute(RETRY_MODE, StringUtils.lowerCase(resolveRetryMode(config.get(RETRY_POLICY),
config.get(RETRY_STRATEGY))));
clientProperties.putAttribute(INTERNAL_METADATA_MARKER, StringUtils.trimToEmpty(config.get(INTERNAL_USER_AGENT)));
clientProperties.putAttribute(IO, StringUtils.lowerCase(resolvedClientType.name()));
clientProperties.putAttribute(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
Loading

0 comments on commit ea8c30c

Please sign in to comment.