Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jdbc] Enhance useragent to indicate Apache Spark #1890

Merged
merged 5 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;

import com.clickhouse.client.ClickHouseClient;
import com.clickhouse.client.ClickHouseConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.*;
import java.util.Map.Entry;

import com.clickhouse.client.ClickHouseClient;
Expand Down Expand Up @@ -49,6 +42,32 @@ public class ClickHouseDriver implements Driver {

static final java.util.logging.Logger parentLogger = java.util.logging.Logger.getLogger("com.clickhouse.jdbc");

static String frameworksDetected = null;

private static class FrameworksDetection {
private static final List<String> FRAMEWORKS_TO_DETECT = List.of("apache.spark");
static volatile String frameworksDetected = null;

private FrameworksDetection() {
}
public static String getFrameworksDetected() {
if (frameworksDetected == null) {
Set<String> inferredFrameworks = new LinkedHashSet<>();
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
for (String framework : FRAMEWORKS_TO_DETECT) {
if (ste.toString().contains(framework)) {
inferredFrameworks.add(String.format("(%s)", framework));
}
}
}

frameworksDetected = String.join("; ", inferredFrameworks);
}
return frameworksDetected;
}

}

static {
String str = ClickHouseDriver.class.getPackage().getImplementationVersion();
if (str != null && !str.isEmpty()) {
Expand Down Expand Up @@ -115,7 +134,8 @@ public static Map<ClickHouseOption, Serializable> toClientOptions(Properties pro
options.put(o, ClickHouseOption.fromString(e.getValue().toString(), o.getValueType()));
}
}

if (frameworksDetected != null)
options.put(ClickHouseClientOption.PRODUCT_NAME, frameworksDetected);
return options;
}

Expand All @@ -128,7 +148,7 @@ private DriverPropertyInfo create(ClickHouseOption option, Properties props) {

Class<?> clazz = option.getValueType();
if (Boolean.class == clazz || boolean.class == clazz) {
propInfo.choices = new String[] { "true", "false" };
propInfo.choices = new String[]{"true", "false"};
} else if (clazz.isEnum()) {
Object[] values = clazz.getEnumConstants();
String[] names = new String[values.length];
Expand All @@ -152,6 +172,8 @@ public ClickHouseConnection connect(String url, Properties info) throws SQLExcep
if (!acceptsURL(url)) {
return null;
}
if (!url.toLowerCase().contains("disable-frameworks-detection"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not every case passes properties thru url - often properties are used.
I suggest do this check in com.clickhouse.jdbc.internal.ClickHouseConnectionImpl#ClickHouseConnectionImpl(com.clickhouse.jdbc.internal.ClickHouseJdbcUrlParser.ConnectionInfo) - this method is called right after URL is parsed for properties so ConnectionInfo contains all of them.

frameworksDetected = FrameworksDetection.getFrameworksDetected();

log.debug("Creating connection");
return new ClickHouseConnectionImpl(url, info);
Expand Down
Loading