From 34ef20bbb3bdee856cef6774259506dfcfef1128 Mon Sep 17 00:00:00 2001 From: Paultagoras Date: Fri, 19 Jul 2024 05:18:46 -0400 Subject: [PATCH 1/3] Adjusting how user agent is calculated --- .../client/config/ClickHouseClientOption.java | 19 ++++++++++++------- .../client/http/ClickHouseHttpConnection.java | 8 ++++---- .../client/http/ClickHouseHttpClientTest.java | 3 ++- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/clickhouse-client/src/main/java/com/clickhouse/client/config/ClickHouseClientOption.java b/clickhouse-client/src/main/java/com/clickhouse/client/config/ClickHouseClientOption.java index f73f27870..7e08d4f46 100644 --- a/clickhouse-client/src/main/java/com/clickhouse/client/config/ClickHouseClientOption.java +++ b/clickhouse-client/src/main/java/com/clickhouse/client/config/ClickHouseClientOption.java @@ -480,9 +480,11 @@ public enum ClickHouseClientOption implements ClickHouseOption { ver = parts[3]; PRODUCT_REVISION = ver.substring(0, ver.length() - 1); } else { // perhaps try harder by checking version from pom.xml? - PRODUCT_VERSION = UNKNOWN; + PRODUCT_VERSION = ""; PRODUCT_REVISION = UNKNOWN; } + + CLIENT_OS_INFO = new StringBuilder().append(getSystemConfig("os.name", "O/S")).append('/') .append(getSystemConfig("os.version", UNKNOWN)).toString(); String javaVersion = System.getProperty("java.vendor.version"); @@ -510,15 +512,18 @@ public enum ClickHouseClientOption implements ClickHouseOption { * @param additionalProperty additional property if any * @return non-empty user-agent */ - public static final String buildUserAgent(String productName, String additionalProperty) { - productName = productName == null || productName.isEmpty() ? (String) PRODUCT_NAME.getEffectiveDefaultValue() - : productName.trim(); - StringBuilder builder = new StringBuilder(productName).append('/').append(PRODUCT_VERSION).append(" (") - .append(CLIENT_OS_INFO).append("; ").append(CLIENT_JVM_INFO); + public static String buildUserAgent(String productName, String additionalProperty) { + productName = productName == null || productName.isEmpty() ? (String) PRODUCT_NAME.getEffectiveDefaultValue() : productName.trim(); + StringBuilder builder = new StringBuilder(productName).append(PRODUCT_VERSION.isEmpty() ? "" : "/" + PRODUCT_VERSION); + + if (!String.valueOf(PRODUCT_NAME.getDefaultValue()).equals(productName)) {//Append if someone changed the original value + builder.append(" ").append(PRODUCT_NAME.getDefaultValue()).append(PRODUCT_VERSION.isEmpty() ? "" : "/" + PRODUCT_VERSION); + } + builder.append(" (").append(CLIENT_JVM_INFO); if (additionalProperty != null && !additionalProperty.isEmpty()) { builder.append("; ").append(additionalProperty.trim()); } - return builder.append("; rv:").append(PRODUCT_REVISION).append(')').toString(); + return builder.append(")").toString(); } /** diff --git a/clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java b/clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java index 91192606c..12ff448a9 100644 --- a/clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java +++ b/clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java @@ -420,14 +420,14 @@ protected String getDefaultUserAgent() { protected final String getUserAgent() { final ClickHouseConfig c = config; String name = c.getClientName(); + String userAgent = getDefaultUserAgent(); + if (!ClickHouseClientOption.CLIENT_NAME.getDefaultValue().equals(name)) { - return name; + return name + " " + userAgent; } - String userAgent = getDefaultUserAgent(); name = c.getProductName(); - return ClickHouseClientOption.PRODUCT_NAME.getDefaultValue().equals(name) ? userAgent - : new StringBuilder(name).append(userAgent.substring(userAgent.indexOf('/'))).toString(); + return ClickHouseClientOption.PRODUCT_NAME.getDefaultValue().equals(name) ? userAgent : name + " " + userAgent; } /** diff --git a/clickhouse-http-client/src/test/java/com/clickhouse/client/http/ClickHouseHttpClientTest.java b/clickhouse-http-client/src/test/java/com/clickhouse/client/http/ClickHouseHttpClientTest.java index 771278afd..6c21ac114 100644 --- a/clickhouse-http-client/src/test/java/com/clickhouse/client/http/ClickHouseHttpClientTest.java +++ b/clickhouse-http-client/src/test/java/com/clickhouse/client/http/ClickHouseHttpClientTest.java @@ -193,7 +193,8 @@ public void testUserAgent() throws Exception { ClickHouseResponse response = newRequest(client, server) .query("select http_user_agent from system.query_log where query='select ''" + uuid + "'''") .executeAndWait()) { - Assert.assertEquals(response.firstRecord().getValue(0).asString(), "MyCustomClient"); + String result = response.firstRecord().getValue(0).asString(); + Assert.assertTrue(result.startsWith("MyCustomClient")); } } From 186efac1fa6a49f5851d0eb7875916699b6a954e Mon Sep 17 00:00:00 2001 From: Paultagoras Date: Fri, 19 Jul 2024 13:28:47 -0400 Subject: [PATCH 2/3] Adjusting version string --- .../com/clickhouse/client/config/ClickHouseClientOption.java | 5 +++-- .../com/clickhouse/client/http/ClickHouseHttpConnection.java | 4 ++++ .../com/clickhouse/client/http/ClickHouseHttpClientTest.java | 5 +++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/clickhouse-client/src/main/java/com/clickhouse/client/config/ClickHouseClientOption.java b/clickhouse-client/src/main/java/com/clickhouse/client/config/ClickHouseClientOption.java index 7e08d4f46..584cb8fb0 100644 --- a/clickhouse-client/src/main/java/com/clickhouse/client/config/ClickHouseClientOption.java +++ b/clickhouse-client/src/main/java/com/clickhouse/client/config/ClickHouseClientOption.java @@ -436,6 +436,7 @@ public enum ClickHouseClientOption implements ClickHouseOption { private static final Map options; static final String UNKNOWN = "unknown"; + public static final String LATEST_KNOWN_VERSION = "0.6.3"; /** * Semantic version of the product. @@ -480,7 +481,7 @@ public enum ClickHouseClientOption implements ClickHouseOption { ver = parts[3]; PRODUCT_REVISION = ver.substring(0, ver.length() - 1); } else { // perhaps try harder by checking version from pom.xml? - PRODUCT_VERSION = ""; + PRODUCT_VERSION = LATEST_KNOWN_VERSION; PRODUCT_REVISION = UNKNOWN; } @@ -517,7 +518,7 @@ public static String buildUserAgent(String productName, String additionalPropert StringBuilder builder = new StringBuilder(productName).append(PRODUCT_VERSION.isEmpty() ? "" : "/" + PRODUCT_VERSION); if (!String.valueOf(PRODUCT_NAME.getDefaultValue()).equals(productName)) {//Append if someone changed the original value - builder.append(" ").append(PRODUCT_NAME.getDefaultValue()).append(PRODUCT_VERSION.isEmpty() ? "" : "/" + PRODUCT_VERSION); + builder.append(" ").append(PRODUCT_NAME.getDefaultValue()).append(LATEST_KNOWN_VERSION); } builder.append(" (").append(CLIENT_JVM_INFO); if (additionalProperty != null && !additionalProperty.isEmpty()) { diff --git a/clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java b/clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java index 12ff448a9..d957daddb 100644 --- a/clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java +++ b/clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java @@ -427,6 +427,10 @@ protected final String getUserAgent() { } name = c.getProductName(); + String version = c.getProductVersion(); + if (!ClickHouseClientOption.PRODUCT_VERSION.equals(version)) { + name = name + "/" + c.getProductVersion(); + } return ClickHouseClientOption.PRODUCT_NAME.getDefaultValue().equals(name) ? userAgent : name + " " + userAgent; } diff --git a/clickhouse-http-client/src/test/java/com/clickhouse/client/http/ClickHouseHttpClientTest.java b/clickhouse-http-client/src/test/java/com/clickhouse/client/http/ClickHouseHttpClientTest.java index 6c21ac114..bf44d33b2 100644 --- a/clickhouse-http-client/src/test/java/com/clickhouse/client/http/ClickHouseHttpClientTest.java +++ b/clickhouse-http-client/src/test/java/com/clickhouse/client/http/ClickHouseHttpClientTest.java @@ -175,7 +175,8 @@ public void testUserAgent() throws Exception { .query("select http_user_agent from system.query_log where query='select ''" + uuid + "'''") .executeAndWait()) { String result = response.firstRecord().getValue(0).asString(); - Assert.assertTrue(result.startsWith("MyCustomClient")); + System.out.println(result); + Assert.assertTrue(result.startsWith("MyCustomClient ClickHouse-JavaClient/")); Assert.assertTrue(result.indexOf("Http") > 0); } @@ -194,7 +195,7 @@ public void testUserAgent() throws Exception { .query("select http_user_agent from system.query_log where query='select ''" + uuid + "'''") .executeAndWait()) { String result = response.firstRecord().getValue(0).asString(); - Assert.assertTrue(result.startsWith("MyCustomClient")); + Assert.assertTrue(result.startsWith("MyCustomClient ClickHouse-JavaClient/")); } } From 7751f51bff19f9c3568cba18f3ea41cddabcaae4 Mon Sep 17 00:00:00 2001 From: Paultagoras Date: Fri, 19 Jul 2024 13:35:01 -0400 Subject: [PATCH 3/3] Update ClickHouseConfigTest.java --- .../java/com/clickhouse/client/ClickHouseConfigTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/clickhouse-client/src/test/java/com/clickhouse/client/ClickHouseConfigTest.java b/clickhouse-client/src/test/java/com/clickhouse/client/ClickHouseConfigTest.java index 3352c4460..1fa2bf3af 100644 --- a/clickhouse-client/src/test/java/com/clickhouse/client/ClickHouseConfigTest.java +++ b/clickhouse-client/src/test/java/com/clickhouse/client/ClickHouseConfigTest.java @@ -74,7 +74,7 @@ public void testCustomValues() { @Test(groups = { "unit" }) public void testClientInfo() throws UnknownHostException { ClickHouseConfig config = new ClickHouseConfig(); - Assert.assertEquals(config.getProductVersion(), "unknown"); + Assert.assertEquals(config.getProductVersion(), ClickHouseClientOption.LATEST_KNOWN_VERSION); Assert.assertEquals(config.getProductRevision(), "unknown"); Assert.assertEquals(config.getClientOsInfo(), System.getProperty("os.name") + "/" + System.getProperty("os.version")); @@ -85,11 +85,10 @@ public void testClientInfo() throws UnknownHostException { Assert.assertEquals(config.getClientHost(), InetAddress.getLocalHost().getHostName()); Assert.assertEquals(ClickHouseClientOption.buildUserAgent(null, null), - "ClickHouse-JavaClient/unknown (" + System.getProperty("os.name") + "/" - + System.getProperty("os.version") + "; " + System.getProperty("java.vm.name") + "/" + "ClickHouse-JavaClient/"+ ClickHouseClientOption.PRODUCT_VERSION + " (" + System.getProperty("java.vm.name") + "/" + System.getProperty("java.vendor.version", System.getProperty("java.vm.version", System.getProperty("java.version", "unknown"))) - + "; rv:unknown)"); + + ")"); Assert.assertEquals(ClickHouseClientOption.buildUserAgent(null, null), ClickHouseClientOption.buildUserAgent("", null));