Skip to content

Commit 712c65f

Browse files
chore: remove double client lib token header (#2681)
* chore: remove double client lib token header Removes the double inclusion of the standard client lib token when no custom client lib token has been set. Also reuses more of the standard feature for setting the client lib token, instaed of appending the standard token at the moment that we are creating the gRPC stub. This should reduce the probability that anyone who might be using a custom header or custom client in some way will get it wrong. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * test: modify expected outcome to match the new behavior --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 9f1f047 commit 712c65f

File tree

5 files changed

+58
-14
lines changed

5 files changed

+58
-14
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ If you are using Maven without the BOM, add this to your dependencies:
5050
If you are using Gradle 5.x or later, add this to your dependencies:
5151

5252
```Groovy
53-
implementation platform('com.google.cloud:libraries-bom:26.24.0')
53+
implementation platform('com.google.cloud:libraries-bom:26.25.0')
5454
5555
implementation 'com.google.cloud:google-cloud-spanner'
5656
```
5757
If you are using Gradle without BOM, add this to your dependencies:
5858

5959
```Groovy
60-
implementation 'com.google.cloud:google-cloud-spanner:6.50.1'
60+
implementation 'com.google.cloud:google-cloud-spanner:6.51.0'
6161
```
6262

6363
If you are using SBT, add this to your dependencies:
6464

6565
```Scala
66-
libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.50.1"
66+
libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.51.0"
6767
```
6868
<!-- {x-version-update-end} -->
6969

@@ -432,7 +432,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
432432
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-spanner/java11.html
433433
[stability-image]: https://img.shields.io/badge/stability-stable-green
434434
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-spanner.svg
435-
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-spanner/6.50.1
435+
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-spanner/6.51.0
436436
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
437437
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
438438
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.spanner;
1818

1919
import com.google.api.core.ApiFunction;
20+
import com.google.api.core.InternalApi;
2021
import com.google.api.gax.core.ExecutorProvider;
2122
import com.google.api.gax.grpc.GrpcCallContext;
2223
import com.google.api.gax.grpc.GrpcInterceptorProvider;
@@ -686,10 +687,10 @@ public static class Builder
686687
private final ImmutableSet<String> allowedClientLibTokens =
687688
ImmutableSet.of(
688689
ServiceOptions.getGoogApiClientLibName(),
689-
JDBC_API_CLIENT_LIB_TOKEN,
690-
HIBERNATE_API_CLIENT_LIB_TOKEN,
691-
LIQUIBASE_API_CLIENT_LIB_TOKEN,
692-
PG_ADAPTER_CLIENT_LIB_TOKEN);
690+
createCustomClientLibToken(JDBC_API_CLIENT_LIB_TOKEN),
691+
createCustomClientLibToken(HIBERNATE_API_CLIENT_LIB_TOKEN),
692+
createCustomClientLibToken(LIQUIBASE_API_CLIENT_LIB_TOKEN),
693+
createCustomClientLibToken(PG_ADAPTER_CLIENT_LIB_TOKEN));
693694
private TransportChannelProvider channelProvider;
694695

695696
@SuppressWarnings("rawtypes")
@@ -725,6 +726,10 @@ public static class Builder
725726
private String emulatorHost = System.getenv("SPANNER_EMULATOR_HOST");
726727
private boolean leaderAwareRoutingEnabled = true;
727728

