From bc7afa63476b61b44ae6464c09ca381e133d17c8 Mon Sep 17 00:00:00 2001 From: King <40067375+Jargon96@users.noreply.github.com> Date: Sun, 8 Nov 2020 09:41:56 +0800 Subject: [PATCH] add support for dbcp 2.x plugin (#5550) --- .github/workflows/plugins-test.3.yaml | 1 + CHANGES.md | 1 + .../trace/component/ComponentsDefine.java | 4 +- .../apm-sdk-plugin/dbcp-2.x-plugin/pom.xml | 46 ++++ .../v2/PoolingCloseConnectInterceptor.java | 50 ++++ .../dbcp/v2/PoolingGetConnectInterceptor.java | 50 ++++ .../BasicDataSourceInstrumentation.java | 75 ++++++ .../DelegatingConnectionInstrumentation.java | 76 ++++++ .../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 | 2 + .../main/resources/component-libraries.yml | 3 + .../dbcp-2.x-scenario/bin/startup.sh | 21 ++ .../config/expectedData.yaml | 240 ++++++++++++++++++ .../dbcp-2.x-scenario/configuration.yml | 32 +++ .../scenarios/dbcp-2.x-scenario/pom.xml | 126 +++++++++ .../src/main/assembly/assembly.xml | 41 +++ .../apm/testcase/dbcp/Application.java | 34 +++ .../apm/testcase/dbcp/MysqlConfig.java | 57 +++++ .../dbcp/controller/CaseController.java | 58 +++++ .../testcase/dbcp/service/CaseService.java | 71 ++++++ .../src/main/resources/application.yaml | 23 ++ .../src/main/resources/jdbc.properties | 18 ++ .../src/main/resources/log4j2.xml | 30 +++ .../dbcp-2.x-scenario/support-version.list | 25 ++ 26 files changed, 1102 insertions(+), 2 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/pom.xml create mode 100755 apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/PoolingCloseConnectInterceptor.java create mode 100755 apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/PoolingGetConnectInterceptor.java create mode 100755 apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/define/BasicDataSourceInstrumentation.java create mode 100755 apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/define/DelegatingConnectionInstrumentation.java create mode 100755 apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/resources/skywalking-plugin.def create mode 100755 test/plugin/scenarios/dbcp-2.x-scenario/bin/startup.sh create mode 100755 test/plugin/scenarios/dbcp-2.x-scenario/config/expectedData.yaml create mode 100755 test/plugin/scenarios/dbcp-2.x-scenario/configuration.yml create mode 100755 test/plugin/scenarios/dbcp-2.x-scenario/pom.xml create mode 100755 test/plugin/scenarios/dbcp-2.x-scenario/src/main/assembly/assembly.xml create mode 100755 test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/Application.java create mode 100755 test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/MysqlConfig.java create mode 100755 test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/controller/CaseController.java create mode 100644 test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/service/CaseService.java create mode 100755 test/plugin/scenarios/dbcp-2.x-scenario/src/main/resources/application.yaml create mode 100755 test/plugin/scenarios/dbcp-2.x-scenario/src/main/resources/jdbc.properties create mode 100755 test/plugin/scenarios/dbcp-2.x-scenario/src/main/resources/log4j2.xml create mode 100755 test/plugin/scenarios/dbcp-2.x-scenario/support-version.list diff --git a/.github/workflows/plugins-test.3.yaml b/.github/workflows/plugins-test.3.yaml index 7a568576e..136f90897 100644 --- a/.github/workflows/plugins-test.3.yaml +++ b/.github/workflows/plugins-test.3.yaml @@ -70,6 +70,7 @@ jobs: - quartz-scheduler-2.x-scenario - xxl-job-2.x-scenario - thrift-scenario + - dbcp-2.x-scenario steps: - uses: actions/checkout@v2 with: diff --git a/CHANGES.md b/CHANGES.md index e7f0502d1..42eb2f8c9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,7 @@ Release Notes. * Polish tracing context related codes. * Add the plugin for async-http-client 2.x * Fix NPE in the nutz plugin. +* Provide Apache Commons DBCP 2.x plugin. #### OAP-Backend * Add the `@SuperDataset` annotation for BrowserErrorLog. 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 9c08058d0..0146301ed 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 @@ -187,5 +187,5 @@ public class ComponentsDefine { public static final OfficialComponent ASYNC_HTTP_CLIENT = new OfficialComponent(102, "AsyncHttpClient"); - -} \ No newline at end of file + public static final OfficialComponent DBCP = new OfficialComponent(103, "dbcp"); +} diff --git a/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/pom.xml new file mode 100644 index 000000000..d26e72121 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/pom.xml @@ -0,0 +1,46 @@ + + + + + + apm-sdk-plugin + org.apache.skywalking + 8.3.0-SNAPSHOT + + 4.0.0 + + dbcp-2.x-plugin + + + UTF-8 + 2.7.0 + + + + + org.apache.commons + commons-dbcp2 + ${dbcp.version} + provided + + + + + \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/PoolingCloseConnectInterceptor.java b/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/PoolingCloseConnectInterceptor.java new file mode 100755 index 000000000..e72e7a0a4 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/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.dbcp.v2; + +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 DBCP 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("DBCP/Connection/" + method.getName()); + span.setComponent(ComponentsDefine.DBCP); + } + + @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/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/PoolingGetConnectInterceptor.java b/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/PoolingGetConnectInterceptor.java new file mode 100755 index 000000000..ccaaeb4d5 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/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.dbcp.v2; + +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 DBCP 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("DBCP/Connection/" + method.getName()); + span.setComponent(ComponentsDefine.DBCP); + } + + @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/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/define/BasicDataSourceInstrumentation.java b/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/define/BasicDataSourceInstrumentation.java new file mode 100755 index 000000000..288590575 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/define/BasicDataSourceInstrumentation.java @@ -0,0 +1,75 @@ +/* + * 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.dbcp.v2.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; + +/** + * BasicDataSource provides a "one stop shopping" solution for database connection pool solution + * basic requirements. BasicDataSource#getConnection() creates (if necessary) and return a connection. + */ +public class BasicDataSourceInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + private static final String ENHANCE_CLASS = "org.apache.commons.dbcp2.BasicDataSource"; + private static final String CONNECT_GET_INTERCEPTOR = "org.apache.skywalking.apm.plugin.dbcp.v2.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("getConnection"); + } + + @Override + public String getMethodsInterceptor() { + return CONNECT_GET_INTERCEPTOR; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + public StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() { + return new StaticMethodsInterceptPoint[0]; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/define/DelegatingConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/define/DelegatingConnectionInstrumentation.java new file mode 100755 index 000000000..1fce69015 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/dbcp/v2/define/DelegatingConnectionInstrumentation.java @@ -0,0 +1,76 @@ +/* + * 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.dbcp.v2.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; + +/** + * DBCP use DelegatingConnection which is the delegating implementation of Connection.All of + * the methods from the Connection interface simply check to see that the Connection is active. + * DelegatingConnection#close() close/return connection. + */ +public class DelegatingConnectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + private static final String ENHANCE_CLASS = "org.apache.commons.dbcp2.DelegatingConnection"; + private static final String CONNECT_CLOSE_INTERCEPTOR = "org.apache.skywalking.apm.plugin.dbcp.v2.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("close"); + } + + @Override + public String getMethodsInterceptor() { + return CONNECT_CLOSE_INTERCEPTOR; + } + + @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/dbcp-2.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/dbcp-2.x-plugin/src/main/resources/skywalking-plugin.def new file mode 100755 index 000000000..c1797be2f --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/dbcp-2.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. + +dbcp-2.x=org.apache.skywalking.apm.plugin.dbcp.v2.define.DelegatingConnectionInstrumentation +dbcp-2.x=org.apache.skywalking.apm.plugin.dbcp.v2.define.BasicDataSourceInstrumentation \ 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 757b17571..6aade3e86 100644 --- a/apm-sniffer/apm-sdk-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/pom.xml @@ -103,6 +103,7 @@ thrift-plugin httpclient-commons asynchttpclient-2.x-plugin + dbcp-2.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 d36329357..322e83992 100644 --- a/docs/en/setup/service-agent/java-agent/Plugin-list.md +++ b/docs/en/setup/service-agent/java-agent/Plugin-list.md @@ -9,6 +9,7 @@ - brpc-java - canal-1.x - cassandra-java-driver-3.x +- dbcp-2.x - dubbo - ehcache-2.x - elastic-job-2.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 771a6ac34..b94517e03 100644 --- a/docs/en/setup/service-agent/java-agent/Supported-list.md +++ b/docs/en/setup/service-agent/java-agent/Supported-list.md @@ -106,6 +106,8 @@ * [Coroutine](https://kotlinlang.org/docs/reference/coroutines-overview.html) 1.0.1 -> 1.3.x (Optional²) * GraphQL * [Graphql](https://github.com/graphql-java) 8.0 -> 15.x +* Pool + * [Apache Commons DBCP](https://github.com/apache/commons-dbcp) 2.x ¹Due to license incompatibilities/restrictions these plugins are hosted and released in 3rd part repository, go to [SkyAPM java plugin extension repository](https://github.com/SkyAPM/java-plugin-extensions) to get these. diff --git a/oap-server/server-bootstrap/src/main/resources/component-libraries.yml b/oap-server/server-bootstrap/src/main/resources/component-libraries.yml index 193f39a2a..aba07b1f0 100755 --- a/oap-server/server-bootstrap/src/main/resources/component-libraries.yml +++ b/oap-server/server-bootstrap/src/main/resources/component-libraries.yml @@ -338,6 +338,9 @@ thrift-client: AsyncHttpClient: id: 102 languages: Java +dbcp: + id: 103 + languages: Java # .NET/.NET Core components # [3000, 4000) for C#/.NET only diff --git a/test/plugin/scenarios/dbcp-2.x-scenario/bin/startup.sh b/test/plugin/scenarios/dbcp-2.x-scenario/bin/startup.sh new file mode 100755 index 000000000..d6e54b06f --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-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/dbcp-2.x-scenario.jar & \ No newline at end of file diff --git a/test/plugin/scenarios/dbcp-2.x-scenario/config/expectedData.yaml b/test/plugin/scenarios/dbcp-2.x-scenario/config/expectedData.yaml new file mode 100755 index 000000000..36ccf7013 --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-scenario/config/expectedData.yaml @@ -0,0 +1,240 @@ +# 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: dbcp-2.x-scenario + segmentSize: nq 0 + segments: + - segmentId: not null + spans: + - operationName: Mysql/JDBI/Connection/close + operationId: eq 0 + parentSpanId: 1 + spanId: 2 + componentId: 33 + isError: false + spanType: Exit + peer: mysql-server:3306 + skipAnalysis: false + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: test} + - {key: db.statement, value: ''} + - operationName: DBCP/Connection/getConnection + operationId: 0 + parentSpanId: 0 + spanId: 1 + startTime: gt 0 + endTime: gt 0 + componentId: 103 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: Mysql/JDBI/Statement/execute + operationId: 0 + parentSpanId: 0 + spanId: 3 + startTime: gt 0 + endTime: gt 0 + componentId: 33 + isError: false + spanType: Exit + peer: mysql-server:3306 + skipAnalysis: false + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: test} + - key: db.statement + value: "CREATE TABLE test_DBCP(\nid VARCHAR(1) PRIMARY KEY, \nvalue VARCHAR(1)\ + \ NOT NULL)" + - operationName: DBCP/Connection/close + operationId: 0 + parentSpanId: 0 + spanId: 4 + startTime: gt 0 + endTime: gt 0 + componentId: 103 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: DBCP/Connection/getConnection + operationId: 0 + parentSpanId: 0 + spanId: 5 + startTime: gt 0 + endTime: gt 0 + componentId: 103 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: Mysql/JDBI/Statement/execute + operationId: 0 + parentSpanId: 0 + spanId: 6 + startTime: gt 0 + endTime: gt 0 + componentId: 33 + isError: false + spanType: Exit + peer: mysql-server:3306 + skipAnalysis: false + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: test} + - key: db.statement + value: 'INSERT INTO test_DBCP(id, value) VALUES(1,1)' + - operationName: DBCP/Connection/close + operationId: 0 + parentSpanId: 0 + spanId: 7 + startTime: gt 0 + endTime: gt 0 + componentId: 103 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: DBCP/Connection/getConnection + operationId: 0 + parentSpanId: 0 + spanId: 8 + startTime: gt 0 + endTime: gt 0 + componentId: 103 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: Mysql/JDBI/Statement/execute + operationId: 0 + parentSpanId: 0 + spanId: 9 + startTime: gt 0 + endTime: gt 0 + componentId: 33 + isError: false + spanType: Exit + peer: mysql-server:3306 + skipAnalysis: false + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: test} + - key: db.statement + value: 'SELECT id, value FROM test_DBCP WHERE id=1' + - operationName: DBCP/Connection/close + operationId: 0 + parentSpanId: 0 + spanId: 10 + startTime: gt 0 + endTime: gt 0 + componentId: 103 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: DBCP/Connection/getConnection + operationId: 0 + parentSpanId: 0 + spanId: 11 + startTime: gt 0 + endTime: gt 0 + componentId: 103 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: Mysql/JDBI/Statement/execute + operationId: 0 + parentSpanId: 0 + spanId: 12 + startTime: gt 0 + endTime: gt 0 + componentId: 33 + isError: false + spanType: Exit + peer: mysql-server:3306 + skipAnalysis: false + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: test} + - key: db.statement + value: "DELETE FROM test_DBCP WHERE id=1" + - operationName: DBCP/Connection/close + operationId: 0 + parentSpanId: 0 + spanId: 13 + startTime: gt 0 + endTime: gt 0 + componentId: 103 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: DBCP/Connection/getConnection + operationId: 0 + parentSpanId: 0 + spanId: 14 + startTime: gt 0 + endTime: gt 0 + componentId: 103 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: Mysql/JDBI/Statement/execute + operationId: 0 + parentSpanId: 0 + spanId: 15 + startTime: gt 0 + endTime: gt 0 + componentId: 33 + isError: false + spanType: Exit + peer: mysql-server:3306 + skipAnalysis: false + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: test} + - key: db.statement + value: "DROP table test_DBCP" + - operationName: DBCP/Connection/close + operationId: 0 + parentSpanId: 0 + spanId: 16 + startTime: gt 0 + endTime: gt 0 + componentId: 103 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: /dbcp-2.x-scenario/case/dbcp-2.x-scenario + operationId: 0 + parentSpanId: -1 + spanId: 0 + spanLayer: Http + startTime: gt 0 + endTime: gt 0 + componentId: 1 + isError: false + spanType: Entry + peer: '' + skipAnalysis: false + tags: + - {key: url, value: 'http://localhost:8080/dbcp-2.x-scenario/case/dbcp-2.x-scenario'} + - {key: http.method, value: GET} diff --git a/test/plugin/scenarios/dbcp-2.x-scenario/configuration.yml b/test/plugin/scenarios/dbcp-2.x-scenario/configuration.yml new file mode 100755 index 000000000..1b26b688d --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-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/dbcp-2.x-scenario/case/dbcp-2.x-scenario +healthCheck: http://localhost:8080/dbcp-2.x-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/dbcp-2.x-scenario/pom.xml b/test/plugin/scenarios/dbcp-2.x-scenario/pom.xml new file mode 100755 index 000000000..c58590f76 --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-scenario/pom.xml @@ -0,0 +1,126 @@ + + + + + org.apache.skywalking.apm.testcase + dbcp-2.x-scenario + 1.0.0 + jar + + 4.0.0 + + + UTF-8 + 1.8 + + 2.7.0 + ${test.framework.version} + + 2.1.6.RELEASE + + + skywalking-dbcp-2.x-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 + + + + org.apache.commons + commons-dbcp2 + ${test.framework.version} + + + + + dbcp-2.x-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/dbcp-2.x-scenario/src/main/assembly/assembly.xml b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/assembly/assembly.xml new file mode 100755 index 000000000..44876dc18 --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/assembly/assembly.xml @@ -0,0 +1,41 @@ + + + + + zip + + + + + ./bin + 0775 + + + + + + ${project.build.directory}/dbcp-2.x-scenario.jar + ./libs + 0775 + + + diff --git a/test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/Application.java b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/Application.java new file mode 100755 index 000000000..35f05a689 --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/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.dbcp; + +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/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/MysqlConfig.java b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/MysqlConfig.java new file mode 100755 index 000000000..af324d988 --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/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.dbcp; + +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 userName; + 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"); + userName = properties.getProperty("mysql.username"); + password = properties.getProperty("mysql.password"); + } + + public static String getUrl() { + return url; + } + + public static String getUserName() { + return userName; + } + + public static String getPassword() { + return password; + } +} diff --git a/test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/controller/CaseController.java b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/controller/CaseController.java new file mode 100755 index 000000000..48d4c9b80 --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/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.dbcp.controller; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.skywalking.apm.testcase.dbcp.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("/dbcp-2.x-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/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/service/CaseService.java b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/service/CaseService.java new file mode 100644 index 000000000..df8af2954 --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/java/org/apache/skywalking/apm/testcase/dbcp/service/CaseService.java @@ -0,0 +1,71 @@ +/* + * 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.dbcp.service; + +import org.apache.commons.dbcp2.BasicDataSourceFactory; +import org.apache.skywalking.apm.testcase.dbcp.MysqlConfig; +import org.springframework.stereotype.Service; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; + +@Service +public class CaseService { + + public static DataSource ds; + private static final String CREATE_TABLE_SQL = "CREATE TABLE test_DBCP(\n" + "id VARCHAR(1) PRIMARY KEY, \n" + "value VARCHAR(1) NOT NULL)"; + private static final String INSERT_DATA_SQL = "INSERT INTO test_DBCP(id, value) VALUES(1,1)"; + private static final String QUERY_DATA_SQL = "SELECT id, value FROM test_DBCP WHERE id=1"; + private static final String DELETE_DATA_SQL = "DELETE FROM test_DBCP WHERE id=1"; + private static final String DROP_TABLE_SQL = "DROP table test_DBCP"; + + static { + Properties properties = new Properties(); + properties.setProperty("driverClassName", "com.mysql.jdbc.Driver"); + properties.setProperty("url", MysqlConfig.getUrl()); + properties.setProperty("username", MysqlConfig.getUserName()); + properties.setProperty("password", MysqlConfig.getPassword()); + try { + ds = BasicDataSourceFactory.createDataSource(properties); + } catch (Exception e) { + e.printStackTrace(); + } + } + + 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/dbcp-2.x-scenario/src/main/resources/application.yaml b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/resources/application.yaml new file mode 100755 index 000000000..3ac374b50 --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-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: /dbcp-2.x-scenario +logging: + config: classpath:log4j2.xml \ No newline at end of file diff --git a/test/plugin/scenarios/dbcp-2.x-scenario/src/main/resources/jdbc.properties b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/resources/jdbc.properties new file mode 100755 index 000000000..aad3ac13a --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-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 +# "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/dbcp-2.x-scenario/src/main/resources/log4j2.xml b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/resources/log4j2.xml new file mode 100755 index 000000000..9849ed5a8 --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-scenario/src/main/resources/log4j2.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/plugin/scenarios/dbcp-2.x-scenario/support-version.list b/test/plugin/scenarios/dbcp-2.x-scenario/support-version.list new file mode 100755 index 000000000..d70f15203 --- /dev/null +++ b/test/plugin/scenarios/dbcp-2.x-scenario/support-version.list @@ -0,0 +1,25 @@ +# 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 +# "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. + +2.8.0 +2.7.0 +2.6.0 +2.5.0 +2.4.0 +2.3.0 +2.2.0 +2.1.1 +2.0.1 \ No newline at end of file