Skip to content

Commit 3274ca4

Browse files
committed
[pinpoint-apm#9794] Add tests, Change minimum version to 0.3.2
1 parent 459a9bf commit 3274ca4

File tree

24 files changed

+947
-191
lines changed

24 files changed

+947
-191
lines changed

commons/src/main/java/com/navercorp/pinpoint/common/trace/ServiceType.java

+2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@
144144
* <tr><td>2751</td><td>H2_EXECUTE_QUERY</td></tr>
145145
* <tr><td>2752</td><td>R2DBC_H2</td></tr>
146146
* <tr><td>2753</td><td>R2DBC_H2_EXECUTE_QUERY</td></tr>
147+
* <tr><td>2800</td><td>CLICK_HOUSE</td></tr>
148+
* <tr><td>2801</td><td>CLICK_HOUSE_EXECUTE_QUERY</td></tr>
147149
* </table>
148150
*
149151
* <h3>Database Sandbox (2900 ~ 2999)</h3>

plugins-it/clickhouse-jdbc-it/pom.xml

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2018 NAVER Corp.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ 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+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>com.navercorp.pinpoint</groupId>
23+
<artifactId>pinpoint-plugins-it</artifactId>
24+
<version>2.6.0-SNAPSHOT</version>
25+
</parent>
26+
27+
<artifactId>pinpoint-clickhouse-jdbc-plugin-it</artifactId>
28+
29+
<packaging>jar</packaging>
30+
31+
<properties>
32+
<jdk.version>11</jdk.version>
33+
<jdk.home>${env.JAVA_11_HOME}</jdk.home>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.navercorp.pinpoint</groupId>
39+
<artifactId>pinpoint-plugin-it-utils</artifactId>
40+
<version>${project.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.navercorp.pinpoint</groupId>
44+
<artifactId>pinpoint-plugin-it-utils-jdbc</artifactId>
45+
<version>${project.version}</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.navercorp.pinpoint</groupId>
49+
<artifactId>pinpoint-clickhouse-jdbc-driver-plugin</artifactId>
50+
<version>${project.version}</version>
51+
</dependency>
52+
<!-- testcontainers -->
53+
<dependency>
54+
<groupId>org.testcontainers</groupId>
55+
<artifactId>testcontainers</artifactId>
56+
<version>${testcontainers.version}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.testcontainers</groupId>
60+
<artifactId>clickhouse</artifactId>
61+
<version>1.19.0</version>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>com.clickhouse</groupId>
66+
<artifactId>clickhouse-jdbc</artifactId>
67+
<version>0.4.1</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.zaxxer</groupId>
71+
<artifactId>HikariCP</artifactId>
72+
<scope>compile</scope>
73+
</dependency>
74+
</dependencies>
75+
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-surefire-plugin</artifactId>
81+
<configuration>
82+
<skip>true</skip>
83+
</configuration>
84+
</plugin>
85+
<plugin>
86+
<groupId>org.apache.maven.plugins</groupId>
87+
<artifactId>maven-failsafe-plugin</artifactId>
88+
<configuration>
89+
<jvm>${env.JAVA_8_HOME}/bin/java</jvm>
90+
</configuration>
91+
</plugin>
92+
</plugins>
93+
</build>
94+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright 2023 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.navercorp.pinpoint.it.plugin.jdbc.clickhouse;
17+
18+
import com.clickhouse.jdbc.ClickHouseConnection;
19+
import com.clickhouse.jdbc.ClickHouseStatement;
20+
import com.navercorp.pinpoint.bootstrap.context.DatabaseInfo;
21+
import com.navercorp.pinpoint.bootstrap.plugin.jdbc.JdbcUrlParserV2;
22+
import com.navercorp.pinpoint.bootstrap.plugin.test.Expectations;
23+
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
24+
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
25+
import com.navercorp.pinpoint.it.plugin.utils.jdbc.DriverProperties;
26+
import com.navercorp.pinpoint.it.plugin.utils.jdbc.JDBCApi;
27+
import com.navercorp.pinpoint.it.plugin.utils.jdbc.JDBCDriverClass;
28+
import com.navercorp.pinpoint.it.plugin.utils.jdbc.template.DriverManagerDataSource;
29+
import com.navercorp.pinpoint.plugin.jdbc.clickhouse.ClickHouseConstants;
30+
import org.apache.logging.log4j.LogManager;
31+
import org.apache.logging.log4j.Logger;
32+
33+
import javax.sql.DataSource;
34+
import java.lang.reflect.Method;
35+
import java.sql.Driver;
36+
import java.sql.DriverManager;
37+
import java.sql.ResultSet;
38+
import java.sql.SQLException;
39+
40+
/**
41+
* @author intr3p1d
42+
*/
43+
public class ClickHouseITBase {
44+
private final Logger logger = LogManager.getLogger(getClass());
45+
static final String TABLE_NAME = "jdbc_example_basic";
46+
47+
protected DriverProperties driverProperties;
48+
protected ClickHouseITHelper clickHouseITHelper;
49+
50+
protected String DB_TYPE = ClickHouseConstants.CLICK_HOUSE.getName();
51+
protected String DB_EXECUTE_QUERY = ClickHouseConstants.CLICK_HOUSE_EXECUTE_QUERY.getName();
52+
53+
protected String jdbcUrl;
54+
protected String databaseId;
55+
protected String databaseIdPassword;
56+
protected String databaseAddress;
57+
protected String databaseName;
58+
protected JDBCDriverClass jdbcDriverClass;
59+
protected JDBCApi jdbcApi;
60+
private DataSource dataSource;
61+
62+
public void setup(
63+
DriverProperties driverProperties,
64+
JdbcUrlParserV2 jdbcUrlParser,
65+
JDBCDriverClass jdbcDriverClass,
66+
JDBCApi jdbcApi) {
67+
this.driverProperties = driverProperties;
68+
this.clickHouseITHelper = new ClickHouseITHelper(driverProperties);
69+
70+
this.jdbcUrl = driverProperties.getUrl();
71+
72+
DatabaseInfo databaseInfo = jdbcUrlParser.parse(jdbcUrl);
73+
74+
this.databaseAddress = databaseInfo.getHost().get(0);
75+
this.databaseName = databaseInfo.getDatabaseId();
76+
77+
this.databaseId = driverProperties.getUser();
78+
this.databaseIdPassword = driverProperties.getPassword();
79+
this.dataSource = new DriverManagerDataSource(jdbcUrl, databaseId, databaseIdPassword);
80+
81+
this.jdbcDriverClass = jdbcDriverClass;
82+
this.jdbcApi = jdbcApi;
83+
84+
try {
85+
Driver driver = jdbcDriverClass.getDriver().newInstance();
86+
DriverManager.registerDriver(driver);
87+
} catch (Exception e) {
88+
throw new RuntimeException("driver register error", e);
89+
}
90+
}
91+
92+
private ClickHouseConnection getConnection() throws SQLException {
93+
return clickHouseITHelper.getConnection();
94+
}
95+
96+
public void executeQueries() throws SQLException {
97+
98+
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
99+
verifier.printCache();
100+
101+
ClickHouseConnection conn = getConnection();
102+
String dropAndCreateQuery = String.format(
103+
"drop table if exists %1$s; create table %1$s(a String, b Nullable(String)) engine=Memory",
104+
TABLE_NAME);
105+
106+
int count;
107+
try (ClickHouseStatement stmt = conn.createStatement()) {
108+
// multi-statement query is supported by default
109+
// session will be created automatically during execution
110+
stmt.execute(dropAndCreateQuery);
111+
count = stmt.getUpdateCount();
112+
logger.info(count);
113+
}
114+
115+
String sql = "select * from " + TABLE_NAME;
116+
try (ClickHouseStatement stmt = conn.createStatement()) {
117+
// set max_result_rows = 3, result_overflow_mode = 'break'
118+
// or simply discard rows after the first 3 in read-only mode
119+
stmt.setMaxRows(3);
120+
count = 0;
121+
try (ResultSet rs = stmt.executeQuery(sql)) {
122+
while (rs.next()) {
123+
count++;
124+
}
125+
}
126+
logger.info(count);
127+
}
128+
129+
Method connect = jdbcApi.getDriver().getConnect();
130+
verifier.verifyTrace(Expectations.event(DB_TYPE, connect, null, databaseAddress, databaseName, Expectations.cachedArgs(jdbcUrl)));
131+
132+
Method execute = jdbcApi.getPreparedStatement().getExecute();
133+
verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, execute, null, databaseAddress, databaseName, Expectations.sql(dropAndCreateQuery, null)));
134+
135+
Method executeQuery = jdbcApi.getPreparedStatement().getExecuteQuery();
136+
verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, executeQuery, null, databaseAddress, databaseName, Expectations.sql(sql, null)));
137+
138+
}
139+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2023 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.navercorp.pinpoint.it.plugin.jdbc.clickhouse;
17+
18+
import com.clickhouse.jdbc.ClickHouseConnection;
19+
import com.clickhouse.jdbc.ClickHouseDriver;
20+
import com.navercorp.pinpoint.it.plugin.utils.jdbc.DriverProperties;
21+
22+
import java.sql.SQLException;
23+
import java.util.Objects;
24+
import java.util.Properties;
25+
26+
/**
27+
* @author intr3p1d
28+
*/
29+
public class ClickHouseITHelper {
30+
31+
private final String jdbcUrl;
32+
private final String userName;
33+
private final String password;
34+
35+
36+
public ClickHouseITHelper(DriverProperties driverProperties) {
37+
Objects.requireNonNull(driverProperties, "driverProperties");
38+
39+
this.jdbcUrl = driverProperties.getUrl();
40+
this.userName = driverProperties.getUser();
41+
this.password = driverProperties.getPassword();
42+
43+
}
44+
45+
public ClickHouseConnection getConnection() throws SQLException {
46+
ClickHouseDriver driver = new ClickHouseDriver();
47+
Properties properties = new Properties();
48+
properties.put("user", userName);
49+
properties.put("password", password);
50+
51+
// final Connection conn = DriverManager.getConnection(jdbcUrl, userName, password);
52+
final ClickHouseConnection conn = driver.connect(jdbcUrl, properties);
53+
54+
System.out.println("Connected to: " + conn.getMetaData().getURL());
55+
return conn;
56+
}
57+
58+
}
59+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2023 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.navercorp.pinpoint.it.plugin.jdbc.clickhouse;
17+
18+
import com.navercorp.pinpoint.it.plugin.utils.jdbc.DefaultJDBCApi;
19+
import com.navercorp.pinpoint.it.plugin.utils.jdbc.JDBCDriverClass;
20+
21+
import java.lang.reflect.Method;
22+
import java.sql.PreparedStatement;
23+
import java.util.Objects;
24+
25+
/**
26+
* @author intr3p1d
27+
*/
28+
public class ClickHouseJDBCApi extends DefaultJDBCApi {
29+
30+
public ClickHouseJDBCApi(JDBCDriverClass jdbcDriverClass) {
31+
super(jdbcDriverClass);
32+
}
33+
34+
35+
36+
public PreparedStatementClass getPreparedStatement() {
37+
return new ClickHousePreparedStatementClass(getJDBCDriverClass());
38+
}
39+
40+
41+
public static class ClickHousePreparedStatementClass implements PreparedStatementClass {
42+
private final JDBCDriverClass jdbcDriverClass;
43+
44+
public ClickHousePreparedStatementClass(JDBCDriverClass jdbcDriverClass) {
45+
this.jdbcDriverClass = Objects.requireNonNull(jdbcDriverClass, "jdbcDriverClass");
46+
}
47+
48+
protected Class<PreparedStatement> getPreparedStatement() {
49+
return jdbcDriverClass.getPreparedStatement();
50+
}
51+
52+
@Override
53+
public Method getExecute() {
54+
final Class<PreparedStatement> statement = getPreparedStatement();
55+
return getDeclaredMethod(statement, "execute", String.class);
56+
}
57+
58+
@Override
59+
public Method getExecuteQuery() {
60+
final Class<PreparedStatement> statement = getPreparedStatement();
61+
return getDeclaredMethod(statement, "executeQuery", String.class);
62+
}
63+
}
64+
65+
}

0 commit comments

Comments
 (0)