diff --git a/.github/workflows/plugins-test.1.yaml b/.github/workflows/plugins-test.1.yaml
index a05aa43a67cc..f9fdd74b94f6 100644
--- a/.github/workflows/plugins-test.1.yaml
+++ b/.github/workflows/plugins-test.1.yaml
@@ -38,6 +38,7 @@ jobs:
- { name: 'httpclient-3.x-scenario', title: 'HttpClient 2.0-3.1 (5)' }
- { name: 'httpclient-4.3.x-scenario', title: 'HttpClient 4.3.x-4.5.x (14)' }
- { name: 'hystrix-scenario', title: 'Hystrix 1.4.20-1.5.12 (20)' }
+ - { name: 'influxdb-scenario', title: 'InfluxDB Java 2.5-2.17 (12)' }
- { name: 'jdk-http-scenario', title: 'JDK http (1)' }
- { name: 'jdk-threading-scenario', title: 'JDK Threading (1)' }
- { name: 'jedis-scenario', title: 'Jedis 2.4.0-2.9.0 (18)' }
diff --git a/.gitignore b/.gitignore
index cc89988ebab6..cd2d1bd2c270 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,3 +20,5 @@ OALLexer.tokens
.externalToolBuilders
/test/plugin/dist
/test/plugin/workspace
+/test/jacoco/classes
+/test/jacoco/*.exec
\ No newline at end of file
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 963baa04d0ba..70bd828de660 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
@@ -160,4 +160,6 @@ public class ComponentsDefine {
public static final OfficialComponent MARIADB_JDBC = new OfficialComponent(87, "mariadb-jdbc");
public static final OfficialComponent QUASAR = new OfficialComponent(88, "quasar");
+
+ public static final OfficialComponent INFLUXDB_JAVA = new OfficialComponent(90, "influxdb-java");
}
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java
index 172015f3f243..e6e4ffb91f46 100755
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java
@@ -399,6 +399,13 @@ public static class Http {
*/
public static int HTTP_PARAMS_LENGTH_THRESHOLD = 1024;
}
+
+ public static class InfluxDB {
+ /**
+ * If set to true, the parameters of the InfluxQL would be collected.
+ */
+ public static boolean TRACE_INFLUXQL = true;
+ }
}
public static class Correlation {
diff --git a/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/pom.xml
new file mode 100644
index 000000000000..67c7d9269209
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/pom.xml
@@ -0,0 +1,47 @@
+
+
+
+
+
+ apm-sdk-plugin
+ org.apache.skywalking
+ 8.1.0-SNAPSHOT
+
+ 4.0.0
+
+ apm-influxdb-2.x-plugin
+ This plugin is for use with influxdb-java client
+
+
+ UTF-8
+ 2.15
+
+
+
+
+ org.influxdb
+ influxdb-java
+ ${influxdb-jave.version}
+ provided
+
+
+
+
\ No newline at end of file
diff --git a/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/define/Constants.java b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/define/Constants.java
new file mode 100644
index 000000000000..a83efd2423eb
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/define/Constants.java
@@ -0,0 +1,45 @@
+/*
+ * 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.influxdb.define;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * InfluxDB plugin Constants
+ *
+ * @since 2020/6/6
+ */
+public class Constants {
+
+ public static final String DB_TYPE = "InfluxDB";
+
+ public static final String PING_METHOD = "ping";
+ public static final String WRITE_METHOD = "write";
+ public static final String QUERY_METHOD = "query";
+ public static final String CREATE_DATABASE_METHOD = "createDatabase";
+ public static final String DELETE_DATABASE_METHOD = "deleteDatabase";
+ public static final String FLUSH_METHOD = "flush";
+ public static final String CREATE_RETENTION_POLICY_METHOD = "createRetentionPolicy";
+ public static final String DROP_RETENTION_POLICY_METHOD = "dropRetentionPolicy";
+
+ public static final Set MATCHER_METHOD_NAME = new HashSet<>(Arrays.asList(PING_METHOD, WRITE_METHOD, QUERY_METHOD, CREATE_DATABASE_METHOD, DELETE_DATABASE_METHOD, FLUSH_METHOD, CREATE_RETENTION_POLICY_METHOD, DROP_RETENTION_POLICY_METHOD));
+
+}
diff --git a/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/define/InfluxDBInstrumentation.java b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/define/InfluxDBInstrumentation.java
new file mode 100644
index 000000000000..3570975f447c
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/define/InfluxDBInstrumentation.java
@@ -0,0 +1,100 @@
+/*
+ * 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.influxdb.define;
+
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.matcher.ElementMatcher;
+import net.bytebuddy.matcher.ElementMatchers;
+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 org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
+
+import java.util.Set;
+
+import static net.bytebuddy.matcher.ElementMatchers.named;
+import static net.bytebuddy.matcher.ElementMatchers.none;
+
+
+/**
+ * Enhance InfluxDB InfluxDBFactory
+ * Really impl class {@link org.influxdb.impl.InfluxDBImpl}
+ *
+ * @since 2020/05/22
+ */
+public class InfluxDBInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
+
+ private static final String ENHANCE_CLASS = "org.influxdb.impl.InfluxDBImpl";
+ private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.influxdb.interceptor.InfluxDBConstructorInterceptor";
+ private static final String INFLUXDB_METHOD_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.influxdb.interceptor.InfluxDBMethodInterceptor";
+
+ @Override
+ protected ClassMatch enhanceClass() {
+ return NameMatch.byName(ENHANCE_CLASS);
+ }
+
+ @Override
+ public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
+ return new ConstructorInterceptPoint[] {
+ new ConstructorInterceptPoint() {
+ @Override
+ public ElementMatcher getConstructorMatcher() {
+ return ElementMatchers.takesArgument(0, String.class);
+ }
+
+ @Override
+ public String getConstructorInterceptor() {
+ return INTERCEPTOR_CLASS;
+ }
+ }
+ };
+ }
+
+ @Override
+ public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
+ return new InstanceMethodsInterceptPoint[] {
+ new InstanceMethodsInterceptPoint() {
+ @Override
+ public ElementMatcher getMethodsMatcher() {
+ ElementMatcher.Junction matcher = none();
+ final Set setters = Constants.MATCHER_METHOD_NAME;
+ for (String setter : setters) {
+ matcher = matcher.or(named(setter));
+ }
+ return matcher;
+ }
+
+ @Override
+ public String getMethodsInterceptor() {
+ return getInstanceMethodsInterceptor();
+ }
+
+ @Override
+ public boolean isOverrideArgs() {
+ return false;
+ }
+ }
+ };
+ }
+
+ protected String getInstanceMethodsInterceptor() {
+ return INFLUXDB_METHOD_INTERCEPT_CLASS;
+ }
+}
diff --git a/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/interceptor/InfluxDBConstructorInterceptor.java b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/interceptor/InfluxDBConstructorInterceptor.java
new file mode 100644
index 000000000000..d73f9f1bda2a
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/interceptor/InfluxDBConstructorInterceptor.java
@@ -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.
+ *
+ */
+
+package org.apache.skywalking.apm.plugin.influxdb.interceptor;
+
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
+
+public class InfluxDBConstructorInterceptor implements InstanceConstructorInterceptor {
+
+ @Override
+ public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
+ String url = (String) allArguments[0];
+ objInst.setSkyWalkingDynamicField(url);
+ }
+
+}
diff --git a/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/interceptor/InfluxDBMethodInterceptor.java b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/interceptor/InfluxDBMethodInterceptor.java
new file mode 100644
index 000000000000..252cc6434b94
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/interceptor/InfluxDBMethodInterceptor.java
@@ -0,0 +1,98 @@
+/*
+ * 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.influxdb.interceptor;
+
+import org.apache.skywalking.apm.agent.core.conf.Config;
+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 org.apache.skywalking.apm.plugin.influxdb.define.Constants;
+import org.influxdb.dto.BatchPoints;
+import org.influxdb.dto.Point;
+import org.influxdb.dto.Query;
+
+import java.lang.reflect.Method;
+
+import static org.apache.skywalking.apm.plugin.influxdb.define.Constants.DB_TYPE;
+
+public class InfluxDBMethodInterceptor implements InstanceMethodsAroundInterceptor {
+
+ @Override
+ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class>[] argumentsTypes,
+ MethodInterceptResult result) throws Throwable {
+ String methodName = method.getName();
+ String peer = String.valueOf(objInst.getSkyWalkingDynamicField());
+ AbstractSpan span = ContextManager.createExitSpan("InfluxDB/" + methodName, peer);
+ span.setComponent(ComponentsDefine.INFLUXDB_JAVA);
+ SpanLayer.asDB(span);
+ Tags.DB_TYPE.set(span, DB_TYPE);
+
+ if (allArguments.length <= 0 || !Config.Plugin.InfluxDB.TRACE_INFLUXQL) {
+ return;
+ }
+
+ if (allArguments[0] instanceof Query) {
+ Query query = (Query) allArguments[0];
+ Tags.DB_INSTANCE.set(span, query.getDatabase());
+ Tags.DB_STATEMENT.set(span, query.getCommand());
+ return;
+ }
+
+ if (Constants.WRITE_METHOD.equals(methodName)) {
+ if (allArguments[0] instanceof BatchPoints) {
+ BatchPoints batchPoints = (BatchPoints) allArguments[0];
+ Tags.DB_INSTANCE.set(span, batchPoints.getDatabase());
+ Tags.DB_STATEMENT.set(span, batchPoints.lineProtocol());
+ return;
+ }
+ if (allArguments.length == 5) {
+ if (allArguments[0] instanceof String) {
+ Tags.DB_INSTANCE.set(span, (String) allArguments[0]);
+ }
+ if (allArguments[4] instanceof String) {
+ Tags.DB_STATEMENT.set(span, (String) allArguments[4]);
+ }
+ return;
+ }
+ if (allArguments.length == 3 && allArguments[2] instanceof Point) {
+ Tags.DB_INSTANCE.set(span, (String) allArguments[0]);
+ Tags.DB_STATEMENT.set(span, ((Point) allArguments[2]).lineProtocol());
+ }
+ }
+
+ }
+
+ @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/influxdb-2.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/resources/skywalking-plugin.def
new file mode 100644
index 000000000000..ae831a911464
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/resources/skywalking-plugin.def
@@ -0,0 +1,17 @@
+# 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.
+
+influxdb-2.x=org.apache.skywalking.apm.plugin.influxdb.define.InfluxDBInstrumentation
diff --git a/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/influxdb/InfluxDBConstructorInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/influxdb/InfluxDBConstructorInterceptorTest.java
new file mode 100644
index 000000000000..434dff094dcb
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/influxdb/InfluxDBConstructorInterceptorTest.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.apm.plugin.influxdb;
+
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+import org.apache.skywalking.apm.plugin.influxdb.interceptor.InfluxDBConstructorInterceptor;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import static org.mockito.Mockito.verify;
+
+@RunWith(MockitoJUnitRunner.class)
+public class InfluxDBConstructorInterceptorTest {
+
+ private static final String INFLUXDB_URL = "http://127.0.0.1:8086";
+
+ private InfluxDBConstructorInterceptor interceptor;
+
+ @Mock
+ private EnhancedInstance enhancedInstance;
+
+ @Before
+ public void setUp() throws Exception {
+ interceptor = new InfluxDBConstructorInterceptor();
+ }
+
+ @Test
+ public void onConstruct() throws Exception {
+ interceptor.onConstruct(enhancedInstance, new Object[] {INFLUXDB_URL});
+
+ verify(enhancedInstance).setSkyWalkingDynamicField(INFLUXDB_URL);
+ }
+
+ @Test
+ public void onConstructWithUsernameAndPassword() {
+ interceptor.onConstruct(enhancedInstance, new Object[] {
+ INFLUXDB_URL,
+ "admin",
+ "123456",
+ null
+ });
+
+ verify(enhancedInstance).setSkyWalkingDynamicField(INFLUXDB_URL);
+ }
+
+}
diff --git a/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/influxdb/InfluxDBMethodInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/influxdb/InfluxDBMethodInterceptorTest.java
new file mode 100644
index 000000000000..342b0a446f63
--- /dev/null
+++ b/apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/influxdb/InfluxDBMethodInterceptorTest.java
@@ -0,0 +1,169 @@
+/*
+ * 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.influxdb;
+
+import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
+import org.apache.skywalking.apm.agent.core.context.trace.LogDataEntity;
+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.context.util.TagValuePair;
+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.helper.SpanHelper;
+import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule;
+import org.apache.skywalking.apm.agent.test.tools.SegmentStorage;
+import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint;
+import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner;
+import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
+import org.apache.skywalking.apm.plugin.influxdb.define.Constants;
+import org.apache.skywalking.apm.plugin.influxdb.interceptor.InfluxDBMethodInterceptor;
+import org.hamcrest.CoreMatchers;
+import org.influxdb.InfluxDB;
+import org.influxdb.InfluxDBException;
+import org.influxdb.dto.Query;
+import org.influxdb.impl.InfluxDBImpl;
+import org.junit.Assert;
+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 java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import static junit.framework.TestCase.assertNotNull;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.when;
+
+@RunWith(PowerMockRunner.class)
+@PowerMockRunnerDelegate(TracingSegmentRunner.class)
+public class InfluxDBMethodInterceptorTest {
+
+ @SegmentStoragePoint
+ private SegmentStorage segmentStorage;
+
+ @Rule
+ public AgentServiceRule serviceRule = new AgentServiceRule();
+
+ @Mock
+ private EnhancedInstance enhancedInstance;
+
+ private InfluxDBMethodInterceptor interceptor;
+
+ private Object[] writeArguments;
+
+ private Class[] writeArgumentTypes;
+
+ private Object[] queryArguments;
+
+ private Class[] queryArgumentTypes;
+
+ @Before
+ public void setUp() throws Exception {
+ // write
+ writeArguments = new Object[] {
+ "sw8", "auto_gen", InfluxDB.ConsistencyLevel.ALL, TimeUnit.SECONDS,
+ "weather,location=us-midwest temperature=82 1465839830100400200"
+ };
+ writeArgumentTypes = new Class[] {
+ String.class, String.class, InfluxDB.ConsistencyLevel.class, TimeUnit.class, String.class
+ };
+
+ // query
+ queryArguments = new Object[] {
+ new Query("select * from weather limit 1", "sw8")
+ };
+ queryArgumentTypes = new Class[] {
+ Query.class
+ };
+
+ interceptor = new InfluxDBMethodInterceptor();
+ when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("http://127.0.0.1:8086");
+ }
+
+ @Test
+ public void testIntercept() throws Throwable {
+ interceptor.beforeMethod(enhancedInstance, getMockWriteMethod(), writeArguments, writeArgumentTypes, null);
+ interceptor.afterMethod(enhancedInstance, getMockQueryMethod(), queryArguments, queryArgumentTypes, null);
+
+ TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
+ List spans = SegmentHelper.getSpans(traceSegment);
+ assertThat(spans.size(), is(1));
+ assertWriteInfluxDBSpan(spans.get(0));
+ }
+
+ @Test
+ public void testInterceptWithException() throws Throwable {
+ interceptor.beforeMethod(enhancedInstance, getMockWriteMethod(), writeArguments, writeArgumentTypes, null);
+ interceptor.handleMethodException(enhancedInstance, getMockWriteMethod(), writeArguments, writeArgumentTypes, new InfluxDBException("test exception"));
+ interceptor.afterMethod(enhancedInstance, getMockWriteMethod(), writeArguments, writeArgumentTypes, null);
+
+ TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
+ List spans = SegmentHelper.getSpans(traceSegment);
+ assertThat(spans.size(), is(1));
+ assertWriteInfluxDBSpan(spans.get(0));
+
+ assertLogData(SpanHelper.getLogs(spans.get(0)));
+ }
+
+ private void assertLogData(List logDataEntities) {
+ assertThat(logDataEntities.size(), is(1));
+ LogDataEntity logData = logDataEntities.get(0);
+ Assert.assertThat(logData.getLogs().size(), is(4));
+ Assert.assertThat(logData.getLogs().get(0).getValue(),
+ CoreMatchers.