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 all 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");

public static String frameworksDetected = null;

public 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,6 @@ public static Map<ClickHouseOption, Serializable> toClientOptions(Properties pro
options.put(o, ClickHouseOption.fromString(e.getValue().toString(), o.getValueType()));
}
}

return options;
}

Expand All @@ -128,7 +146,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,11 @@ public ClickHouseConnectionImpl(String url, Properties properties) throws SQLExc
public ClickHouseConnectionImpl(ConnectionInfo connInfo) throws SQLException {
Properties props = connInfo.getProperties();
jvmTimeZone = TimeZone.getDefault();

if (props.get("disable_frameworks_detection") == null || !props.get("disable_frameworks_detection").toString().equalsIgnoreCase("true")) {
ClickHouseDriver.frameworksDetected = ClickHouseDriver.FrameworksDetection.getFrameworksDetected();
if (ClickHouseDriver.frameworksDetected != null)
props.setProperty(ClickHouseClientOption.PRODUCT_NAME.getKey(), props.getProperty(ClickHouseClientOption.PRODUCT_NAME.getKey()) + ClickHouseDriver.frameworksDetected);
}
ClickHouseClientBuilder clientBuilder = ClickHouseClient.builder()
.options(ClickHouseDriver.toClientOptions(props))
.defaultCredentials(connInfo.getDefaultCredentials());
Expand Down
Loading