729+
private static String createCustomClientLibToken(String token) {
730+
return token + " " + ServiceOptions.getGoogApiClientLibName();
731+
}
732+
728733
private Builder() {
729734
// Manually set retry and polling settings that work.
730735
OperationTimedPollAlgorithm longRunningPollingAlgorithm =
@@ -795,6 +800,13 @@ protected Set<String> getAllowedClientLibTokens() {
795800
return allowedClientLibTokens;
796801
}
797802

803+
@InternalApi
804+
@Override
805+
public SpannerOptions.Builder setClientLibToken(String clientLibToken) {
806+
return super.setClientLibToken(
807+
clientLibToken + " " + ServiceOptions.getGoogApiClientLibName());
808+
}
809+
798810
/**
799811
* Sets the {@code ChannelProvider}. {@link GapicSpannerRpc} would create a default one if none
800812
* is provided.

google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpc.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import com.google.api.pathtemplate.PathTemplate;
5656
import com.google.cloud.RetryHelper;
5757
import com.google.cloud.RetryHelper.RetryHelperException;
58-
import com.google.cloud.ServiceOptions;
5958
import com.google.cloud.grpc.GcpManagedChannelBuilder;
6059
import com.google.cloud.grpc.GcpManagedChannelOptions;
6160
import com.google.cloud.grpc.GcpManagedChannelOptions.GcpMetricsOptions;
@@ -297,8 +296,7 @@ public GapicSpannerRpc(final SpannerOptions options) {
297296
ApiClientHeaderProvider internalHeaderProvider =
298297
internalHeaderProviderBuilder
299298
.setClientLibToken(
300-
options.getClientLibToken() + " " + ServiceOptions.getGoogApiClientLibName(),
301-
GaxProperties.getLibraryVersion(options.getClass()))
299+
options.getClientLibToken(), GaxProperties.getLibraryVersion(options.getClass()))
302300
.setTransportToken(
303301
GaxGrpcProperties.getGrpcTokenName(), GaxGrpcProperties.getGrpcVersion())
304302
.build();

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,23 +516,25 @@ public void testSetClientLibToken() {
516516
.setCredentials(NoCredentials.getInstance())
517517
.setClientLibToken(jdbcToken)
518518
.build();
519-
assertThat(options.getClientLibToken()).isEqualTo(jdbcToken);
519+
// Verify that the client lib token that will actually be used contains both the JDBC token and
520+
// the standard Java client library token ('gccl').
521+
assertEquals("sp-jdbc gccl", options.getClientLibToken());
520522

521523
options =
522524
SpannerOptions.newBuilder()
523525
.setProjectId("some-project")
524526
.setCredentials(NoCredentials.getInstance())
525527
.setClientLibToken(hibernateToken)
526528
.build();
527-
assertThat(options.getClientLibToken()).isEqualTo(hibernateToken);
529+
assertEquals("sp-hib gccl", options.getClientLibToken());
528530

529531
options =
530532
SpannerOptions.newBuilder()
531533
.setProjectId("some-project")
532534
.setCredentials(NoCredentials.getInstance())
533535
.setClientLibToken(pgAdapterToken)
534536
.build();
535-
assertEquals(options.getClientLibToken(), pgAdapterToken);
537+
assertEquals("pg-adapter gccl", options.getClientLibToken());
536538

537539
options =
538540
SpannerOptions.newBuilder()

google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpcTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,35 @@ public void testRouteToLeaderHeaderWithLeaderAwareRoutingDisabled() {
583583
assertFalse(isRouteToLeader);
584584
}
585585

586+
@Test
587+
public void testClientLibToken() {
588+
SpannerOptions options = createSpannerOptions();
589+
try (Spanner spanner = options.getService()) {
590+
DatabaseClient databaseClient =
591+
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
592+
TransactionRunner runner = databaseClient.readWriteTransaction();
593+
runner.run(transaction -> transaction.executeUpdate(UPDATE_FOO_STATEMENT));
594+
}
595+
Key<String> key = Key.of("x-goog-api-client", Metadata.ASCII_STRING_MARSHALLER);
596+
assertTrue(lastSeenHeaders.containsKey(key));
597+
assertTrue(
598+
lastSeenHeaders.get(key),
599+
Objects.requireNonNull(lastSeenHeaders.get(key))
600+
.contains(ServiceOptions.getGoogApiClientLibName() + "/"));
601+
// Check that the default header value is only included once in the header.
602+
// We do this by splitting the entire header by the default header value. The resulting array
603+
// should have 2 elements.
604+
assertEquals(
605+
lastSeenHeaders.get(key),
606+
2,
607+
Objects.requireNonNull(lastSeenHeaders.get(key))
608+
.split(ServiceOptions.getGoogApiClientLibName())
609+
.length);
610+
assertTrue(
611+
lastSeenHeaders.get(key),
612+
Objects.requireNonNull(lastSeenHeaders.get(key)).contains("gl-java/"));
613+
}
614+
586615
@Test
587616
public void testCustomClientLibToken_alsoContainsDefaultToken() {
588617
SpannerOptions options =
@@ -602,6 +631,9 @@ public void testCustomClientLibToken_alsoContainsDefaultToken() {
602631
lastSeenHeaders.get(key),
603632
Objects.requireNonNull(lastSeenHeaders.get(key))
604633
.contains(ServiceOptions.getGoogApiClientLibName() + "/"));
634+
assertTrue(
635+
lastSeenHeaders.get(key),
636+
Objects.requireNonNull(lastSeenHeaders.get(key)).contains("gl-java/"));
605637
}
606638

607639
private SpannerOptions createSpannerOptions() {

0 commit comments

Comments
 (0)