Skip to content

Commit e2cae1c

Browse files
authored
Merge pull request #1739 from ClickHouse/user-agent-string
Adjusting how user agent is calculated
2 parents 75b7922 + 7751f51 commit e2cae1c

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

clickhouse-client/src/main/java/com/clickhouse/client/config/ClickHouseClientOption.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ public enum ClickHouseClientOption implements ClickHouseOption {
436436
private static final Map<String, ClickHouseClientOption> options;
437437

438438
static final String UNKNOWN = "unknown";
439+
public static final String LATEST_KNOWN_VERSION = "0.6.3";
439440

440441
/**
441442
* Semantic version of the product.
@@ -480,9 +481,11 @@ public enum ClickHouseClientOption implements ClickHouseOption {
480481
ver = parts[3];
481482
PRODUCT_REVISION = ver.substring(0, ver.length() - 1);
482483
} else { // perhaps try harder by checking version from pom.xml?
483-
PRODUCT_VERSION = UNKNOWN;
484+
PRODUCT_VERSION = LATEST_KNOWN_VERSION;
484485
PRODUCT_REVISION = UNKNOWN;
485486
}
487+
488+
486489
CLIENT_OS_INFO = new StringBuilder().append(getSystemConfig("os.name", "O/S")).append('/')
487490
.append(getSystemConfig("os.version", UNKNOWN)).toString();
488491
String javaVersion = System.getProperty("java.vendor.version");
@@ -510,15 +513,18 @@ public enum ClickHouseClientOption implements ClickHouseOption {
510513
* @param additionalProperty additional property if any
511514
* @return non-empty user-agent
512515
*/
513-
public static final String buildUserAgent(String productName, String additionalProperty) {
514-
productName = productName == null || productName.isEmpty() ? (String) PRODUCT_NAME.getEffectiveDefaultValue()
515-
: productName.trim();
516-
StringBuilder builder = new StringBuilder(productName).append('/').append(PRODUCT_VERSION).append(" (")
517-
.append(CLIENT_OS_INFO).append("; ").append(CLIENT_JVM_INFO);
516+
public static String buildUserAgent(String productName, String additionalProperty) {
517+
productName = productName == null || productName.isEmpty() ? (String) PRODUCT_NAME.getEffectiveDefaultValue() : productName.trim();
518+
StringBuilder builder = new StringBuilder(productName).append(PRODUCT_VERSION.isEmpty() ? "" : "/" + PRODUCT_VERSION);
519+
520+
if (!String.valueOf(PRODUCT_NAME.getDefaultValue()).equals(productName)) {//Append if someone changed the original value
521+
builder.append(" ").append(PRODUCT_NAME.getDefaultValue()).append(LATEST_KNOWN_VERSION);
522+
}
523+
builder.append(" (").append(CLIENT_JVM_INFO);
518524
if (additionalProperty != null && !additionalProperty.isEmpty()) {
519525
builder.append("; ").append(additionalProperty.trim());
520526
}
521-
return builder.append("; rv:").append(PRODUCT_REVISION).append(')').toString();
527+
return builder.append(")").toString();
522528
}
523529

524530
/**

clickhouse-client/src/test/java/com/clickhouse/client/ClickHouseConfigTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void testCustomValues() {
7474
@Test(groups = { "unit" })
7575
public void testClientInfo() throws UnknownHostException {
7676
ClickHouseConfig config = new ClickHouseConfig();
77-
Assert.assertEquals(config.getProductVersion(), "unknown");
77+
Assert.assertEquals(config.getProductVersion(), ClickHouseClientOption.LATEST_KNOWN_VERSION);
7878
Assert.assertEquals(config.getProductRevision(), "unknown");
7979
Assert.assertEquals(config.getClientOsInfo(),
8080
System.getProperty("os.name") + "/" + System.getProperty("os.version"));
@@ -85,11 +85,10 @@ public void testClientInfo() throws UnknownHostException {
8585
Assert.assertEquals(config.getClientHost(), InetAddress.getLocalHost().getHostName());
8686

8787
Assert.assertEquals(ClickHouseClientOption.buildUserAgent(null, null),
88-
"ClickHouse-JavaClient/unknown (" + System.getProperty("os.name") + "/"
89-
+ System.getProperty("os.version") + "; " + System.getProperty("java.vm.name") + "/"
88+
"ClickHouse-JavaClient/"+ ClickHouseClientOption.PRODUCT_VERSION + " (" + System.getProperty("java.vm.name") + "/"
9089
+ System.getProperty("java.vendor.version",
9190
System.getProperty("java.vm.version", System.getProperty("java.version", "unknown")))
92-
+ "; rv:unknown)");
91+
+ ")");
9392
Assert.assertEquals(ClickHouseClientOption.buildUserAgent(null, null),
9493
ClickHouseClientOption.buildUserAgent("", null));
9594

clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,18 @@ protected String getDefaultUserAgent() {
420420
protected final String getUserAgent() {
421421
final ClickHouseConfig c = config;
422422
String name = c.getClientName();
423+
String userAgent = getDefaultUserAgent();
424+
423425
if (!ClickHouseClientOption.CLIENT_NAME.getDefaultValue().equals(name)) {
424-
return name;
426+
return name + " " + userAgent;
425427
}
426428

427-
String userAgent = getDefaultUserAgent();
428429
name = c.getProductName();
429-
return ClickHouseClientOption.PRODUCT_NAME.getDefaultValue().equals(name) ? userAgent
430-
: new StringBuilder(name).append(userAgent.substring(userAgent.indexOf('/'))).toString();
430+
String version = c.getProductVersion();
431+
if (!ClickHouseClientOption.PRODUCT_VERSION.equals(version)) {
432+
name = name + "/" + c.getProductVersion();
433+
}
434+
return ClickHouseClientOption.PRODUCT_NAME.getDefaultValue().equals(name) ? userAgent : name + " " + userAgent;
431435
}
432436

433437
/**

clickhouse-http-client/src/test/java/com/clickhouse/client/http/ClickHouseHttpClientTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ public void testUserAgent() throws Exception {
175175
.query("select http_user_agent from system.query_log where query='select ''" + uuid + "'''")
176176
.executeAndWait()) {
177177
String result = response.firstRecord().getValue(0).asString();
178-
Assert.assertTrue(result.startsWith("MyCustomClient"));
178+
System.out.println(result);
179+
Assert.assertTrue(result.startsWith("MyCustomClient ClickHouse-JavaClient/"));
179180
Assert.assertTrue(result.indexOf("Http") > 0);
180181
}
181182

@@ -193,7 +194,8 @@ public void testUserAgent() throws Exception {
193194
ClickHouseResponse response = newRequest(client, server)
194195
.query("select http_user_agent from system.query_log where query='select ''" + uuid + "'''")
195196
.executeAndWait()) {
196-
Assert.assertEquals(response.firstRecord().getValue(0).asString(), "MyCustomClient");
197+
String result = response.firstRecord().getValue(0).asString();
198+
Assert.assertTrue(result.startsWith("MyCustomClient ClickHouse-JavaClient/"));
197199
}
198200
}
199201

0 commit comments

Comments
 (0)