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

feat: include User agent #747

Merged
merged 4 commits into from Dec 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -35,6 +35,7 @@
import com.google.api.gax.rpc.ApiCallContext;
import com.google.api.gax.rpc.ApiClientHeaderProvider;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.FixedHeaderProvider;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.InstantiatingWatchdogProvider;
import com.google.api.gax.rpc.OperationCallable;
Expand Down Expand Up @@ -70,6 +71,7 @@
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.RateLimiter;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.iam.v1.GetIamPolicyRequest;
Expand Down Expand Up @@ -232,6 +234,8 @@ private void awaitTermination() throws InterruptedException {
private static final int DEFAULT_TIMEOUT_SECONDS = 30 * 60;
private static final int DEFAULT_PERIOD_SECONDS = 10;
private static final int GRPC_KEEPALIVE_SECONDS = 2 * 60;
private static final String USER_AGENT_KEY = "user-agent";
private static final String CLIENT_LIBRARY_LANGUAGE = "spanner-java";

// TODO(weiranf): Remove this temporary endpoint once DirectPath goes to public beta.
private static final String DIRECT_PATH_ENDPOINT = "aa423245250f2bbf.sandbox.googleapis.com:443";
Expand Down Expand Up @@ -297,9 +301,20 @@ public GapicSpannerRpc(final SpannerOptions options) {
.build();

HeaderProvider mergedHeaderProvider = options.getMergedHeaderProvider(internalHeaderProvider);
Map<String, String> headersWithUserAgent =
ImmutableMap.<String, String>builder()
.put(
USER_AGENT_KEY,
CLIENT_LIBRARY_LANGUAGE
+ "/"
+ GaxProperties.getLibraryVersion(GapicSpannerRpc.class))
.putAll(mergedHeaderProvider.getHeaders())
.build();
final HeaderProvider headerProviderWithUserAgent =
FixedHeaderProvider.create(headersWithUserAgent);
this.metadataProvider =
SpannerMetadataProvider.create(
mergedHeaderProvider.getHeaders(),
headerProviderWithUserAgent.getHeaders(),
internalHeaderProviderBuilder.getResourceHeaderKey());
this.callCredentialsProvider = options.getCallCredentialsProvider();
this.compressorName = options.getCompressorName();
Expand Down Expand Up @@ -338,7 +353,7 @@ public GapicSpannerRpc(final SpannerOptions options) {
options.getInterceptorProvider(),
SpannerInterceptorProvider.createDefault()))
.withEncoding(compressorName))
.setHeaderProvider(mergedHeaderProvider);
.setHeaderProvider(headerProviderWithUserAgent);

// TODO(weiranf): Set to true by default once DirectPath goes to public beta.
if (shouldAttemptDirectPath()) {
Expand Down Expand Up @@ -478,8 +493,10 @@ private static void checkEmulatorConnection(
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.UNAVAILABLE,
String.format(
"The environment variable SPANNER_EMULATOR_HOST has been set to %s, but no running emulator could be found at that address.\n"
+ "Did you forget to start the emulator, or to unset the environment variable?",
"The environment variable SPANNER_EMULATOR_HOST has been set to %s, but no running"
+ " emulator could be found at that address.\n"
+ "Did you forget to start the emulator, or to unset the environment"
+ " variable?",
emulatorHost));
}
}
Expand Down