Skip to content

Commit 7094469

Browse files
authored
Support setting library header in grpc services (#1078)
- add libraryName() and libraryVersion() to ServiceOptions - use setClientLibHeader in DefaultPubSubRpc
1 parent 42f0fe8 commit 7094469

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

gcloud-java-core/src/main/java/com/google/cloud/ServiceOptions.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ public abstract class ServiceOptions<ServiceT extends Service<OptionsT>, Service
6868
private static final String MANIFEST_ARTIFACT_ID_KEY = "artifactId";
6969
private static final String MANIFEST_VERSION_KEY = "Implementation-Version";
7070
private static final String ARTIFACT_ID = "gcloud-java-core";
71-
private static final String APPLICATION_BASE_NAME = "gcloud-java";
72-
private static final String APPLICATION_NAME = getApplicationName();
73-
private static final long serialVersionUID = -6410263550484023006L;
71+
private static final String LIBRARY_NAME = "gcloud-java";
72+
private static final String LIBRARY_VERSION = getLibraryVersion();
73+
private static final String APPLICATION_NAME =
74+
LIBRARY_VERSION == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + LIBRARY_VERSION;
75+
private static final long serialVersionUID = 3049375916337507361L;
7476

7577
private final String projectId;
7678
private final String host;
@@ -439,6 +441,20 @@ public String applicationName() {
439441
return APPLICATION_NAME;
440442
}
441443

444+
/**
445+
* Returns the library's name, {@code gcloud-java}, as a string.
446+
*/
447+
public String libraryName() {
448+
return LIBRARY_NAME;
449+
}
450+
451+
/**
452+
* Returns the library's version as a string.
453+
*/
454+
public String libraryVersion() {
455+
return LIBRARY_VERSION;
456+
}
457+
442458
protected int baseHashCode() {
443459
return Objects.hash(projectId, host, authCredentialsState, retryParams, serviceFactoryClassName,
444460
serviceRpcFactoryClassName, clock);
@@ -491,7 +507,7 @@ static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance) {
491507
return Iterables.getFirst(ServiceLoader.load(clazz), defaultInstance);
492508
}
493509

494-
private static String getApplicationName() {
510+
private static String getLibraryVersion() {
495511
String version = null;
496512
try {
497513
Enumeration<URL> resources =
@@ -507,6 +523,6 @@ private static String getApplicationName() {
507523
} catch (IOException e) {
508524
// ignore
509525
}
510-
return version != null ? APPLICATION_BASE_NAME + "/" + version : APPLICATION_BASE_NAME;
526+
return version;
511527
}
512528
}

gcloud-java-core/src/test/java/com/google/cloud/ServiceOptionsTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.IOException;
3131
import java.io.InputStream;
3232
import java.util.Set;
33+
import java.util.regex.Pattern;
3334

3435
public class ServiceOptionsTest {
3536
private static final String JSON_KEY =
@@ -80,6 +81,9 @@ public class ServiceOptionsTest {
8081
private static final TestServiceOptions DEFAULT_OPTIONS =
8182
TestServiceOptions.builder().projectId("project-id").build();
8283
private static final TestServiceOptions OPTIONS_COPY = OPTIONS.toBuilder().build();
84+
private static final String LIBRARY_NAME = "gcloud-java";
85+
private static final Pattern APPLICATION_NAME_PATTERN =
86+
Pattern.compile(LIBRARY_NAME + "(/[0-9]+.[0-9]+.[0-9]+)?");
8387

8488
private static class TestClock extends Clock {
8589
@Override
@@ -214,6 +218,16 @@ public void testBaseEquals() {
214218
assertNotEquals(DEFAULT_OPTIONS, OPTIONS);
215219
}
216220

221+
@Test
222+
public void testLibraryName() {
223+
assertEquals(LIBRARY_NAME, OPTIONS.libraryName());
224+
}
225+
226+
@Test
227+
public void testApplicationName() {
228+
assertTrue(APPLICATION_NAME_PATTERN.matcher(OPTIONS.applicationName()).matches());
229+
}
230+
217231
@Test
218232
public void testBaseHashCode() {
219233
assertEquals(OPTIONS.hashCode(), OPTIONS_COPY.hashCode());

gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/spi/DefaultPubSubRpc.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.cloud.pubsub.spi;
1818

19+
import static com.google.common.base.MoreObjects.firstNonNull;
20+
1921
import com.google.api.gax.core.RetrySettings;
2022
import com.google.api.gax.grpc.ApiCallSettings;
2123
import com.google.api.gax.grpc.ApiException;
@@ -118,11 +120,15 @@ public void onFailure(Throwable error) {
118120
public DefaultPubSubRpc(PubSubOptions options) throws IOException {
119121
executorFactory = new InternalPubSubOptions(options).executorFactory();
120122
executor = executorFactory.get();
123+
String libraryName = options.libraryName();
124+
String libraryVersion = firstNonNull(options.libraryVersion(), "");
121125
try {
122-
PublisherSettings.Builder pubBuilder =
123-
PublisherSettings.defaultBuilder().provideExecutorWith(executor, false);
124-
SubscriberSettings.Builder subBuilder =
125-
SubscriberSettings.defaultBuilder().provideExecutorWith(executor, false);
126+
PublisherSettings.Builder pubBuilder = PublisherSettings.defaultBuilder()
127+
.provideExecutorWith(executor, false)
128+
.setClientLibHeader(libraryName, libraryVersion);
129+
SubscriberSettings.Builder subBuilder = SubscriberSettings.defaultBuilder()
130+
.provideExecutorWith(executor, false)
131+
.setClientLibHeader(libraryName, libraryVersion);
126132
// todo(mziccard): PublisherSettings should support null/absent credentials for testing
127133
if (options.host().contains("localhost")
128134
|| options.authCredentials().equals(AuthCredentials.noAuth())) {

0 commit comments

Comments
 (0)