Skip to content

Commit 861fd5b

Browse files
authored
Merge pull request #2280 from ClickHouse/use-product-name
Updating the client name to include produce name in URL
2 parents 1ca07a7 + c1c1491 commit 861fd5b

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.clickhouse.jdbc;
22

33
import com.clickhouse.client.api.Client;
4+
import com.clickhouse.client.api.ClientConfigProperties;
45
import com.clickhouse.client.api.internal.ServerSettings;
56
import com.clickhouse.client.api.query.GenericRecord;
67
import com.clickhouse.client.api.query.QuerySettings;
@@ -50,6 +51,7 @@ public class ConnectionImpl implements Connection, JdbcV2Wrapper {
5051
protected String cluster;
5152
private String catalog;
5253
private String schema;
54+
private String appName;
5355
private QuerySettings defaultQuerySettings;
5456

5557
private final com.clickhouse.jdbc.metadata.DatabaseMetaData metadata;
@@ -62,14 +64,27 @@ public ConnectionImpl(String url, Properties info) throws SQLException {
6264
this.config = new JdbcConfiguration(url, info);
6365
this.onCluster = false;
6466
this.cluster = null;
67+
this.appName = "";
6568
String clientName = "ClickHouse JDBC Driver V2/" + Driver.driverVersion;
6669

70+
Map<String, String> clientProperties = config.getClientProperties();
71+
if (clientProperties.get(ClientConfigProperties.CLIENT_NAME.getKey()) != null) {
72+
this.appName = clientProperties.get(ClientConfigProperties.CLIENT_NAME.getKey()).trim();
73+
clientName = this.appName + " " + clientName; // Use the application name as client name
74+
} else if (clientProperties.get(ClientConfigProperties.PRODUCT_NAME.getKey()) != null) {
75+
// Backward compatibility for old property
76+
this.appName = clientProperties.get(ClientConfigProperties.PRODUCT_NAME.getKey()).trim();
77+
clientName = this.appName + " " + clientName; // Use the application name as client name
78+
}
79+
6780
if (this.config.isDisableFrameworkDetection()) {
6881
log.debug("Framework detection is disabled.");
6982
} else {
7083
String detectedFrameworks = Driver.FrameworksDetection.getFrameworksDetected();
7184
log.debug("Detected frameworks: {}", detectedFrameworks);
72-
clientName += " (" + detectedFrameworks + ")";
85+
if (!detectedFrameworks.trim().isEmpty()) {
86+
clientName += " (" + detectedFrameworks + ")";
87+
}
7388
}
7489

7590
this.client = this.config.applyClientProperties(new Client.Builder())
@@ -441,8 +456,6 @@ public boolean isValid(int timeout) throws SQLException {
441456
return true;
442457
}
443458

444-
private String appName = "";
445-
446459
@Override
447460
public void setClientInfo(String name, String value) throws SQLClientInfoException {
448461
if (ClientInfoProperties.APPLICATION_NAME.getKey().equals(name)) {

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.clickhouse.client.api.Client;
44
import com.clickhouse.client.api.ClientConfigProperties;
55
import com.clickhouse.jdbc.Driver;
6+
import com.google.common.collect.ImmutableMap;
67

78
import java.net.URI;
89
import java.sql.DriverPropertyInfo;
@@ -26,6 +27,9 @@ public class JdbcConfiguration {
2627
final boolean disableFrameworkDetection;
2728

2829
final Map<String, String> clientProperties;
30+
public Map<String, String> getClientProperties() {
31+
return ImmutableMap.copyOf(clientProperties);
32+
}
2933

3034
private final Map<String, String> driverProperties;
3135

@@ -62,6 +66,7 @@ public JdbcConfiguration(String url, Properties info) throws SQLException {
6266
if (bearerToken != null) {
6367
clientProperties.put(ClientConfigProperties.BEARERTOKEN_AUTH.getKey(), bearerToken);
6468
}
69+
6570
this.connectionUrl = createConnectionURL(tmpConnectionUrl, useSSL);
6671
this.isIgnoreUnsupportedRequests= Boolean.parseBoolean(getDriverProperty(DriverProperties.IGNORE_UNSUPPORTED_VALUES.getKey(), "false"));
6772
}
@@ -186,9 +191,7 @@ private void initProperties(Map<String, String> urlProperties, Properties provid
186191
}
187192
}
188193

189-
for (Map.Entry<String, String> entry : urlProperties.entrySet()) {
190-
props.put(entry.getKey(), entry.getValue());
191-
}
194+
props.putAll(urlProperties);
192195

193196
// Process all properties
194197
Map<String, DriverPropertyInfo> propertyInfos = new HashMap<>();

jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,35 @@ public void setAndGetClientInfoTest(String clientName) throws SQLException {
282282
}
283283
}
284284

285+
@Test(groups = { "integration" })
286+
public void influenceUserAgentClientNameTest() throws SQLException {
287+
String clientName = UUID.randomUUID().toString().replace("-", "");
288+
influenceUserAgentTest(clientName, "?" + ClientConfigProperties.CLIENT_NAME.getKey() + "=" + clientName);
289+
influenceUserAgentTest(clientName, "?" + ClientConfigProperties.PRODUCT_NAME.getKey() + "=" + clientName);
290+
}
291+
private void influenceUserAgentTest(String clientName, String urlParam) throws SQLException {
292+
Properties info = new Properties();
293+
info.setProperty("user", "default");
294+
info.setProperty("password", ClickHouseServerForTest.getPassword());
295+
info.setProperty(ClientConfigProperties.DATABASE.getKey(), ClickHouseServerForTest.getDatabase());
296+
297+
try (Connection localConnection = new ConnectionImpl(getEndpointString() + urlParam, info);
298+
Statement stmt = localConnection.createStatement()) {
299+
300+
final String testQuery = "SELECT '" + UUID.randomUUID() + "'";
301+
stmt.execute(testQuery);
302+
stmt.execute("SYSTEM FLUSH LOGS");
303+
304+
final String logQuery ="SELECT http_user_agent " +
305+
" FROM system.query_log WHERE query = '" + testQuery.replaceAll("'", "\\\\'") + "'";
306+
try (ResultSet rs = stmt.executeQuery(logQuery)) {
307+
Assert.assertTrue(rs.next());
308+
String userAgent = rs.getString("http_user_agent");
309+
Assert.assertTrue(userAgent.startsWith(clientName), "Expected to start with '" + clientName + "' but value was '" + userAgent + "'");
310+
}
311+
}
312+
}
313+
285314
@DataProvider(name = "setAndGetClientInfoTestDataProvider")
286315
public static Object[][] setAndGetClientInfoTestDataProvider() {
287316
return new Object[][] {

0 commit comments

Comments
 (0)