Skip to content

Commit 7005f2d

Browse files
authored
Feature/fix clickhouse with druid (#566)
1 parent d42e245 commit 7005f2d

5 files changed

Lines changed: 55 additions & 2 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Callable {
139139
* Fix NPE in guava-eventbus-plugin.
140140
* Add WebSphere Liberty 23.x plugin
141141
* Add Plugin to support aerospike Java client
142+
* Add ClickHouse parsing to the jdbc-common plugin.
142143

143144
#### Documentation
144145

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.jdbc.connectionurl.parser;
20+
21+
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
22+
23+
/**
24+
* {@link ClickHouseURLParser} parse connection url of mysql.
25+
*/
26+
public class ClickHouseURLParser extends MysqlURLParser {
27+
28+
public ClickHouseURLParser(String url) {
29+
super(url, "ClickHouse", ComponentsDefine.CLICKHOUSE_JDBC_DRIVER, 8123);
30+
}
31+
32+
}

apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/MysqlURLParser.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
public class MysqlURLParser extends AbstractURLParser {
2929

3030
private static final int DEFAULT_PORT = 3306;
31+
private int defaultPort = DEFAULT_PORT;
3132
private String dbType = "Mysql";
3233
private OfficialComponent component = ComponentsDefine.MYSQL_JDBC_DRIVER;
3334

@@ -41,6 +42,13 @@ public MysqlURLParser(String url, String dbType, OfficialComponent component) {
4142
this.component = component;
4243
}
4344

45+
public MysqlURLParser(String url, String dbType, OfficialComponent component, int defaultPort) {
46+
super(url);
47+
this.dbType = dbType;
48+
this.component = component;
49+
this.defaultPort = defaultPort;
50+
}
51+
4452
@Override
4553
protected URLLocation fetchDatabaseHostsIndexRange() {
4654
int hostLabelStartIndex = url.indexOf("//");
@@ -101,7 +109,7 @@ public ConnectionInfo parse() {
101109
StringBuilder sb = new StringBuilder();
102110
for (String host : hostSegment) {
103111
if (host.split(":").length == 1) {
104-
sb.append(host).append(":").append(DEFAULT_PORT).append(",");
112+
sb.append(host).append(":").append(defaultPort).append(",");
105113
} else {
106114
sb.append(host).append(",");
107115
}
@@ -113,7 +121,7 @@ public ConnectionInfo parse() {
113121
return new ConnectionInfo(component, dbType, hostAndPort[0], Integer.valueOf(hostAndPort[1]), fetchDatabaseNameFromURL(location
114122
.endIndex()));
115123
} else {
116-
return new ConnectionInfo(component, dbType, hostAndPort[0], DEFAULT_PORT, fetchDatabaseNameFromURL(location
124+
return new ConnectionInfo(component, dbType, hostAndPort[0], defaultPort, fetchDatabaseNameFromURL(location
117125
.endIndex()));
118126
}
119127
}

apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class URLParser {
3535
private static final String MSSQL_JDBC_URL_PREFIX = "jdbc:sqlserver:";
3636
private static final String KYLIN_JDBC_URK_PREFIX = "jdbc:kylin";
3737
private static final String IMPALA_JDBC_URK_PREFIX = "jdbc:impala";
38+
private static final String CLICKHOUSE_JDBC_URK_PREFIX = "jdbc:clickhouse";
3839

3940
public static ConnectionInfo parser(String url) {
4041
ConnectionURLParser parser = null;
@@ -57,6 +58,8 @@ public static ConnectionInfo parser(String url) {
5758
parser = new KylinJdbcURLParser(url);
5859
} else if (lowerCaseUrl.startsWith(IMPALA_JDBC_URK_PREFIX)) {
5960
parser = new ImpalaJdbcURLParser(url);
61+
} else if (lowerCaseUrl.startsWith(CLICKHOUSE_JDBC_URK_PREFIX)) {
62+
parser = new ClickHouseURLParser(url);
6063
}
6164
return parser.parse();
6265
}

apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParserTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,13 @@ public void testParseMariadbJDBCURLWithHost() {
168168
assertThat(connectionInfo.getDatabaseName(), is("test"));
169169
assertThat(connectionInfo.getDatabasePeer(), is("primaryhost:3306"));
170170
}
171+
172+
@Test
173+
public void testParseClickhouseJDBCURL() {
174+
ConnectionInfo connectionInfo = new URLParser().parser("jdbc:clickhouse://localhost:8123/test");
175+
assertThat(connectionInfo.getDBType(), is("ClickHouse"));
176+
assertThat(connectionInfo.getDatabaseName(), is("test"));
177+
assertThat(connectionInfo.getDatabasePeer(), is("localhost:8123"));
178+
}
179+
171180
}

0 commit comments

Comments
 (0)