From bb0b3d369f9f965471557f815a1809cc5317a3ef Mon Sep 17 00:00:00 2001 From: "stone.wlg" Date: Fri, 13 Sep 2019 07:14:48 +0800 Subject: [PATCH] provide cassandra java driver 3.x plugin (#3410) * add cassandra java driver 3.x plugin --- .../trace/component/ComponentsDefine.java | 2 + .../cassandra-java-driver-3.x-plugin/pom.xml | 46 ++++++++++ .../driver/v3/ClusterConnectInterceptor.java | 58 ++++++++++++ ...ructorWithStateListenerArgInterceptor.java | 48 ++++++++++ .../java/driver/v3/ConnectionInfo.java | 44 ++++++++++ .../cassandra/java/driver/v3/Constants.java | 27 ++++++ ...etFutureGetUninterruptiblyInterceptor.java | 61 +++++++++++++ ...ecuteAsyncWithStatementArgInterceptor.java | 84 ++++++++++++++++++ .../v3/define/ClusterInstrumentation.java | 84 ++++++++++++++++++ ...DefaultResultSetFutureInstrumentation.java | 71 +++++++++++++++ .../define/SessionManagerInstrumentation.java | 72 +++++++++++++++ .../src/main/resources/skywalking-plugin.def | 19 ++++ .../v3/ClusterConnectInterceptorTest.java | 68 ++++++++++++++ ...orWithStateListenerArgInterceptorTest.java | 74 ++++++++++++++++ ...tureGetUninterruptiblyInterceptorTest.java | 79 +++++++++++++++++ ...eAsyncWithStatementArgInterceptorTest.java | 88 +++++++++++++++++++ apm-sniffer/apm-sdk-plugin/pom.xml | 1 + .../java-agent/Supported-list.md | 2 + .../test/resources/component-libraries.yml | 9 +- .../main/resources/component-libraries.yml | 9 +- 20 files changed, 944 insertions(+), 2 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/pom.xml create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConnectInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConstructorWithStateListenerArgInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ConnectionInfo.java create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/Constants.java create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/DefaultResultSetFutureGetUninterruptiblyInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/define/ClusterInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/define/DefaultResultSetFutureInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/define/SessionManagerInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/resources/skywalking-plugin.def create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConnectInterceptorTest.java create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConstructorWithStateListenerArgInterceptorTest.java create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/DefaultResultSetFutureGetUninterruptiblyInterceptorTest.java create mode 100644 apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptorTest.java 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 da9e764521..0105cea0ab 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 @@ -132,4 +132,6 @@ public class ComponentsDefine { public static final OfficialComponent PLAY = new OfficialComponent(68, "Play"); + public static final OfficialComponent CASSANDRA_JAVA_DRIVER = new OfficialComponent(69, "cassandra-java-driver"); + } diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/pom.xml new file mode 100644 index 0000000000..a804cba1a8 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/pom.xml @@ -0,0 +1,46 @@ + + + + + apm-sdk-plugin + org.apache.skywalking + 6.5.0-SNAPSHOT + + 4.0.0 + + apm-cassandra-java-driver-3.x-plugin + apm-cassandra-java-driver-3.x-plugin + http://maven.apache.org + + + UTF-8 + 3.7.2 + + + + + com.datastax.cassandra + cassandra-driver-core + ${cassandra.version} + provided + + + \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConnectInterceptor.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConnectInterceptor.java new file mode 100644 index 0000000000..552a087641 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConnectInterceptor.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.plugin.cassandra.java.driver.v3; + +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 java.lang.reflect.Method; + +/** + * @author stone.wlg + */ +public class ClusterConnectInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { + if (ret instanceof EnhancedInstance) { + String keyspace = allArguments.length > 0 ? (String) allArguments[0] : null; + ConnectionInfo connectionInfo = (ConnectionInfo) objInst.getSkyWalkingDynamicField(); + connectionInfo.setKeyspace(keyspace); + ((EnhancedInstance) ret).setSkyWalkingDynamicField(connectionInfo); + } + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Throwable t) { + if (ContextManager.isActive()) { + AbstractSpan span = ContextManager.activeSpan(); + span.errorOccurred(); + span.log(t); + } + } +} + diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConstructorWithStateListenerArgInterceptor.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConstructorWithStateListenerArgInterceptor.java new file mode 100644 index 0000000000..176f5b631d --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConstructorWithStateListenerArgInterceptor.java @@ -0,0 +1,48 @@ +/* + * 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.cassandra.java.driver.v3; + +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; + +import java.net.InetSocketAddress; +import java.util.List; + +/** + * @author stone.wlg + */ +public class ClusterConstructorWithStateListenerArgInterceptor implements InstanceConstructorInterceptor { + @Override + public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { + List inetSocketAddresses = (List) allArguments[1]; + StringBuilder hosts = new StringBuilder(); + for (InetSocketAddress inetSocketAddress : inetSocketAddresses) { + hosts.append(inetSocketAddress.getHostName() + ":" + inetSocketAddress.getPort() + ","); + } + + String contactPoints = hosts.toString(); + if (contactPoints.length() > 0) { + contactPoints = contactPoints.substring(0, contactPoints.length() - 1); + } + + objInst.setSkyWalkingDynamicField( + new ConnectionInfo(contactPoints) + ); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ConnectionInfo.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ConnectionInfo.java new file mode 100644 index 0000000000..1256d9ae19 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ConnectionInfo.java @@ -0,0 +1,44 @@ +/* + * 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.cassandra.java.driver.v3; + +/** + * @author stone.wlg + */ +public class ConnectionInfo { + + private final String contactPoints; + private String keyspace; + + public ConnectionInfo(final String contactPoints) { + this.contactPoints = contactPoints; + } + + public String getKeyspace() { + return this.keyspace; + } + + public void setKeyspace(String keyspace) { + this.keyspace = keyspace; + } + + public String getContactPoints() { + return this.contactPoints; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/Constants.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/Constants.java new file mode 100644 index 0000000000..f91562838c --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/Constants.java @@ -0,0 +1,27 @@ +/* + * 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.cassandra.java.driver.v3; + +/** + * @author stone.wlg + */ +public class Constants { + public static final String CASSANDRA_OP_PREFIX = "Cassandra/"; + public static final String CASSANDRA_DB_TYPE = "cassandra"; +} diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/DefaultResultSetFutureGetUninterruptiblyInterceptor.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/DefaultResultSetFutureGetUninterruptiblyInterceptor.java new file mode 100644 index 0000000000..16ccda5236 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/DefaultResultSetFutureGetUninterruptiblyInterceptor.java @@ -0,0 +1,61 @@ +/* + * 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.cassandra.java.driver.v3; + +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.tag.Tags; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; +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; + + +/** + * @author stone.wlg + */ +public class DefaultResultSetFutureGetUninterruptiblyInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + AbstractSpan span = ContextManager.createLocalSpan(Constants.CASSANDRA_OP_PREFIX + method.getName()); + span.setComponent(ComponentsDefine.CASSANDRA_JAVA_DRIVER); + Tags.DB_TYPE.set(span, Constants.CASSANDRA_DB_TYPE); + SpanLayer.asDB(span); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Object ret) throws Throwable { + if (ContextManager.isActive()) { + ContextManager.stopSpan(); + } + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, Throwable t) { + if (ContextManager.isActive()) { + AbstractSpan span = ContextManager.activeSpan(); + span.errorOccurred(); + span.log(t); + } + } +} diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptor.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptor.java new file mode 100644 index 0000000000..1224df3e98 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptor.java @@ -0,0 +1,84 @@ +/* + * 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.cassandra.java.driver.v3; + +import com.datastax.driver.core.BoundStatement; +import com.datastax.driver.core.Statement; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.tag.Tags; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; +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; + +/** + * @author stone.wlg + */ +public class SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + ConnectionInfo connectionInfo = (ConnectionInfo) objInst.getSkyWalkingDynamicField(); + if (connectionInfo == null) { + return; + } + + Statement statement = (Statement) allArguments[0]; + String remotePeer = statement.getHost() == null ? connectionInfo.getContactPoints() : (statement.getHost().getSocketAddress().getHostName() + ":" + statement.getHost().getSocketAddress().getPort()); + String keyspace = statement.getKeyspace() == null ? connectionInfo.getKeyspace() : statement.getKeyspace(); + String query = statement.toString(); + if (statement instanceof BoundStatement) { + query = ((BoundStatement) statement).preparedStatement().getQueryString(); + } + + AbstractSpan span = ContextManager.createExitSpan(Constants.CASSANDRA_OP_PREFIX + method.getName(), remotePeer); + span.setComponent(ComponentsDefine.CASSANDRA_JAVA_DRIVER); + Tags.DB_TYPE.set(span, Constants.CASSANDRA_DB_TYPE); + Tags.DB_INSTANCE.set(span, keyspace); + Tags.DB_STATEMENT.set(span, query); + SpanLayer.asDB(span); + } + + @Override + public final Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, + Object ret) throws Throwable { + ConnectionInfo connectionInfo = (ConnectionInfo) objInst.getSkyWalkingDynamicField(); + if (connectionInfo != null && ContextManager.isActive()) { + ContextManager.stopSpan(); + } + return ret; + } + + @Override + public final void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + if (ContextManager.isActive()) { + AbstractSpan span = ContextManager.activeSpan(); + span.errorOccurred(); + span.log(t); + } + } +} diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/define/ClusterInstrumentation.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/define/ClusterInstrumentation.java new file mode 100644 index 0000000000..888f4c1e59 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/define/ClusterInstrumentation.java @@ -0,0 +1,84 @@ +/* + * 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.cassandra.java.driver.v3.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.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; + +/** + * @author stone.wlg + */ +public class ClusterInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.datastax.driver.core.Cluster"; + private static final String CONSTRUCTOR_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.cassandra.java.driver.v3.ClusterConstructorWithStateListenerArgInterceptor"; + private static final String METHODS_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.cassandra.java.driver.v3.ClusterConnectInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[]{ + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return takesArguments(4); + } + + @Override + public String getConstructorInterceptor() { + return CONSTRUCTOR_INTERCEPT_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("connect"); + } + + @Override + public String getMethodsInterceptor() { + return METHODS_INTERCEPT_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/define/DefaultResultSetFutureInstrumentation.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/define/DefaultResultSetFutureInstrumentation.java new file mode 100644 index 0000000000..5f41e14bf0 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/define/DefaultResultSetFutureInstrumentation.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.plugin.cassandra.java.driver.v3.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.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; + + +/** + * @author stone.wlg + */ +public class DefaultResultSetFutureInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.datastax.driver.core.DefaultResultSetFuture"; + private static final String METHODS_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.cassandra.java.driver.v3.DefaultResultSetFutureGetUninterruptiblyInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return null; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("getUninterruptibly"); + } + + @Override + public String getMethodsInterceptor() { + return METHODS_INTERCEPT_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/define/SessionManagerInstrumentation.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/define/SessionManagerInstrumentation.java new file mode 100644 index 0000000000..67dea73f9d --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/define/SessionManagerInstrumentation.java @@ -0,0 +1,72 @@ +/* + * 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.cassandra.java.driver.v3.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.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.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; +import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +/** + * @author stone.wlg + */ +public class SessionManagerInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + private static final String ENHANCE_CLASS = "com.datastax.driver.core.SessionManager"; + private static final String METHODS_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.cassandra.java.driver.v3.SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return null; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("execute").and(takesArgumentWithType(0, "com.datastax.driver.core.Statement")) + .or(named("executeAsync").and(takesArgumentWithType(0, "com.datastax.driver.core.Statement"))); + } + + @Override + public String getMethodsInterceptor() { + return METHODS_INTERCEPT_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/resources/skywalking-plugin.def new file mode 100644 index 0000000000..f6551a0a64 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/main/resources/skywalking-plugin.def @@ -0,0 +1,19 @@ +# 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. + +cassandra-java-driver-3.x=org.apache.skywalking.apm.plugin.cassandra.java.driver.v3.define.ClusterInstrumentation +cassandra-java-driver-3.x=org.apache.skywalking.apm.plugin.cassandra.java.driver.v3.define.SessionManagerInstrumentation +cassandra-java-driver-3.x=org.apache.skywalking.apm.plugin.cassandra.java.driver.v3.define.DefaultResultSetFutureInstrumentation diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConnectInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConnectInterceptorTest.java new file mode 100644 index 0000000000..6a8704d039 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConnectInterceptorTest.java @@ -0,0 +1,68 @@ +/* + * 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.cassandra.java.driver.v3; + +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.hamcrest.core.Is; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.lang.reflect.Method; + +/** + * @author stone.wlg + */ +@RunWith(PowerMockRunner.class) +public class ClusterConnectInterceptorTest { + + private ClusterConnectInterceptor interceptor; + + private EnhancedInstance enhancedInstance = new EnhancedInstance() { + private ConnectionInfo connectionInfo = new ConnectionInfo("localhost:9042"); + + @Override + public Object getSkyWalkingDynamicField() { + return connectionInfo; + } + + @Override + public void setSkyWalkingDynamicField(Object value) { + this.connectionInfo = (ConnectionInfo) value; + } + }; + + @Mock + private Method method; + + @Before + public void setUp() throws Exception { + interceptor = new ClusterConnectInterceptor(); + } + + @Test + public void afterMethod() throws Throwable { + EnhancedInstance ret = (EnhancedInstance) interceptor.afterMethod(enhancedInstance, method, new Object[]{"test"}, null, enhancedInstance); + ConnectionInfo connectionInfo = (ConnectionInfo) ret.getSkyWalkingDynamicField(); + Assert.assertThat(connectionInfo.getKeyspace(), Is.is("test")); + } +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConstructorWithStateListenerArgInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConstructorWithStateListenerArgInterceptorTest.java new file mode 100644 index 0000000000..6f292cc175 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/ClusterConstructorWithStateListenerArgInterceptorTest.java @@ -0,0 +1,74 @@ +/* + * 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.cassandra.java.driver.v3; + +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.hamcrest.core.Is; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; + +/** + * @author stone.wlg + */ +@RunWith(PowerMockRunner.class) +public class ClusterConstructorWithStateListenerArgInterceptorTest { + + @Mock + private ClusterConstructorWithStateListenerArgInterceptor interceptor; + + @Mock + private EnhancedInstance enhancedInstance = new EnhancedInstance() { + private ConnectionInfo connectionInfo; + + @Override + public Object getSkyWalkingDynamicField() { + return connectionInfo; + } + + @Override + public void setSkyWalkingDynamicField(Object value) { + this.connectionInfo = (ConnectionInfo) value; + } + }; + + private List inetSocketAddresses; + + @Before + public void setUp() throws Exception { + interceptor = new ClusterConstructorWithStateListenerArgInterceptor(); + + inetSocketAddresses = new ArrayList(); + inetSocketAddresses.add(new InetSocketAddress("172.20.0.2", 9042)); + } + + @Test + public void onConstruct() { + interceptor.onConstruct(enhancedInstance, new Object[]{"cluster-name", inetSocketAddresses}); + ConnectionInfo connectionInfo = (ConnectionInfo) enhancedInstance.getSkyWalkingDynamicField(); + Assert.assertThat(connectionInfo.getContactPoints(), Is.is("172.20.0.2:9042")); + } +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/DefaultResultSetFutureGetUninterruptiblyInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/DefaultResultSetFutureGetUninterruptiblyInterceptorTest.java new file mode 100644 index 0000000000..204537e31f --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/DefaultResultSetFutureGetUninterruptiblyInterceptorTest.java @@ -0,0 +1,79 @@ +/* + * 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.cassandra.java.driver.v3; + +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.test.helper.SegmentHelper; +import org.apache.skywalking.apm.agent.test.tools.*; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; + +import java.lang.reflect.Method; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.powermock.api.mockito.PowerMockito.when; + +/** + * @author stone.wlg + */ +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class DefaultResultSetFutureGetUninterruptiblyInterceptorTest { + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + private DefaultResultSetFutureGetUninterruptiblyInterceptor interceptor; + + @Mock + private EnhancedInstance objectInstance; + @Mock + private Method method; + + @Before + public void setUp() throws Exception { + interceptor = new DefaultResultSetFutureGetUninterruptiblyInterceptor(); + when(method.getName()).thenReturn("executeAsync"); + } + + @Test + public void testCreateLocalSpan() throws Throwable { + interceptor.beforeMethod(objectInstance, method, null, null, null); + interceptor.afterMethod(objectInstance, method, null, null, null); + + assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment segment = segmentStorage.getTraceSegments().get(0); + assertThat(SegmentHelper.getSpans(segment).size(), is(1)); + AbstractTracingSpan span = SegmentHelper.getSpans(segment).get(0); + SpanAssert.assertLayer(span, SpanLayer.DB); + assertThat(span.getOperationName(), is(Constants.CASSANDRA_OP_PREFIX)); + SpanAssert.assertTag(span, 0, Constants.CASSANDRA_DB_TYPE); + } +} \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptorTest.java new file mode 100644 index 0000000000..928c02af75 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/cassandra-java-driver-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/cassandra/java/driver/v3/SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptorTest.java @@ -0,0 +1,88 @@ +/* + * 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.cassandra.java.driver.v3; + +import com.datastax.driver.core.SimpleStatement; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.apache.skywalking.apm.agent.core.context.trace.SpanLayer; +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.test.helper.SegmentHelper; +import org.apache.skywalking.apm.agent.test.tools.*; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; + +import java.lang.reflect.Method; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.powermock.api.mockito.PowerMockito.when; + +/** + * @author stone.wlg + */ +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptorTest { + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + private SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptor interceptor; + + @Mock + private ConnectionInfo connectionInfo; + @Mock + private EnhancedInstance objectInstance; + @Mock + private Method method; + + @Before + public void setUp() throws Exception { + interceptor = new SessionManagerExecuteAndExecuteAsyncWithStatementArgInterceptor(); + + when(objectInstance.getSkyWalkingDynamicField()).thenReturn(connectionInfo); + when(method.getName()).thenReturn("executeAsync"); + when(connectionInfo.getContactPoints()).thenReturn("localhost:9042"); + when(connectionInfo.getKeyspace()).thenReturn("test"); + } + + @Test + public void testCreateExitSpan() throws Throwable { + interceptor.beforeMethod(objectInstance, method, new Object[]{new SimpleStatement("SELECT * FROM test")}, null, null); + interceptor.afterMethod(objectInstance, method, new Object[]{new SimpleStatement("SELECT * FROM test")}, null, null); + + assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment segment = segmentStorage.getTraceSegments().get(0); + assertThat(SegmentHelper.getSpans(segment).size(), is(1)); + AbstractTracingSpan span = SegmentHelper.getSpans(segment).get(0); + SpanAssert.assertLayer(span, SpanLayer.DB); + assertThat(span.getOperationName(), is(Constants.CASSANDRA_OP_PREFIX)); + SpanAssert.assertTag(span, 0, Constants.CASSANDRA_DB_TYPE); + SpanAssert.assertTag(span, 1, "test"); + SpanAssert.assertTag(span, 2, "SELECT * FROM test"); + } +} \ 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 107bd18507..f3be935411 100644 --- a/apm-sniffer/apm-sdk-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/pom.xml @@ -74,6 +74,7 @@ vertx-plugins resteasy-plugin solrj-7.x-plugin + cassandra-java-driver-3.x-plugin pom 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 fc333b9aeb..b04758bf10 100644 --- a/docs/en/setup/service-agent/java-agent/Supported-list.md +++ b/docs/en/setup/service-agent/java-agent/Supported-list.md @@ -56,6 +56,8 @@ * [transport-client](https://github.com/elastic/elasticsearch/tree/master/client/transport) 5.2.x-5.6.x * [Solr](https://github.com/apache/lucene-solr/) * [SolrJ](https://github.com/apache/lucene-solr/tree/master/solr/solrj) 7.x + * [Cassandra](https://github.com/apache/cassandra) 3.x + * [cassandra-java-driver](https://github.com/datastax/java-driver) 3.6.0-3.7.2 * Service Discovery * [Netflix Eureka](https://github.com/Netflix/eureka) diff --git a/oap-server/server-core/src/test/resources/component-libraries.yml b/oap-server/server-core/src/test/resources/component-libraries.yml index e539cd6bd2..a2b6568fce 100755 --- a/oap-server/server-core/src/test/resources/component-libraries.yml +++ b/oap-server/server-core/src/test/resources/component-libraries.yml @@ -215,6 +215,12 @@ spring-webflux: Play: id: 68 languages: Java,Scala +cassandra-java-driver: + id: 69 + languages: Java +Cassandra: + id: 70 + languages: Java # .NET/.NET Core components # [3000, 4000) for C#/.NET only @@ -312,4 +318,5 @@ Component-Server-Mappings: Pomelo.EntityFrameworkCore.MySql: Mysql Npgsql.EntityFrameworkCore.PostgreSQL: PostgreSQL transport-client: Elasticsearch - SolrJ: Solr \ No newline at end of file + SolrJ: Solr + cassandra-java-driver: Cassandra \ No newline at end of file diff --git a/oap-server/server-starter/src/main/resources/component-libraries.yml b/oap-server/server-starter/src/main/resources/component-libraries.yml index ae33ede9f4..711a3ba2c9 100755 --- a/oap-server/server-starter/src/main/resources/component-libraries.yml +++ b/oap-server/server-starter/src/main/resources/component-libraries.yml @@ -233,6 +233,12 @@ spring-webflux: Play: id: 68 languages: Java,Scala +cassandra-java-driver: + id: 69 + languages: Java +Cassandra: + id: 70 + languages: Java # .NET/.NET Core components @@ -333,4 +339,5 @@ Component-Server-Mappings: Pomelo.EntityFrameworkCore.MySql: Mysql Npgsql.EntityFrameworkCore.PostgreSQL: PostgreSQL transport-client: Elasticsearch - SolrJ: Solr \ No newline at end of file + SolrJ: Solr + cassandra-java-driver: Cassandra \ No newline at end of file -- GitLab