From 2553b89424a2e42dda0a83eb209671a0e63562a8 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 24 Jun 2016 20:40:29 +0200 Subject: [PATCH] Support setting library header in grpc services (#1078) - add libraryName() and libraryVersion() to ServiceOptions - use setClientLibHeader in DefaultPubSubRpc --- .../java/com/google/cloud/ServiceOptions.java | 26 +++++++++++++++---- .../com/google/cloud/ServiceOptionsTest.java | 14 ++++++++++ .../cloud/pubsub/spi/DefaultPubSubRpc.java | 14 +++++++--- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/gcloud-java-core/src/main/java/com/google/cloud/ServiceOptions.java b/gcloud-java-core/src/main/java/com/google/cloud/ServiceOptions.java index 8a1d95f32e54..db24555a189d 100644 --- a/gcloud-java-core/src/main/java/com/google/cloud/ServiceOptions.java +++ b/gcloud-java-core/src/main/java/com/google/cloud/ServiceOptions.java @@ -68,9 +68,11 @@ public abstract class ServiceOptions, Service private static final String MANIFEST_ARTIFACT_ID_KEY = "artifactId"; private static final String MANIFEST_VERSION_KEY = "Implementation-Version"; private static final String ARTIFACT_ID = "gcloud-java-core"; - private static final String APPLICATION_BASE_NAME = "gcloud-java"; - private static final String APPLICATION_NAME = getApplicationName(); - private static final long serialVersionUID = -6410263550484023006L; + private static final String LIBRARY_NAME = "gcloud-java"; + private static final String LIBRARY_VERSION = getLibraryVersion(); + private static final String APPLICATION_NAME = + LIBRARY_VERSION == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + LIBRARY_VERSION; + private static final long serialVersionUID = 3049375916337507361L; private final String projectId; private final String host; @@ -439,6 +441,20 @@ public String applicationName() { return APPLICATION_NAME; } + /** + * Returns the library's name, {@code gcloud-java}, as a string. + */ + public String libraryName() { + return LIBRARY_NAME; + } + + /** + * Returns the library's version as a string. + */ + public String libraryVersion() { + return LIBRARY_VERSION; + } + protected int baseHashCode() { return Objects.hash(projectId, host, authCredentialsState, retryParams, serviceFactoryClassName, serviceRpcFactoryClassName, clock); @@ -491,7 +507,7 @@ static T getFromServiceLoader(Class clazz, T defaultInstance) { return Iterables.getFirst(ServiceLoader.load(clazz), defaultInstance); } - private static String getApplicationName() { + private static String getLibraryVersion() { String version = null; try { Enumeration resources = @@ -507,6 +523,6 @@ private static String getApplicationName() { } catch (IOException e) { // ignore } - return version != null ? APPLICATION_BASE_NAME + "/" + version : APPLICATION_BASE_NAME; + return version; } } diff --git a/gcloud-java-core/src/test/java/com/google/cloud/ServiceOptionsTest.java b/gcloud-java-core/src/test/java/com/google/cloud/ServiceOptionsTest.java index 7afc2b0b972b..7792c63e821b 100644 --- a/gcloud-java-core/src/test/java/com/google/cloud/ServiceOptionsTest.java +++ b/gcloud-java-core/src/test/java/com/google/cloud/ServiceOptionsTest.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.Set; +import java.util.regex.Pattern; public class ServiceOptionsTest { private static final String JSON_KEY = @@ -80,6 +81,9 @@ public class ServiceOptionsTest { private static final TestServiceOptions DEFAULT_OPTIONS = TestServiceOptions.builder().projectId("project-id").build(); private static final TestServiceOptions OPTIONS_COPY = OPTIONS.toBuilder().build(); + private static final String LIBRARY_NAME = "gcloud-java"; + private static final Pattern APPLICATION_NAME_PATTERN = + Pattern.compile(LIBRARY_NAME + "(/[0-9]+.[0-9]+.[0-9]+)?"); private static class TestClock extends Clock { @Override @@ -214,6 +218,16 @@ public void testBaseEquals() { assertNotEquals(DEFAULT_OPTIONS, OPTIONS); } + @Test + public void testLibraryName() { + assertEquals(LIBRARY_NAME, OPTIONS.libraryName()); + } + + @Test + public void testApplicationName() { + assertTrue(APPLICATION_NAME_PATTERN.matcher(OPTIONS.applicationName()).matches()); + } + @Test public void testBaseHashCode() { assertEquals(OPTIONS.hashCode(), OPTIONS_COPY.hashCode()); diff --git a/gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/spi/DefaultPubSubRpc.java b/gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/spi/DefaultPubSubRpc.java index d174e5172a23..11b2948bc995 100644 --- a/gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/spi/DefaultPubSubRpc.java +++ b/gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/spi/DefaultPubSubRpc.java @@ -16,6 +16,8 @@ package com.google.cloud.pubsub.spi; +import static com.google.common.base.MoreObjects.firstNonNull; + import com.google.api.gax.core.RetrySettings; import com.google.api.gax.grpc.ApiCallSettings; import com.google.api.gax.grpc.ApiException; @@ -118,11 +120,15 @@ public void onFailure(Throwable error) { public DefaultPubSubRpc(PubSubOptions options) throws IOException { executorFactory = new InternalPubSubOptions(options).executorFactory(); executor = executorFactory.get(); + String libraryName = options.libraryName(); + String libraryVersion = firstNonNull(options.libraryVersion(), ""); try { - PublisherSettings.Builder pubBuilder = - PublisherSettings.defaultBuilder().provideExecutorWith(executor, false); - SubscriberSettings.Builder subBuilder = - SubscriberSettings.defaultBuilder().provideExecutorWith(executor, false); + PublisherSettings.Builder pubBuilder = PublisherSettings.defaultBuilder() + .provideExecutorWith(executor, false) + .setClientLibHeader(libraryName, libraryVersion); + SubscriberSettings.Builder subBuilder = SubscriberSettings.defaultBuilder() + .provideExecutorWith(executor, false) + .setClientLibHeader(libraryName, libraryVersion); // todo(mziccard): PublisherSettings should support null/absent credentials for testing if (options.host().contains("localhost") || options.authCredentials().equals(AuthCredentials.noAuth())) {