Skip to content

Commit

Permalink
[pinpoint-apm#9794] Add tests, Change minimum version to 0.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
intr3p1d committed Oct 13, 2023
1 parent ed97e0f commit 9779623
Show file tree
Hide file tree
Showing 19 changed files with 812 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@
* <tr><td>2751</td><td>H2_EXECUTE_QUERY</td></tr>
* <tr><td>2752</td><td>R2DBC_H2</td></tr>
* <tr><td>2753</td><td>R2DBC_H2_EXECUTE_QUERY</td></tr>
* <tr><td>2800</td><td>CLICK_HOUSE</td></tr>
* <tr><td>2801</td><td>CLICK_HOUSE_EXECUTE_QUERY</td></tr>
* </table>
*
* <h3>Database Sandbox (2900 ~ 2999)</h3>
Expand Down
94 changes: 94 additions & 0 deletions plugins-it/clickhouse-jdbc-it/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2018 NAVER Corp.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-plugins-it</artifactId>
<version>2.6.0-SNAPSHOT</version>
</parent>

<artifactId>pinpoint-clickhouse-jdbc-plugin-it</artifactId>

<packaging>jar</packaging>

<properties>
<jdk.version>11</jdk.version>
<jdk.home>${env.JAVA_11_HOME}</jdk.home>
</properties>

<dependencies>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-plugin-it-utils</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-plugin-it-utils-jdbc</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-clickhouse-jdbc-driver-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<!-- testcontainers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>clickhouse</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.3.2-patch11</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<jvm>${env.JAVA_8_HOME}/bin/java</jvm>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright 2023 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.it.plugin.jdbc.clickhouse;

import com.clickhouse.jdbc.ClickHouseConnection;
import com.clickhouse.jdbc.ClickHouseStatement;
import com.navercorp.pinpoint.bootstrap.plugin.jdbc.JdbcUrlParserV2;
import com.navercorp.pinpoint.bootstrap.plugin.test.Expectations;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder;
import com.navercorp.pinpoint.it.plugin.utils.jdbc.DataBaseTestCase;
import com.navercorp.pinpoint.it.plugin.utils.jdbc.DefaultJDBCApi;
import com.navercorp.pinpoint.it.plugin.utils.jdbc.DriverProperties;
import com.navercorp.pinpoint.it.plugin.utils.jdbc.JDBCApi;
import com.navercorp.pinpoint.it.plugin.utils.jdbc.JDBCDriverClass;
import com.navercorp.pinpoint.it.plugin.utils.jdbc.testcontainers.DatabaseContainers;
import com.navercorp.pinpoint.plugin.jdbc.clickhouse.ClickHouseJdbcUrlParser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;

import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.net.URI;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;

/**
* @author intr3p1d
*/
public class ClickHouseITBase {
private final Logger logger = LogManager.getLogger(getClass());
static final String TABLE_NAME = "jdbc_example_basic";

protected static DriverProperties driverProperties = DatabaseContainers.readSystemProperties();
private final ClickHouseITHelper clickHouseITHelper = new ClickHouseITHelper(driverProperties);


public static DriverProperties getDriverProperties() {
return driverProperties;
}

private ClickHouseConnection getConnection() throws SQLException {
return clickHouseITHelper.getConnection();
}

public void dropAndCreateTable() throws SQLException {

PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();

ClickHouseConnection conn = getConnection();
String dropAndCreateQuery = String.format(
"drop table if exists %1$s; create table %1$s(a String, b Nullable(String)) engine=Memory",
TABLE_NAME);

int count;
try (ClickHouseStatement stmt = conn.createStatement()) {
// multi-statement query is supported by default
// session will be created automatically during execution
stmt.execute(dropAndCreateQuery);
count = stmt.getUpdateCount();
}

JDBCApi jdbcApi = new DefaultJDBCApi(new ClickHouseJDBCDriverClass());

Method connect = jdbcApi.getDriver().getConnect();
logger.info(verifier.getExecutedMethod());
verifier.verifyTrace(Expectations.event("CLICK_HOUSE", connect, null, "test", "test", Expectations.cachedArgs("test")));

// Method execute = jdbcApi.getPreparedStatement().getExecute();
// verifier.verifyTrace(Expectations.event("CLICK_HOUSE_EXECUTE_QUERY", execute, null, "test", "test", Expectations.sql(dropAndCreateQuery, null)));
}

public int query(ClickHouseConnection conn) throws SQLException {
String sql = "select * from " + TABLE_NAME;
try (ClickHouseStatement stmt = conn.createStatement()) {
// set max_result_rows = 3, result_overflow_mode = 'break'
// or simply discard rows after the first 3 in read-only mode
stmt.setMaxRows(3);
int count = 0;
try (ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
count++;
}
}
return count;
}
}

public void insertByteArray(ClickHouseConnection conn) throws SQLException {
try (ClickHouseStatement s = conn.createStatement()) {
s.execute("drop table if exists t_map;"
+ "CREATE TABLE t_map"
+ "("
+ " `audit_seq` Int64 CODEC(Delta(8), LZ4),"
+ "`timestamp` Int64 CODEC(Delta(8), LZ4),"
+ "`event_type` LowCardinality(String),"
+ "`event_subtype` LowCardinality(String),"
+ "`actor_type` LowCardinality(String),"
+ "`actor_id` String,"
+ "`actor_tenant_id` LowCardinality(String),"
+ "`actor_tenant_name` String,"
+ "`actor_firstname` String,"
+ "`actor_lastname` String,"
+ "`resource_type` LowCardinality(String),"
+ "`resource_id` String,"
+ "`resource_container` LowCardinality(String),"
+ "`resource_path` String,"
+ "`origin_ip` String,"
+ "`origin_app_name` LowCardinality(String),"
+ "`origin_app_instance` String,"
+ "`description` String,"
+ "`attributes` Map(String, String)"
+ ")"
+ "ENGINE = MergeTree "
+ "ORDER BY (resource_container, event_type, event_subtype) "
+ "SETTINGS index_granularity = 8192");
try (PreparedStatement stmt = conn.prepareStatement(
"INSERT INTO t_map SETTINGS async_insert=1,wait_for_async_insert=1 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', ?)")) {
stmt.setObject(1, Collections.singletonMap("key1", "value1"));
stmt.execute();

try (ResultSet rs = s.executeQuery("select attributes from t_map")) {
logger.info(rs.next());
logger.info(rs.getObject(1));
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2023 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.it.plugin.jdbc.clickhouse;

import com.clickhouse.jdbc.ClickHouseConnection;
import com.clickhouse.jdbc.ClickHouseDriver;
import com.navercorp.pinpoint.it.plugin.utils.jdbc.DriverProperties;

import java.sql.SQLException;
import java.util.Objects;
import java.util.Properties;

/**
* @author intr3p1d
*/
public class ClickHouseITHelper {

private final String jdbcUrl;
private final String userName;
private final String password;


public ClickHouseITHelper(DriverProperties driverProperties) {
Objects.requireNonNull(driverProperties, "driverProperties");

this.jdbcUrl = driverProperties.getUrl();
this.userName = driverProperties.getUser();
this.password = driverProperties.getPassword();

}

public ClickHouseConnection getConnection() throws SQLException {
ClickHouseDriver driver = new ClickHouseDriver();
Properties properties = new Properties();
properties.put("user", userName);
properties.put("password", password);

// final Connection conn = DriverManager.getConnection(jdbcUrl, userName, password);
final ClickHouseConnection conn = driver.connect(jdbcUrl, properties);

System.out.println("Connected to: " + conn.getMetaData().getURL());
return conn;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2023 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.it.plugin.jdbc.clickhouse;

import com.navercorp.pinpoint.it.plugin.utils.jdbc.AbstractJDBCDriverClass;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.Statement;

/**
* @author intr3p1d
*/
public class ClickHouseJDBCDriverClass extends AbstractJDBCDriverClass {
@Override
public Class<Driver> getDriver() {
return forName("com.clickhouse.jdbc.ClickHouseDriver");
}

@Override
public Class<Connection> getConnection() {
return forName("com.clickhouse.jdbc.ClickHouseConnection");
}

@Override
public Class<Statement> getStatement() {
return forName("com.clickhouse.jdbc.ClickHouseStatement");
}

@Override
public Class<PreparedStatement> getPreparedStatement() {
return forName("com.clickhouse.jdbc.internal.InputBasedPreparedStatement");
}

@Override
public Class<CallableStatement> getCallableStatement() {
// Unsupported
return null;
}
}
Loading

0 comments on commit 9779623

Please sign in to comment.