Skip to content

Commit

Permalink
Support setting library header in grpc services (googleapis#1078)
Browse files Browse the repository at this point in the history
- add libraryName() and libraryVersion() to ServiceOptions
- use setClientLibHeader in DefaultPubSubRpc
  • Loading branch information
mziccard committed Jun 27, 2016
1 parent 57883ae commit 2553b89
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ public abstract class ServiceOptions<ServiceT extends Service<OptionsT>, 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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -491,7 +507,7 @@ static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance) {
return Iterables.getFirst(ServiceLoader.load(clazz), defaultInstance);
}

private static String getApplicationName() {
private static String getLibraryVersion() {
String version = null;
try {
Enumeration<URL> resources =
Expand All @@ -507,6 +523,6 @@ private static String getApplicationName() {
} catch (IOException e) {
// ignore
}
return version != null ? APPLICATION_BASE_NAME + "/" + version : APPLICATION_BASE_NAME;
return version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())) {
Expand Down

0 comments on commit 2553b89

Please sign in to comment.