Skip to content

Commit 34ef20b

Browse files
author
Paultagoras
committed
Adjusting how user agent is calculated
1 parent 75b7922 commit 34ef20b

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,11 @@ public enum ClickHouseClientOption implements ClickHouseOption {
480480
ver = parts[3];
481481
PRODUCT_REVISION = ver.substring(0, ver.length() - 1);
482482
} else { // perhaps try harder by checking version from pom.xml?
483-
PRODUCT_VERSION = UNKNOWN;
483+
PRODUCT_VERSION = "";
484484
PRODUCT_REVISION = UNKNOWN;
485485
}
486+
487+
486488
CLIENT_OS_INFO = new StringBuilder().append(getSystemConfig("os.name", "O/S")).append('/')
487489
.append(getSystemConfig("os.version", UNKNOWN)).toString();
488490
String javaVersion = System.getProperty("java.vendor.version");
@@ -510,15 +512,18 @@ public enum ClickHouseClientOption implements ClickHouseOption {
510512
* @param additionalProperty additional property if any
511513
* @return non-empty user-agent
512514
*/
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);
515+
public static String buildUserAgent(String productName, String additionalProperty) {
516+
productName = productName == null || productName.isEmpty() ? (String) PRODUCT_NAME.getEffectiveDefaultValue() : productName.trim();
517+
StringBuilder builder = new StringBuilder(productName).append(PRODUCT_VERSION.isEmpty() ? "" : "/" + PRODUCT_VERSION);
518+
519+
if (!String.valueOf(PRODUCT_NAME.getDefaultValue()).equals(productName)) {//Append if someone changed the original value
520+
builder.append(" ").append(PRODUCT_NAME.getDefaultValue()).append(PRODUCT_VERSION.isEmpty() ? "" : "/" + PRODUCT_VERSION);
521+
}
522+
builder.append(" (").append(CLIENT_JVM_INFO);
518523
if (additionalProperty != null && !additionalProperty.isEmpty()) {
519524
builder.append("; ").append(additionalProperty.trim());
520525
}
521-
return builder.append("; rv:").append(PRODUCT_REVISION).append(')').toString();
526+
return builder.append(")").toString();
522527
}
523528

524529
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,14 @@ 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+
return ClickHouseClientOption.PRODUCT_NAME.getDefaultValue().equals(name) ? userAgent : name + " " + userAgent;
431431
}
432432

433433
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ public void testUserAgent() throws Exception {
193193
ClickHouseResponse response = newRequest(client, server)
194194
.query("select http_user_agent from system.query_log where query='select ''" + uuid + "'''")
195195
.executeAndWait()) {
196-
Assert.assertEquals(response.firstRecord().getValue(0).asString(), "MyCustomClient");
196+
String result = response.firstRecord().getValue(0).asString();
197+
Assert.assertTrue(result.startsWith("MyCustomClient"));
197198
}
198199
}
199200

0 commit comments

Comments
 (0)