You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2020/06/06 07:13:08 UTC

[GitHub] [skywalking] kezhenxu94 commented on a change in pull request #4846: New feature provided : new plugin for influxdb-java client

kezhenxu94 commented on a change in pull request #4846:
URL: https://github.com/apache/skywalking/pull/4846#discussion_r436245114



##########
File path: apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/define/InfluxDBInstrumentation.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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 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 org.apache.skywalking.apm.plugin.influxdb.InfluxDBMethodMatch;
+
+import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
+
+/**
+ * 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";

Review comment:
       ```suggestion
       private static final String INFLUXDB_METHOD_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.influxdb.interceptor.InfluxDBMethodInterceptor";
   ```

##########
File path: apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/define/InfluxDBInstrumentation.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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 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 org.apache.skywalking.apm.plugin.influxdb.InfluxDBMethodMatch;
+
+import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
+
+/**
+ * enhance InfluxDB InfluxDBFactory
+ * really impl class {@link org.influxdb.impl.InfluxDBImpl}
+ *
+ * @since  2020/05/22

Review comment:
       Capitalize the first letter of the sentence please

##########
File path: apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/pom.xml
##########
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  ~
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <parent>
+    <artifactId>apm-sdk-plugin</artifactId>
+    <groupId>org.apache.skywalking</groupId>
+    <version>8.0.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>apm-influxdb-2.x-plugin</artifactId>
+  <description>This plugin is for use with InfluxDB 1.x.</description>

Review comment:
       The `artifactId` and the `description` don't match, one says it's for `2.x` while the other says it's for `1.x`

##########
File path: test/plugin/scenarios/influxdb-scenario/src/test/java/org/apache/skywalking/apm/testcase/influxdb/executor/InfluxDBExecutorTest.java
##########
@@ -0,0 +1,37 @@
+/*
+ * 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.influxdb.executor;
+
+/**
+ * InfluxDBExecutorTest
+ *
+ * @author guhao
+ * @since 2020/6/3
+ */
+public class InfluxDBExecutorTest {
+
+//  @Test
+//  public void testPing(){
+//    InfluxDBExecutor executor = new InfluxDBExecutor("http://localhost:8086");
+//    Pong pong = executor.ping();
+//    System.out.println(pong.getVersion());
+//    Assert.assertNotNull(pong.getVersion());
+//  }
+
+}

Review comment:
       Remove this if it's useless

##########
File path: test/plugin/scenarios/influxdb-scenario/src/main/java/org/apache/skywalking/apm/testcase/influxdb/executor/InfluxDBExecutor.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.testcase.influxdb.executor;
+
+import org.influxdb.InfluxDB;
+import org.influxdb.InfluxDBFactory;
+import org.influxdb.dto.Point;
+import org.influxdb.dto.Pong;
+import org.influxdb.dto.Query;
+import org.influxdb.dto.QueryResult;
+
+/**
+ * InfluxDBExecutor
+ *
+ * @author guhao
+ * @since 2020/6/3

Review comment:
       Remove author tag

##########
File path: apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/influxdb/define/InfluxDBInstrumentation.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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 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 org.apache.skywalking.apm.plugin.influxdb.InfluxDBMethodMatch;
+
+import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
+
+/**
+ * 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<MethodDescription> getConstructorMatcher() {
+                    return takesArgument(0, String.class);
+                }
+
+                @Override
+                public String getConstructorInterceptor() {
+                    return INTERCEPTOR_CLASS;
+                }
+            }
+        };
+    }
+
+    @Override
+    public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
+      return new InstanceMethodsInterceptPoint[] {
+          new InstanceMethodsInterceptPoint() {
+            @Override
+            public ElementMatcher<MethodDescription> getMethodsMatcher() {
+              return InfluxDBMethodMatch.INSTANCE.getInfluxDBMethodMatcher();

Review comment:
       This is good for encapsulation, but it's a pitfall that we check third-party classes in classes whose name end with `*Instrumentation`
   
   https://github.com/apache/skywalking/blob/f3d907bf6df924e7cc7e65efbdfd847920ce47e5/apm-checkstyle/checkStyle.xml#L122-L129
   
   to avoid issues like this https://github.com/apache/skywalking/pull/2871 , but this breaks the checks, although there is no third-party class in the `InfluxDBMethodMatch` for now, other reviewers should pay attention to this class in the future, FYI @wu-sheng 

##########
File path: apm-sniffer/apm-sdk-plugin/influxdb-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/influxdb/InfluxDBMethodInterceptorTest.java
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.interceptor.InfluxDBMethodInterceptor;
+import org.hamcrest.CoreMatchers;
+import org.influxdb.InfluxDBException;
+import org.influxdb.dto.Point;
+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 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[] allArgument;
+
+    private Class[] argumentType;
+
+    @Before
+    public void setUp() throws Exception {
+        allArgument = new Object[] {
+            Point.measurement("cpu")
+                .tag("host", "127.0.0.1")
+                .addField("use_idle", 0.8)
+                .build()
+        };
+        argumentType = new Class[] {
+            Point.class
+        };
+
+        interceptor = new InfluxDBMethodInterceptor();
+        when(enhancedInstance.getSkyWalkingDynamicField()).thenReturn("http://127.0.0.1:8086");
+    }
+
+    @Test
+    public void testIntercept() throws Throwable {
+        interceptor.beforeMethod(enhancedInstance, getMockWriteMethod(), allArgument, argumentType, null);
+        interceptor.afterMethod(enhancedInstance, getMockWriteMethod(), allArgument, argumentType, null);
+
+        TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
+        List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
+        assertThat(spans.size(), is(1));
+        assertInfluxDBSpan(spans.get(0));
+    }
+
+    @Test
+    public void testInterceptWithException() throws Throwable {
+        interceptor.beforeMethod(enhancedInstance, getMockWriteMethod(), allArgument, argumentType, null);
+        interceptor.handleMethodException(enhancedInstance, getMockWriteMethod(), allArgument, argumentType, new InfluxDBException("test exception"));
+        interceptor.afterMethod(enhancedInstance, getMockWriteMethod(), allArgument, argumentType, null);
+
+        TraceSegment traceSegment = segmentStorage.getTraceSegments().get(0);
+        List<AbstractTracingSpan> spans = SegmentHelper.getSpans(traceSegment);
+        assertThat(spans.size(), is(1));
+        assertInfluxDBSpan(spans.get(0));
+
+        assertLogData(SpanHelper.getLogs(spans.get(0)));
+    }
+
+    private void assertLogData(List<LogDataEntity> 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.<Object>is("error"));
+        Assert.assertThat(logData.getLogs()
+                                 .get(1)
+                                 .getValue(), CoreMatchers.<Object>is(InfluxDBException.class.getName()));
+        Assert.assertEquals("test exception", logData.getLogs().get(2).getValue());
+        assertNotNull(logData.getLogs().get(3).getValue());
+    }
+
+    private void assertInfluxDBSpan(AbstractTracingSpan span) {
+        assertThat(span.getOperationName(), is("InfluxDB/write"));
+        assertThat(span.isExit(), is(true));
+        assertThat(SpanHelper.getComponentId(span), is(ComponentsDefine.INFLUXDB_JAVA.getId()));
+        List<TagValuePair> tags = SpanHelper.getTags(span);
+        assertThat(tags.get(0).getValue(), is("InfluxDB"));
+//        assertThat(tags.get(1).getValue(), is("write ".concat(allArgument[0].toString())));

Review comment:
       Remove if useless, or fix it if assertion is failed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org