File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.api.gax.rpc.ApiCallContext;
3636
import com.google.api.gax.rpc.ApiClientHeaderProvider;
3737
import com.google.api.gax.rpc.ApiException;
38+
import com.google.api.gax.rpc.FixedHeaderProvider;
3839
import com.google.api.gax.rpc.HeaderProvider;
3940
import com.google.api.gax.rpc.InstantiatingWatchdogProvider;
4041
import com.google.api.gax.rpc.OperationCallable;
@@ -70,6 +71,7 @@
7071
import com.google.common.base.MoreObjects;
7172
import com.google.common.base.Preconditions;
7273
import com.google.common.collect.ImmutableList;
74+
import com.google.common.collect.ImmutableMap;
7375
import com.google.common.util.concurrent.RateLimiter;
7476
import com.google.common.util.concurrent.ThreadFactoryBuilder;
7577
import com.google.iam.v1.GetIamPolicyRequest;
@@ -232,6 +234,8 @@ private void awaitTermination() throws InterruptedException {
232234
private static final int DEFAULT_TIMEOUT_SECONDS = 30 * 60;
233235
private static final int DEFAULT_PERIOD_SECONDS = 10;
234236
private static final int GRPC_KEEPALIVE_SECONDS = 2 * 60;
237+
private static final String USER_AGENT_KEY = "user-agent";
238+
private static final String CLIENT_LIBRARY_LANGUAGE = "spanner-java";
235239

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

299303
HeaderProvider mergedHeaderProvider = options.getMergedHeaderProvider(internalHeaderProvider);
304+
Map<String, String> headersWithUserAgent =
305+
ImmutableMap.<String, String>builder()
306+
.put(
307+
USER_AGENT_KEY,
308+
CLIENT_LIBRARY_LANGUAGE
309+
+ "/"
310+
+ GaxProperties.getLibraryVersion(GapicSpannerRpc.class))
311+
.putAll(mergedHeaderProvider.getHeaders())
312+
.build();
313+
final HeaderProvider headerProviderWithUserAgent =
314+
FixedHeaderProvider.create(headersWithUserAgent);
300315
this.metadataProvider =
301316
SpannerMetadataProvider.create(
302-
mergedHeaderProvider.getHeaders(),
317+
headerProviderWithUserAgent.getHeaders(),
303318
internalHeaderProviderBuilder.getResourceHeaderKey());
304319
this.callCredentialsProvider = options.getCallCredentialsProvider();
305320
this.compressorName = options.getCompressorName();
@@ -338,7 +353,7 @@ public GapicSpannerRpc(final SpannerOptions options) {
338353
options.getInterceptorProvider(),
339354
SpannerInterceptorProvider.createDefault()))
340355
.withEncoding(compressorName))
341-
.setHeaderProvider(mergedHeaderProvider);
356+
.setHeaderProvider(headerProviderWithUserAgent);
342357

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

0 commit comments

Comments
 (0)