From cb640abeb8ec9321136b86d5b54e620dba087080 Mon Sep 17 00:00:00 2001 From: gauravsnj <98738378+gauravsnj@users.noreply.github.com> Date: Mon, 28 Feb 2022 13:19:44 +0530 Subject: [PATCH] feat: Track PG Adapter usage from user-agent headers (#1711) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added the string "pg-adapter" in the list of allowed client library token, so that when PG Adapter connects via client library by passing the string "pg-adapter" in the user-agent header, it doesn't get discarded. * Added an additional test case for the testSetClientLibToken test. * Changed assertThat to assertEquals * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Changed assertThat to assertEquals Co-authored-by: Owl Bot --- .../com/google/cloud/spanner/SpannerOptions.java | 4 +++- .../com/google/cloud/spanner/SpannerOptionsTest.java | 12 +++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java index 65de63b720f..b62b2d44556 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java @@ -80,6 +80,7 @@ public class SpannerOptions extends ServiceOptions { private static final String JDBC_API_CLIENT_LIB_TOKEN = "sp-jdbc"; private static final String HIBERNATE_API_CLIENT_LIB_TOKEN = "sp-hib"; private static final String LIQUIBASE_API_CLIENT_LIB_TOKEN = "sp-liq"; + private static final String PG_ADAPTER_CLIENT_LIB_TOKEN = "pg-adapter"; private static final String API_SHORT_NAME = "Spanner"; private static final String DEFAULT_HOST = "https://spanner.googleapis.com"; @@ -657,7 +658,8 @@ public static class Builder ServiceOptions.getGoogApiClientLibName(), JDBC_API_CLIENT_LIB_TOKEN, HIBERNATE_API_CLIENT_LIB_TOKEN, - LIQUIBASE_API_CLIENT_LIB_TOKEN); + LIQUIBASE_API_CLIENT_LIB_TOKEN, + PG_ADAPTER_CLIENT_LIB_TOKEN); private TransportChannelProvider channelProvider; @SuppressWarnings("rawtypes") diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java index fc82954ad40..7d5c8a553aa 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThrows; import static org.mockito.Mockito.mock; @@ -502,6 +503,7 @@ public void testDoNotCacheClosedSpannerInstance() { public void testSetClientLibToken() { final String jdbcToken = "sp-jdbc"; final String hibernateToken = "sp-hib"; + final String pgAdapterToken = "pg-adapter"; SpannerOptions options = SpannerOptions.newBuilder() .setProjectId("some-project") @@ -522,8 +524,16 @@ public void testSetClientLibToken() { SpannerOptions.newBuilder() .setProjectId("some-project") .setCredentials(NoCredentials.getInstance()) + .setClientLibToken(pgAdapterToken) .build(); - assertThat(options.getClientLibToken()).isEqualTo(ServiceOptions.getGoogApiClientLibName()); + assertEquals(options.getClientLibToken(), pgAdapterToken); + + options = + SpannerOptions.newBuilder() + .setProjectId("some-project") + .setCredentials(NoCredentials.getInstance()) + .build(); + assertEquals(options.getClientLibToken(), ServiceOptions.getGoogApiClientLibName()); } @Test(expected = IllegalArgumentException.class)