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

mgmt, update user-agent #13085

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions sdk/management/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Azure Management Libraries require a `TokenCredential` implementation for authen
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.0-beta.5</version>
<version>1.0.8</version>
</dependency>
```
[//]: # ({x-version-update-end})
Expand All @@ -58,7 +58,7 @@ Azure Management Libraries require a `TokenCredential` implementation for authen
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-netty</artifactId>
<version>1.5.2</version>
<version>1.5.3</version>
</dependency>
```
[//]: # ({x-version-update-end})
Expand Down
9 changes: 2 additions & 7 deletions sdk/management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-management</artifactId>
Expand Down Expand Up @@ -113,13 +108,13 @@
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-netty</artifactId>
<version>1.4.0</version>
<version>1.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.0-beta.3</version>
<version>1.0.8</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
4 changes: 0 additions & 4 deletions sdk/resources/mgmt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@
</developers>

<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-management</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.azure.core.management.polling.PollResult;
import com.azure.core.management.serializer.AzureJacksonAdapter;
import com.azure.core.util.Context;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.polling.AsyncPollResponse;
import com.azure.core.util.polling.LongRunningOperationStatus;
Expand Down Expand Up @@ -50,20 +49,8 @@ protected AzureServiceClient(HttpPipeline httpPipeline, AzureEnvironment environ
((AzureJacksonAdapter) serializerAdapter).serializer().registerModule(DateTimeDeserializer.getModule());
}

private static final String OS;
private static final String OS_NAME;
private static final String OS_VERSION;
private static final String JAVA_VERSION;
private static final String SDK_VERSION = "2.0.0-SNAPSHOT";

static {
OS_NAME = System.getProperty("os.name");
OS_VERSION = System.getProperty("os.version");
OS = OS_NAME + "/" + OS_VERSION;
String version = System.getProperty("java.version");
JAVA_VERSION = version != null ? version : "Unknown";
}

private final SerializerAdapter serializerAdapter = new AzureJacksonAdapter();

private String sdkName;
Expand All @@ -83,24 +70,11 @@ public SerializerAdapter getSerializerAdapter() {
* @return the default client context.
*/
public Context getContext() {
Context context = new Context("java.version", JAVA_VERSION);
if (!CoreUtils.isNullOrEmpty(OS_NAME)) {
context = context.addData("os.name", OS_NAME);
}
if (!CoreUtils.isNullOrEmpty(OS_VERSION)) {
context = context.addData("os.version", OS_VERSION);
}
if (sdkName == null) {
String packageName = this.getClass().getPackage().getName();
if (packageName.endsWith(".models")) {
sdkName = packageName.substring(0, packageName.length() - ".models".length());
} else {
sdkName = packageName;
}
sdkName = this.getClass().getPackage().getName();
}
context = context.addData("Sdk-Name", sdkName);
context = context.addData("Sdk-Version", SDK_VERSION);
return context;
return new Context("Sdk-Name", sdkName)
.addData("Sdk-Version", SDK_VERSION);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why sdk version hardcoded?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there might be other approach. not investigated yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it. let's fix it before GA.

Copy link
Member Author

@weidongxu-microsoft weidongxu-microsoft Jul 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already fixed in this PR.

now the flow is:

  1. maven package the project, one step it would populate azure.properties with its current project.version.
  2. jar now has this azure.properties packaged and released with the jar
  3. runtime, code read version from azure.properties

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.UserAgentUtil;
import reactor.core.publisher.Mono;

/**
Expand All @@ -21,36 +22,12 @@ public class UserAgentPolicy implements HttpPipelinePolicy {
private static final String SDK_VERSION_KEY = "Sdk-Version";
private static final String APPLICATION_ID_KEY = "Application-Id";

private static final String OS_NAME = System.getProperty("os.name");
private static final String OS_VERSION = System.getProperty("os.version");
private static final String JAVA_VERSION = System.getProperty("java.version");

/*
* The base User-Agent header format is azsdk-java-<client_lib>/<sdk_version>. Additional information such as the
* application ID will be prepended and platform telemetry will be appended, a fully configured User-Agent header
* format is <application_id> azsdk-java-<client_lib>/<sdk_version> <platform_info>.
*/
private static final String DEFAULT_USER_AGENT_FORMAT = "azsdk-java-%s/%s";

// From the design guidelines, the platform info format is:
// <language runtime>; <os name> <os version>
private static final String PLATFORM_INFO_FORMAT = "%s; %s %s";

private final String defaultSdkName = this.getClass().getPackage().getName();
private final String defaultSdkVersion = this.getClass().getPackage().getSpecificationVersion();

private final HttpLogOptions httpLogOptions;
private final Configuration configuration;

private static final UserAgentPolicy DEFAULT_USER_AGENT_POLICY = new UserAgentPolicy(null, null);

/**
* @return default user agent policy
*/
public static UserAgentPolicy getDefaultUserAgentPolicy() {
return DEFAULT_USER_AGENT_POLICY;
}

/**
* @param httpLogOptions used for get application id
* @param configuration used for check telemetry enable or not
Expand All @@ -69,33 +46,6 @@ public UserAgentPolicy(HttpLogOptions httpLogOptions, Configuration configuratio
}
}

protected String buildUserAgent(String applicationId, String sdkName, String sdkVersion) {
StringBuilder userAgentBuilder = new StringBuilder();

// Only add the application ID if it is present as it is optional.
if (applicationId != null) {
userAgentBuilder.append(applicationId).append(" ");
}

// Add the required default User-Agent string.
userAgentBuilder.append(String.format(DEFAULT_USER_AGENT_FORMAT, sdkName, sdkVersion));

// Only add the platform telemetry if it is allowed as it is optional.
if (!telemetryDisabled()) {
String platformInfo = String.format(PLATFORM_INFO_FORMAT, JAVA_VERSION, OS_NAME, OS_VERSION);
userAgentBuilder.append(" ")
.append("(")
.append(platformInfo)
.append(")");
}

return userAgentBuilder.toString();
}

private boolean telemetryDisabled() {
return configuration.get(Configuration.PROPERTY_AZURE_TELEMETRY_DISABLED, false);
}

@Override
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
String userAgent = context.getHttpRequest().getHeaders().getValue(USER_AGENT_KEY);
Expand Down Expand Up @@ -124,7 +74,8 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN
applicationId = httpLogOptions.getApplicationId();
}

context.getHttpRequest().setHeader(USER_AGENT_KEY, buildUserAgent(applicationId, sdkName, sdkVersion));
context.getHttpRequest().setHeader(USER_AGENT_KEY,
UserAgentUtil.toUserAgentString(applicationId, sdkName, sdkVersion, configuration));
return next.process();
}
}