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

Support setting library header in grpc services #1078

Merged
merged 2 commits into from
Jun 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

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