Skip to content

Commit

Permalink
fix: always include default client lib header (#2676)
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Oct 14, 2023
1 parent 54d4511 commit 74fd174
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.google.api.pathtemplate.PathTemplate;
import com.google.cloud.RetryHelper;
import com.google.cloud.RetryHelper.RetryHelperException;
import com.google.cloud.ServiceOptions;
import com.google.cloud.grpc.GcpManagedChannelBuilder;
import com.google.cloud.grpc.GcpManagedChannelOptions;
import com.google.cloud.grpc.GcpManagedChannelOptions.GcpMetricsOptions;
Expand Down Expand Up @@ -296,7 +297,8 @@ public GapicSpannerRpc(final SpannerOptions options) {
ApiClientHeaderProvider internalHeaderProvider =
internalHeaderProviderBuilder
.setClientLibToken(
options.getClientLibToken(), GaxProperties.getLibraryVersion(options.getClass()))
options.getClientLibToken() + " " + ServiceOptions.getGoogApiClientLibName(),
GaxProperties.getLibraryVersion(options.getClass()))
.setTransportToken(
GaxGrpcProperties.getGrpcTokenName(), GaxGrpcProperties.getGrpcVersion())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.api.gax.rpc.HeaderProvider;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.OAuth2Credentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Dialect;
Expand Down Expand Up @@ -77,6 +78,7 @@
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -181,6 +183,12 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
String auth =
headers.get(Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER));
assertThat(auth).isEqualTo("Bearer " + VARIABLE_OAUTH_TOKEN);
String clientLibToken =
headers.get(
Metadata.Key.of("x-goog-api-client", Metadata.ASCII_STRING_MARSHALLER));
assertNotNull(clientLibToken);
assertTrue(
clientLibToken.contains(ServiceOptions.getGoogApiClientLibName() + "/"));
if (call.getMethodDescriptor()
.equals(SpannerGrpc.getExecuteStreamingSqlMethod())
|| call.getMethodDescriptor().equals(SpannerGrpc.getExecuteSqlMethod())) {
Expand Down Expand Up @@ -575,6 +583,27 @@ public void testRouteToLeaderHeaderWithLeaderAwareRoutingDisabled() {
assertFalse(isRouteToLeader);
}

@Test
public void testCustomClientLibToken_alsoContainsDefaultToken() {
SpannerOptions options =
createSpannerOptions().toBuilder().setClientLibToken("pg-adapter").build();
try (Spanner spanner = options.getService()) {
DatabaseClient databaseClient =
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
TransactionRunner runner = databaseClient.readWriteTransaction();
runner.run(transaction -> transaction.executeUpdate(UPDATE_FOO_STATEMENT));
}
Key<String> key = Key.of("x-goog-api-client", Metadata.ASCII_STRING_MARSHALLER);
assertTrue(lastSeenHeaders.containsKey(key));
assertTrue(
lastSeenHeaders.get(key),
Objects.requireNonNull(lastSeenHeaders.get(key)).contains("pg-adapter"));
assertTrue(
lastSeenHeaders.get(key),
Objects.requireNonNull(lastSeenHeaders.get(key))
.contains(ServiceOptions.getGoogApiClientLibName() + "/"));
}

private SpannerOptions createSpannerOptions() {
String endpoint = address.getHostString() + ":" + server.getPort();
return SpannerOptions.newBuilder()
Expand Down

0 comments on commit 74fd174

Please sign in to comment.