Skip to content

Commit 699f612

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

File tree

25 files changed

+1086
-173
lines changed

25 files changed

+1086
-173
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

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
<!-- https://mvnrepository.com/artifact/com.clickhouse/clickhouse-jdbc -->
38+
<dependency>
39+
<groupId>com.clickhouse</groupId>
40+
<artifactId>clickhouse-jdbc</artifactId>
41+
<version>0.3.2-patch11</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>com.navercorp.pinpoint</groupId>
46+
<artifactId>pinpoint-plugin-it-utils</artifactId>
47+
<version>${project.version}</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.navercorp.pinpoint</groupId>
51+
<artifactId>pinpoint-plugin-it-utils-jdbc</artifactId>
52+
<version>${project.version}</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>com.navercorp.pinpoint</groupId>
56+
<artifactId>pinpoint-clickhouse-jdbc-driver-plugin</artifactId>
57+
<version>${project.version}</version>
58+
</dependency>
59+
<!-- testcontainers -->
60+
<dependency>
61+
<groupId>org.testcontainers</groupId>
62+
<artifactId>testcontainers</artifactId>
63+
<version>${testcontainers.version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.testcontainers</groupId>
67+
<artifactId>clickhouse</artifactId>
68+
<version>1.19.0</version>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>com.clickhouse</groupId>
73+
<artifactId>clickhouse-jdbc</artifactId>
74+
<version>0.4.1</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>com.zaxxer</groupId>
78+
<artifactId>HikariCP</artifactId>
79+
<scope>compile</scope>
80+
</dependency>
81+
</dependencies>
82+
83+
<build>
84+
<plugins>
85+
<plugin>
86+
<groupId>org.apache.maven.plugins</groupId>
87+
<artifactId>maven-surefire-plugin</artifactId>
88+
<configuration>
89+
<skip>true</skip>
90+
</configuration>
91+
</plugin>
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-failsafe-plugin</artifactId>
95+
<configuration>
96+
<jvm>${env.JAVA_8_HOME}/bin/java</jvm>
97+
</configuration>
98+
</plugin>
99+
</plugins>
100+
</build>
101+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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.bootstrap.context.DatabaseInfo;
19+
import com.navercorp.pinpoint.bootstrap.plugin.jdbc.JdbcUrlParserV2;
20+
import com.navercorp.pinpoint.bootstrap.plugin.test.Expectations;
21+
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
22+
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
23+
import com.navercorp.pinpoint.common.profiler.sql.DefaultSqlNormalizer;
24+
import com.navercorp.pinpoint.common.profiler.sql.NormalizedSql;
25+
import com.navercorp.pinpoint.common.profiler.sql.SqlNormalizer;
26+
import com.navercorp.pinpoint.it.plugin.utils.jdbc.DriverProperties;
27+
import com.navercorp.pinpoint.it.plugin.utils.jdbc.JDBCApi;
28+
import com.navercorp.pinpoint.it.plugin.utils.jdbc.JDBCDriverClass;
29+
import com.navercorp.pinpoint.it.plugin.utils.jdbc.template.DriverManagerDataSource;
30+
import com.navercorp.pinpoint.plugin.jdbc.clickhouse.ClickHouseConstants;
31+
import org.apache.logging.log4j.LogManager;
32+
import org.apache.logging.log4j.Logger;
33+
34+
import javax.sql.DataSource;
35+
import java.lang.reflect.Method;
36+
import java.sql.Connection;
37+
import java.sql.Driver;
38+
import java.sql.DriverManager;
39+
import java.sql.PreparedStatement;
40+
import java.sql.ResultSet;
41+
import java.sql.SQLException;
42+
import java.sql.Statement;
43+
import java.util.Collections;
44+
45+
/**
46+
* @author intr3p1d
47+
*/
48+
public class ClickHouseITBase {
49+
private final Logger logger = LogManager.getLogger(getClass());
50+
private static final String TABLE_NAME = "jdbc_example_basic";
51+
52+
private static final SqlNormalizer sqlNormalizer = new DefaultSqlNormalizer();
53+
54+
protected DriverProperties driverProperties;
55+
protected ClickHouseITHelper clickHouseITHelper;
56+
57+
protected String DB_TYPE = ClickHouseConstants.CLICK_HOUSE.getName();
58+
protected String DB_EXECUTE_QUERY = ClickHouseConstants.CLICK_HOUSE_EXECUTE_QUERY.getName();
59+
60+
protected String jdbcUrl;
61+
protected String databaseId;
62+
protected String databaseIdPassword;
63+
protected String databaseAddress;
64+
protected String databaseName;
65+
protected JDBCDriverClass jdbcDriverClass;
66+
protected JDBCApi jdbcApi;
67+
private DataSource dataSource;
68+
69+
public void setup(
70+
DriverProperties driverProperties,
71+
JdbcUrlParserV2 jdbcUrlParser,
72+
JDBCDriverClass jdbcDriverClass,
73+
JDBCApi jdbcApi
74+
) {
75+
this.driverProperties = driverProperties;
76+
this.clickHouseITHelper = new ClickHouseITHelper(driverProperties);
77+
78+
this.jdbcUrl = driverProperties.getUrl();
79+
80+
DatabaseInfo databaseInfo = jdbcUrlParser.parse(jdbcUrl);
81+
82+
this.databaseAddress = databaseInfo.getHost().get(0);
83+
this.databaseName = databaseInfo.getDatabaseId();
84+
85+
this.databaseId = driverProperties.getUser();
86+
this.databaseIdPassword = driverProperties.getPassword();
87+
this.dataSource = new DriverManagerDataSource(jdbcUrl, databaseId, databaseIdPassword);
88+
89+
this.jdbcDriverClass = jdbcDriverClass;
90+
this.jdbcApi = jdbcApi;
91+
92+
try {
93+
Driver driver = jdbcDriverClass.getDriver().newInstance();
94+
DriverManager.registerDriver(driver);
95+
} catch (Exception e) {
96+
throw new RuntimeException("driver register error", e);
97+
}
98+
}
99+
100+
private Connection getConnection() throws SQLException {
101+
return clickHouseITHelper.getConnection();
102+
}
103+
104+
public void testStatements() throws SQLException {
105+
106+
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
107+
108+
Connection conn = getConnection();
109+
110+
String sql1 = String.format(
111+
"drop table if exists %1$s; create table %1$s(a String, b Nullable(String)) engine=Memory",
112+
TABLE_NAME);
113+
String sql2 = "select * from " + TABLE_NAME;
114+
String sql3 = "select * from " + TABLE_NAME;
115+
116+
try (Statement stmt = conn.createStatement()) {
117+
stmt.execute(sql1);
118+
}
119+
120+
try (Statement stmt = conn.createStatement()) {
121+
stmt.setMaxRows(3);
122+
int count = 0;
123+
try (ResultSet rs = stmt.executeQuery(sql2)) {
124+
while (rs.next()) {
125+
count++;
126+
}
127+
}
128+
}
129+
130+
try (Statement stmt = conn.createStatement()) {
131+
stmt.setMaxRows(3);
132+
int count = stmt.executeUpdate(sql3);
133+
}
134+
135+
verifier.printCache();
136+
137+
Method connect = jdbcApi.getDriver().getConnect();
138+
verifier.verifyTrace(Expectations.event(DB_TYPE, connect, null, databaseAddress, databaseName, Expectations.cachedArgs(jdbcUrl)));
139+
140+
Method execute = jdbcApi.getStatement().getExecute();
141+
verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, execute, null, databaseAddress, databaseName, Expectations.sql(sql1, null)));
142+
143+
Method executeQuery = jdbcApi.getStatement().getExecuteQuery();
144+
verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, executeQuery, null, databaseAddress, databaseName, Expectations.sql(sql2, null)));
145+
146+
Method executeUpdate = jdbcApi.getStatement().getExecuteUpdate();
147+
verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, executeUpdate, null, databaseAddress, databaseName, Expectations.sql(sql3, null)));
148+
149+
}
150+
151+
public void testPreparedStatements() throws SQLException {
152+
153+
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
154+
155+
Connection conn = getConnection();
156+
157+
String sql0 = "INSERT INTO test (name, age) VALUES (?, 5)";
158+
159+
String sql1 = "drop table if exists t_map;"
160+
+ "CREATE TABLE t_map"
161+
+ "("
162+
+ " `audit_seq` Int64 CODEC(Delta(8), LZ4),"
163+
+ "`timestamp` Int64 CODEC(Delta(8), LZ4),"
164+
+ "`event_type` LowCardinality(String),"
165+
+ "`event_subtype` LowCardinality(String),"
166+
+ "`actor_type` LowCardinality(String),"
167+
+ "`actor_id` String,"
168+
+ "`actor_tenant_id` LowCardinality(String),"
169+
+ "`actor_tenant_name` String,"
170+
+ "`actor_firstname` String,"
171+
+ "`actor_lastname` String,"
172+
+ "`resource_type` LowCardinality(String),"
173+
+ "`resource_id` String,"
174+
+ "`resource_container` LowCardinality(String),"
175+
+ "`resource_path` String,"
176+
+ "`origin_ip` String,"
177+
+ "`origin_app_name` LowCardinality(String),"
178+
+ "`origin_app_instance` String,"
179+
+ "`description` String,"
180+
+ "`attributes` Map(String, String)"
181+
+ ")"
182+
+ "ENGINE = MergeTree "
183+
+ "ORDER BY (resource_container, event_type, event_subtype) "
184+
+ "SETTINGS index_granularity = 8192";
185+
String sql2 = "INSERT INTO t_map SETTINGS async_insert=1,wait_for_async_insert=1 " +
186+
"VALUES (8481365034795008,1673349039830,'operation-9','a','service', 'bc3e47b8-2b34-4c1a-9004-123656fa0000','b', 'c', 'service-56','d', 'object','e', 'my-value-62', 'mypath', 'some.hostname.address.com', 'app-9', 'instance-6','x', ?)";
187+
188+
189+
try (PreparedStatement stmt = conn.prepareStatement(sql0)) {
190+
stmt.setString(1, "maru");
191+
stmt.execute();
192+
}
193+
194+
try (Statement s = conn.createStatement()) {
195+
s.execute(sql1);
196+
}
197+
198+
try (PreparedStatement stmt = conn.prepareStatement(sql2)) {
199+
stmt.setObject(1, Collections.singletonMap("key1", "value1"));
200+
stmt.execute();
201+
}
202+
203+
verifier.printCache();
204+
205+
Method connect = jdbcApi.getDriver().getConnect();
206+
verifier.verifyTrace(Expectations.event(DB_TYPE, connect, null, databaseAddress, databaseName, Expectations.cachedArgs(jdbcUrl)));
207+
208+
NormalizedSql normalizedSql0 = normalize(sql0);
209+
210+
Method prepare1 = jdbcApi.getConnection().getPrepareStatement();
211+
verifier.verifyTrace(Expectations.event(DB_TYPE, prepare1, null, databaseAddress, databaseName,
212+
Expectations.sql(normalizedSql0.getNormalizedSql(), normalizedSql0.getParseParameter())));
213+
214+
Method executePrepared1 = jdbcApi.getPreparedStatement().getExecute();
215+
verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, executePrepared1, null, databaseAddress, databaseName,
216+
Expectations.sql(normalizedSql0.getNormalizedSql(), normalizedSql0.getParseParameter(), "maru")));
217+
218+
219+
NormalizedSql normalizedSql1 = normalize(sql1);
220+
Method execute = jdbcApi.getStatement().getExecute();
221+
verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, execute, null, databaseAddress, databaseName,
222+
Expectations.sql(normalizedSql1.getNormalizedSql(), normalizedSql1.getParseParameter())));
223+
224+
NormalizedSql normalizedSql2 = normalize(sql2);
225+
Method prepare2 = jdbcApi.getConnection().getPrepareStatement();
226+
verifier.verifyTrace(Expectations.event(DB_TYPE, prepare2, null, databaseAddress, databaseName,
227+
Expectations.sql(normalizedSql2.getNormalizedSql(), normalizedSql2.getParseParameter())));
228+
229+
Method executePrepared2 = jdbcApi.getPreparedStatement().getExecute();
230+
verifier.verifyTrace(Expectations.event(DB_EXECUTE_QUERY, executePrepared2, null, databaseAddress, databaseName,
231+
Expectations.sql(normalizedSql2.getNormalizedSql(), normalizedSql2.getParseParameter(), "java.util.Collections$SingletonMap")));
232+
}
233+
234+
private NormalizedSql normalize(String sql) {
235+
return sqlNormalizer.normalizeSql(sql);
236+
}
237+
238+
}

0 commit comments

Comments
 (0)