From 3e97740036242cf4ee8808b737727a7093b3d31f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=9A=E4=BD=99=E5=B8=83=E9=81=93=E5=B8=88?= Date: Mon, 20 Sep 2021 15:21:57 +0800 Subject: [PATCH] Add HikariCP connection pool plug-in. (#29) --- .github/workflows/plugins-test.3.yaml | 1 + CHANGES.md | 1 + .../trace/component/ComponentsDefine.java | 2 + .../hikaricp-3.x-4.x-plugin/pom.xml | 44 ++++ .../PoolingCloseConnectInterceptor.java | 50 +++++ .../PoolingGetConnectInterceptor.java | 50 +++++ .../HikariDataSourceInstrumentation.java | 97 +++++++++ .../HikariProxyConnectionInstrumentation.java | 80 +++++++ .../src/main/resources/skywalking-plugin.def | 18 ++ apm-sniffer/apm-sdk-plugin/pom.xml | 1 + .../service-agent/java-agent/Plugin-list.md | 1 + .../java-agent/Supported-list.md | 1 + .../hikaricp-scenario/bin/startup.sh | 21 ++ .../config/expectedData.yaml | 205 ++++++++++++++++++ .../hikaricp-scenario/configuration.yml | 32 +++ .../scenarios/hikaricp-scenario/pom.xml | 126 +++++++++++ .../src/main/assembly/assembly.xml | 41 ++++ .../apm/testcase/hikaricp/Application.java | 34 +++ .../apm/testcase/hikaricp/MySQLConfig.java | 57 +++++ .../hikaricp/controller/CaseController.java | 58 +++++ .../hikaricp/service/CaseService.java | 65 ++++++ .../src/main/resources/application.yaml | 23 ++ .../src/main/resources/jdbc.properties | 18 ++ .../src/main/resources/log4j2.xml | 30 +++ .../hikaricp-scenario/support-version.list | 22 ++ 25 files changed, 1078 insertions(+) create mode 100644 apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/pom.xml create mode 100644 apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/PoolingCloseConnectInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/PoolingGetConnectInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/define/HikariDataSourceInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/define/HikariProxyConnectionInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/resources/skywalking-plugin.def create mode 100644 test/plugin/scenarios/hikaricp-scenario/bin/startup.sh create mode 100644 test/plugin/scenarios/hikaricp-scenario/config/expectedData.yaml create mode 100644 test/plugin/scenarios/hikaricp-scenario/configuration.yml create mode 100644 test/plugin/scenarios/hikaricp-scenario/pom.xml create mode 100644 test/plugin/scenarios/hikaricp-scenario/src/main/assembly/assembly.xml create mode 100644 test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/Application.java create mode 100644 test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/MySQLConfig.java create mode 100644 test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/controller/CaseController.java create mode 100644 test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/service/CaseService.java create mode 100644 test/plugin/scenarios/hikaricp-scenario/src/main/resources/application.yaml create mode 100644 test/plugin/scenarios/hikaricp-scenario/src/main/resources/jdbc.properties create mode 100644 test/plugin/scenarios/hikaricp-scenario/src/main/resources/log4j2.xml create mode 100644 test/plugin/scenarios/hikaricp-scenario/support-version.list diff --git a/.github/workflows/plugins-test.3.yaml b/.github/workflows/plugins-test.3.yaml index db7f40315..979946aa4 100644 --- a/.github/workflows/plugins-test.3.yaml +++ b/.github/workflows/plugins-test.3.yaml @@ -89,6 +89,7 @@ jobs: - neo4j-4.x-scenario - oracle-scenario - druid-1.x-scenario + - hikaricp-scenario steps: - uses: actions/checkout@v2 with: diff --git a/CHANGES.md b/CHANGES.md index f2f1e2f65..f39b0b9c2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -20,6 +20,7 @@ Release Notes. * Add thrift plugin support thrift TMultiplexedProcessor. * Add benchmark result for `exception-ignore` plugin and polish plugin guide. * Provide Alibaba Druid database connection pool plugin. +* Provide HikariCP database connection pool plugin. #### Documentation diff --git a/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java b/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java index 626ba2c4a..cc9ebd3e9 100755 --- a/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java +++ b/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java @@ -209,4 +209,6 @@ public class ComponentsDefine { public static final OfficialComponent ALIBABA_DRUID = new OfficialComponent(115, "AlibabaDruid"); + public static final OfficialComponent HIKARI_CP = new OfficialComponent(116, "HikariCP"); + } diff --git a/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/pom.xml new file mode 100644 index 000000000..ccab5f7a4 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/pom.xml @@ -0,0 +1,44 @@ + + + + + + apm-sdk-plugin + org.apache.skywalking + 8.8.0-SNAPSHOT + + 4.0.0 + + apm-hikaricp-3.x-4.x-plugin + jar + hikaricp-3.x-4.x-plugin + + + 3.2.0 + + + + + com.zaxxer + HikariCP + ${hikaricp.version} + provided + + + + \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/PoolingCloseConnectInterceptor.java b/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/PoolingCloseConnectInterceptor.java new file mode 100644 index 000000000..5e3fbc981 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/PoolingCloseConnectInterceptor.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.hikaricp; + +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; + +/** + * {@link PoolingCloseConnectInterceptor} intercepted the method of HikariCP closing/returning connection. + */ +public class PoolingCloseConnectInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + AbstractSpan span = ContextManager.createLocalSpan("HikariCP/Connection/" + method.getName()); + span.setComponent(ComponentsDefine.HIKARI_CP); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().errorOccurred().log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/PoolingGetConnectInterceptor.java b/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/PoolingGetConnectInterceptor.java new file mode 100644 index 000000000..106851f80 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/PoolingGetConnectInterceptor.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.hikaricp; + +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; + +/** + * {@link PoolingGetConnectInterceptor} intercepted the method of HikariCP getting connection. + */ +public class PoolingGetConnectInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + AbstractSpan span = ContextManager.createLocalSpan("HikariCP/Connection/" + method.getName()); + span.setComponent(ComponentsDefine.HIKARI_CP); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().errorOccurred().log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/define/HikariDataSourceInstrumentation.java b/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/define/HikariDataSourceInstrumentation.java new file mode 100644 index 000000000..6273cfd30 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/define/HikariDataSourceInstrumentation.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.hikaricp.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.StaticMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; +import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +/** + * HikariCP is a solid, high-performance, JDBC connection pool at last. Starting from Spring Boot 2.0, + * It is the default JDBC Connection Pool. + *

+ * HikariDataSource provides a "one stop" solution for database connection pool solution + * basic requirements. HikariDataSource#getConnection() or HikariDataSource#getConnection(String, String) + * creates (if necessary) and return a connection. + */ +public class HikariDataSourceInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + private static final String ENHANCE_CLASS = "com.zaxxer.hikari.HikariDataSource"; + private static final String ENHANCE_METHOD = "getConnection"; + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.hikaricp.PoolingGetConnectInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named(ENHANCE_METHOD); + } + + @Override + public String getMethodsInterceptor() { + return INTERCEPTOR_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named(ENHANCE_METHOD).and(takesArguments(String.class, String.class)); + } + + @Override + public String getMethodsInterceptor() { + return INTERCEPTOR_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + public StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() { + return new StaticMethodsInterceptPoint[0]; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/define/HikariProxyConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/define/HikariProxyConnectionInstrumentation.java new file mode 100644 index 000000000..cf665d6b7 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/hikaricp/define/HikariProxyConnectionInstrumentation.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.plugin.hikaricp.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.StaticMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +/** + * HikariCP is a solid, high-performance, JDBC connection pool at last. Starting from Spring Boot 2.0, + * It is the default JDBC Connection Pool. + *

+ * HikariCP use HikariProxyConnection which is the delegating implementation of Connection. All of + * the methods from the Connection interface simply check to see that the Connection is active. + * In parent ProxyConnection#close() close/return connection. + */ +public class HikariProxyConnectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + private static final String ENHANCE_CLASS = "com.zaxxer.hikari.pool.ProxyConnection"; + private static final String ENHANCE_METHOD = "close"; + private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.hikaricp.PoolingCloseConnectInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named(ENHANCE_METHOD); + } + + @Override + public String getMethodsInterceptor() { + return INTERCEPTOR_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + public StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() { + return new StaticMethodsInterceptPoint[0]; + } +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/resources/skywalking-plugin.def new file mode 100644 index 000000000..80d38e88c --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/hikaricp-3.x-4.x-plugin/src/main/resources/skywalking-plugin.def @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +hikaricp-3.x/4.x=org.apache.skywalking.apm.plugin.hikaricp.define.HikariProxyConnectionInstrumentation +hikaricp-3.x/4.x=org.apache.skywalking.apm.plugin.hikaricp.define.HikariDataSourceInstrumentation \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/pom.xml index 67b0860ac..5a302f4ef 100644 --- a/apm-sniffer/apm-sdk-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/pom.xml @@ -108,6 +108,7 @@ neo4j-4.x-plugin shardingsphere-plugins druid-1.x-plugin + hikaricp-3.x-4.x-plugin pom diff --git a/docs/en/setup/service-agent/java-agent/Plugin-list.md b/docs/en/setup/service-agent/java-agent/Plugin-list.md index 717438402..0e027ee1e 100644 --- a/docs/en/setup/service-agent/java-agent/Plugin-list.md +++ b/docs/en/setup/service-agent/java-agent/Plugin-list.md @@ -27,6 +27,7 @@ - guava-cache - h2-1.x - hbase-1.x/2.x +- hikaricp-3.x/4.x - httpasyncclient-4.x - httpclient-3.x - httpclient-4.x diff --git a/docs/en/setup/service-agent/java-agent/Supported-list.md b/docs/en/setup/service-agent/java-agent/Supported-list.md index 27e4270eb..bc4df7100 100644 --- a/docs/en/setup/service-agent/java-agent/Supported-list.md +++ b/docs/en/setup/service-agent/java-agent/Supported-list.md @@ -123,6 +123,7 @@ metrics based on the tracing data. * Pool * [Apache Commons DBCP](https://github.com/apache/commons-dbcp) 2.x * [Alibaba Druid](https://github.com/alibaba/druid) 1.x + * [HikariCP](https://github.com/brettwooldridge/HikariCP) 3.x -> 4.x * Logging Framework * [log4j](https://github.com/apache/log4j) 2.x * [log4j2](https://github.com/apache/logging-log4j2) 1.2.x diff --git a/test/plugin/scenarios/hikaricp-scenario/bin/startup.sh b/test/plugin/scenarios/hikaricp-scenario/bin/startup.sh new file mode 100644 index 000000000..85e4ab69c --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/bin/startup.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +home="$(cd "$(dirname $0)"; pwd)" + +java -jar ${agent_opts} ${home}/../libs/hikaricp-scenario.jar & \ No newline at end of file diff --git a/test/plugin/scenarios/hikaricp-scenario/config/expectedData.yaml b/test/plugin/scenarios/hikaricp-scenario/config/expectedData.yaml new file mode 100644 index 000000000..b9ab3d17a --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/config/expectedData.yaml @@ -0,0 +1,205 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +segmentItems: +- serviceName: hikaricp-scenario + segmentSize: ge 0 + segments: + - segmentId: not null + spans: + - operationName: HikariCP/Connection/getConnection + parentSpanId: 0 + spanId: 1 + spanLayer: Unknown + startTime: gt 0 + endTime: gt 0 + componentId: 116 + isError: false + spanType: Local + peer: '' + - operationName: Mysql/JDBI/Statement/execute + parentSpanId: 0 + spanId: 2 + spanLayer: Database + startTime: gt 0 + endTime: gt 0 + componentId: 33 + isError: false + spanType: Exit + peer: mysql-server:3306 + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: test} + - {key: db.statement, value: 'CREATE TABLE test_Hikari(id VARCHAR(1) PRIMARY + KEY, value VARCHAR(1) NOT NULL)'} + - operationName: HikariCP/Connection/close + parentSpanId: 0 + spanId: 3 + spanLayer: Unknown + startTime: gt 0 + endTime: gt 0 + componentId: 116 + isError: false + spanType: Local + peer: '' + - operationName: HikariCP/Connection/getConnection + parentSpanId: 0 + spanId: 4 + spanLayer: Unknown + startTime: gt 0 + endTime: gt 0 + componentId: 116 + isError: false + spanType: Local + peer: '' + - operationName: Mysql/JDBI/Statement/execute + parentSpanId: 0 + spanId: 5 + spanLayer: Database + startTime: gt 0 + endTime: gt 0 + componentId: 33 + isError: false + spanType: Exit + peer: mysql-server:3306 + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: test} + - {key: db.statement, value: 'INSERT INTO test_Hikari(id, value) VALUES(1,1)'} + - operationName: HikariCP/Connection/close + parentSpanId: 0 + spanId: 6 + spanLayer: Unknown + startTime: gt 0 + endTime: gt 0 + componentId: 116 + isError: false + spanType: Local + peer: '' + - operationName: HikariCP/Connection/getConnection + parentSpanId: 0 + spanId: 7 + spanLayer: Unknown + startTime: gt 0 + endTime: gt 0 + componentId: 116 + isError: false + spanType: Local + peer: '' + - operationName: Mysql/JDBI/Statement/execute + parentSpanId: 0 + spanId: 8 + spanLayer: Database + startTime: gt 0 + endTime: gt 0 + componentId: 33 + isError: false + spanType: Exit + peer: mysql-server:3306 + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: test} + - {key: db.statement, value: 'SELECT id, value FROM test_Hikari WHERE id=1'} + - operationName: HikariCP/Connection/close + parentSpanId: 0 + spanId: 9 + spanLayer: Unknown + startTime: gt 0 + endTime: gt 0 + componentId: 116 + isError: false + spanType: Local + peer: '' + - operationName: HikariCP/Connection/getConnection + parentSpanId: 0 + spanId: 10 + spanLayer: Unknown + startTime: gt 0 + endTime: gt 0 + componentId: 116 + isError: false + spanType: Local + peer: '' + - operationName: Mysql/JDBI/Statement/execute + parentSpanId: 0 + spanId: 11 + spanLayer: Database + startTime: gt 0 + endTime: gt 0 + componentId: 33 + isError: false + spanType: Exit + peer: mysql-server:3306 + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: test} + - {key: db.statement, value: 'DELETE FROM test_Hikari WHERE id=1'} + - operationName: HikariCP/Connection/close + parentSpanId: 0 + spanId: 12 + spanLayer: Unknown + startTime: gt 0 + endTime: gt 0 + componentId: 116 + isError: false + spanType: Local + peer: '' + - operationName: HikariCP/Connection/getConnection + parentSpanId: 0 + spanId: 13 + spanLayer: Unknown + startTime: gt 0 + endTime: gt 0 + componentId: 116 + isError: false + spanType: Local + peer: '' + - operationName: Mysql/JDBI/Statement/execute + parentSpanId: 0 + spanId: 14 + spanLayer: Database + startTime: gt 0 + endTime: gt 0 + componentId: 33 + isError: false + spanType: Exit + peer: mysql-server:3306 + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: test} + - {key: db.statement, value: 'DROP table test_Hikari'} + - operationName: HikariCP/Connection/close + parentSpanId: 0 + spanId: 15 + spanLayer: Unknown + startTime: gt 0 + endTime: gt 0 + componentId: 116 + isError: false + spanType: Local + peer: '' + - operationName: /hikaricp-scenario/case/hikaricp-scenario + parentSpanId: -1 + spanId: 0 + spanLayer: Http + startTime: gt 0 + endTime: gt 0 + componentId: 1 + isError: false + spanType: Entry + peer: '' + tags: + - {key: url, value: 'http://localhost:8080/hikaricp-scenario/case/hikaricp-scenario'} + - {key: http.method, value: GET} diff --git a/test/plugin/scenarios/hikaricp-scenario/configuration.yml b/test/plugin/scenarios/hikaricp-scenario/configuration.yml new file mode 100644 index 000000000..63c187113 --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/configuration.yml @@ -0,0 +1,32 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +type: jvm +entryService: http://localhost:8080/hikaricp-scenario/case/hikaricp-scenario +healthCheck: http://localhost:8080/hikaricp-scenario/case/healthCheck +startScript: ./bin/startup.sh +environment: +depends_on: + - mysql-server +dependencies: + mysql-server: + image: mysql:5.7 + hostname: mysql-server + expose: + - "3306" + environment: + - MYSQL_ROOT_PASSWORD=root + - MYSQL_DATABASE=test diff --git a/test/plugin/scenarios/hikaricp-scenario/pom.xml b/test/plugin/scenarios/hikaricp-scenario/pom.xml new file mode 100644 index 000000000..6c3700357 --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/pom.xml @@ -0,0 +1,126 @@ + + + + + org.apache.skywalking.apm.testcase + hikaricp-scenario + 1.0.0 + jar + + 4.0.0 + + + UTF-8 + 1.8 + + 3.2.0 + ${test.framework.version} + + 2.1.6.RELEASE + + + skywalking-hikaricp-scenario + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot-version} + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + + mysql + mysql-connector-java + 5.1.25 + + + + com.zaxxer + HikariCP + ${test.framework.version} + + + + + hikaricp-scenario + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + maven-compiler-plugin + + ${compiler.version} + ${compiler.version} + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-assembly-plugin + + + assemble + package + + single + + + + src/main/assembly/assembly.xml + + ./target/ + + + + + + + \ No newline at end of file diff --git a/test/plugin/scenarios/hikaricp-scenario/src/main/assembly/assembly.xml b/test/plugin/scenarios/hikaricp-scenario/src/main/assembly/assembly.xml new file mode 100644 index 000000000..76f043f2a --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/src/main/assembly/assembly.xml @@ -0,0 +1,41 @@ + + + + + zip + + + + + ./bin + 0775 + + + + + + ${project.build.directory}/hikaricp-scenario.jar + ./libs + 0775 + + + diff --git a/test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/Application.java b/test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/Application.java new file mode 100644 index 000000000..6062beb08 --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/Application.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.testcase.hikaricp; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + try { + SpringApplication.run(Application.class, args); + } catch (Exception e) { + // Never do this + } + } +} diff --git a/test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/MySQLConfig.java b/test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/MySQLConfig.java new file mode 100644 index 000000000..394776ae5 --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/MySQLConfig.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.testcase.hikaricp; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class MySQLConfig { + private static Logger LOGGER = LogManager.getLogger(MySQLConfig.class); + private static String URL; + private static String USER_NAME; + private static String PASSWORD; + + static { + InputStream inputStream = MySQLConfig.class.getClassLoader().getResourceAsStream("/jdbc.properties"); + Properties properties = new Properties(); + try { + properties.load(inputStream); + } catch (IOException e) { + LOGGER.error("Failed to load config", e); + } + URL = properties.getProperty("mysql.url"); + USER_NAME = properties.getProperty("mysql.username"); + PASSWORD = properties.getProperty("mysql.password"); + } + + public static String getUrl() { + return URL; + } + + public static String getUserName() { + return USER_NAME; + } + + public static String getPassword() { + return PASSWORD; + } +} diff --git a/test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/controller/CaseController.java b/test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/controller/CaseController.java new file mode 100644 index 000000000..3afb16cf0 --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/controller/CaseController.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.testcase.hikaricp.controller; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.skywalking.apm.testcase.hikaricp.service.CaseService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/case") +public class CaseController { + + @Autowired + CaseService caseService; + + private static final Logger LOGGER = LogManager.getLogger(CaseController.class); + + private static final String SUCCESS = "Success"; + + @RequestMapping("/hikaricp-scenario") + @ResponseBody + public String testcase() throws Exception { + try { + caseService.testCase(); + } catch (Exception e) { + LOGGER.error("Failed to execute sql.", e); + throw e; + } + return SUCCESS; + } + + @RequestMapping("/healthCheck") + @ResponseBody + public String healthCheck() throws Exception { + return SUCCESS; + } + +} diff --git a/test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/service/CaseService.java b/test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/service/CaseService.java new file mode 100644 index 000000000..05e801161 --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/src/main/java/org/apache/skywalking/apm/testcase/hikaricp/service/CaseService.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.skywalking.apm.testcase.hikaricp.service; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import org.apache.skywalking.apm.testcase.hikaricp.MySQLConfig; +import org.springframework.stereotype.Service; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; + +@Service +public class CaseService { + + public static HikariDataSource DS; + private static final String CREATE_TABLE_SQL = "CREATE TABLE test_Hikari(id VARCHAR(1) PRIMARY KEY, value VARCHAR(1) NOT NULL)"; + private static final String INSERT_DATA_SQL = "INSERT INTO test_Hikari(id, value) VALUES(1,1)"; + private static final String QUERY_DATA_SQL = "SELECT id, value FROM test_Hikari WHERE id=1"; + private static final String DELETE_DATA_SQL = "DELETE FROM test_Hikari WHERE id=1"; + private static final String DROP_TABLE_SQL = "DROP table test_Hikari"; + + static { + HikariConfig config = new HikariConfig(); + config.setJdbcUrl(MySQLConfig.getUrl()); + config.setUsername(MySQLConfig.getUserName()); + config.setPassword(MySQLConfig.getPassword()); + DS = new HikariDataSource(config); + } + + public void testCase() { + sqlExecutor(CREATE_TABLE_SQL); + sqlExecutor(INSERT_DATA_SQL); + sqlExecutor(QUERY_DATA_SQL); + sqlExecutor(DELETE_DATA_SQL); + sqlExecutor(DROP_TABLE_SQL); + } + + public void sqlExecutor(String sql) { + try (Connection conn = DS.getConnection()) { + Statement statement = conn.createStatement(); + statement.execute(sql); + } catch (SQLException e) { + e.printStackTrace(); + } + } + +} diff --git a/test/plugin/scenarios/hikaricp-scenario/src/main/resources/application.yaml b/test/plugin/scenarios/hikaricp-scenario/src/main/resources/application.yaml new file mode 100644 index 000000000..25503c3ea --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/src/main/resources/application.yaml @@ -0,0 +1,23 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +# +# +server: + port: 8080 + servlet: + context-path: /hikaricp-scenario +logging: + config: classpath:log4j2.xml \ No newline at end of file diff --git a/test/plugin/scenarios/hikaricp-scenario/src/main/resources/jdbc.properties b/test/plugin/scenarios/hikaricp-scenario/src/main/resources/jdbc.properties new file mode 100644 index 000000000..5c266c320 --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/src/main/resources/jdbc.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +mysql.url=jdbc:mysql://mysql-server:3306/test?serverTimezone=CST +mysql.username=root +mysql.password=root diff --git a/test/plugin/scenarios/hikaricp-scenario/src/main/resources/log4j2.xml b/test/plugin/scenarios/hikaricp-scenario/src/main/resources/log4j2.xml new file mode 100644 index 000000000..9849ed5a8 --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/src/main/resources/log4j2.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/plugin/scenarios/hikaricp-scenario/support-version.list b/test/plugin/scenarios/hikaricp-scenario/support-version.list new file mode 100644 index 000000000..68ae9ddc3 --- /dev/null +++ b/test/plugin/scenarios/hikaricp-scenario/support-version.list @@ -0,0 +1,22 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +4.0.0 +3.4.0 +3.3.0 +3.2.0 +3.1.0 +3.0.0