From ee6841fc13ffb246566283184e955381ae243644 Mon Sep 17 00:00:00 2001 From: XiaoFu <248628578@qq.com> Date: Thu, 3 Jan 2019 23:51:22 +0800 Subject: [PATCH] Support Gson Plugin (#2090) * Support Gson plugin --- .../trace/component/ComponentsDefine.java | 5 +- apm-sniffer/apm-sdk-plugin/pom.xml | 2 +- .../gson-2.8.x-plugin/pom.xml | 45 ++++++++++ .../plugin/gson/GsonFromJsonInterceptor.java | 62 +++++++++++++ .../plugin/gson/GsonToJsonInterceptor.java | 61 +++++++++++++ .../define/GsonFromJsonInstrumentation.java | 71 +++++++++++++++ .../define/GsonToJsonInstrumentation.java | 88 +++++++++++++++++++ .../src/main/resources/skywalking-plugin.def | 18 ++++ .../gson/GsonFromJsonInterceptorTest.java | 80 +++++++++++++++++ .../gson/GsonToJsonInterceptorTest.java | 79 +++++++++++++++++ apm-sniffer/optional-plugins/pom.xml | 1 + docker/config/component-libraries.yml | 3 + .../setup/service-agent/java-agent/README.md | 1 + .../java-agent/Supported-list.md | 2 + .../main/resources/component-libraries.yml | 3 + 15 files changed, 519 insertions(+), 2 deletions(-) create mode 100644 apm-sniffer/optional-plugins/gson-2.8.x-plugin/pom.xml create mode 100644 apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/GsonFromJsonInterceptor.java create mode 100644 apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/GsonToJsonInterceptor.java create mode 100644 apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/define/GsonFromJsonInstrumentation.java create mode 100644 apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/define/GsonToJsonInstrumentation.java create mode 100644 apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/resources/skywalking-plugin.def create mode 100644 apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/gson/GsonFromJsonInterceptorTest.java create mode 100644 apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/gson/GsonToJsonInterceptorTest.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 fd155d1d07..adc249c6a7 100644 --- 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 @@ -106,6 +106,8 @@ public class ComponentsDefine { public static final OfficialComponent CANAL = new OfficialComponent(54,"Canal"); + public static final OfficialComponent GSON = new OfficialComponent(55,"Gson"); + private static ComponentsDefine INSTANCE = new ComponentsDefine(); private String[] components; @@ -115,7 +117,7 @@ public class ComponentsDefine { } public ComponentsDefine() { - components = new String[55]; + components = new String[56]; addComponent(TOMCAT); addComponent(HTTPCLIENT); addComponent(DUBBO); @@ -155,6 +157,7 @@ public class ComponentsDefine { addComponent(RABBITMQ_PRODUCER); addComponent(RABBITMQ_CONSUMER); addComponent(CANAL); + addComponent(GSON); } private void addComponent(OfficialComponent component) { diff --git a/apm-sniffer/apm-sdk-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/pom.xml index 13a4c4b02c..02ab4fa77d 100644 --- a/apm-sniffer/apm-sdk-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/pom.xml @@ -60,7 +60,7 @@ activemq-5.x-plugin elasticsearch-5.x-plugin undertow-plugins - rabbitmq-5.x-plugin + rabbitmq-5.x-plugin dubbo-conflict-patch canal-1.x-plugin diff --git a/apm-sniffer/optional-plugins/gson-2.8.x-plugin/pom.xml b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/pom.xml new file mode 100644 index 0000000000..0046f95acb --- /dev/null +++ b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/pom.xml @@ -0,0 +1,45 @@ + + + + + 4.0.0 + + org.apache.skywalking + optional-plugins + 6.0.0-GA-SNAPSHOT + + + apm-gson-2.x-plugin + gson-2.8.x-plugin + jar + + + UTF-8 + 2.8.5 + + + + + com.google.code.gson + gson + ${gson.version} + provided + + + diff --git a/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/GsonFromJsonInterceptor.java b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/GsonFromJsonInterceptor.java new file mode 100644 index 0000000000..af89c4a86b --- /dev/null +++ b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/GsonFromJsonInterceptor.java @@ -0,0 +1,62 @@ +/* + * 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.gson; + +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; + + +/** + * @author withlin + */ +public class GsonFromJsonInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + + AbstractSpan span = ContextManager.createLocalSpan("Gson/FromJson"); + span.setComponent(ComponentsDefine.GSON); + Integer length = allArguments[0].toString().length(); + span.tag("length", length.toString()); + + } + + @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); + } +} \ No newline at end of file diff --git a/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/GsonToJsonInterceptor.java b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/GsonToJsonInterceptor.java new file mode 100644 index 0000000000..931fe47366 --- /dev/null +++ b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/GsonToJsonInterceptor.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.gson; + +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; + +/** + * @author withlin + */ +public class GsonToJsonInterceptor implements InstanceMethodsAroundInterceptor { + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, + MethodInterceptResult result) throws Throwable { + + AbstractSpan span = ContextManager.createLocalSpan("Gson/ToJson"); + span.setComponent(ComponentsDefine.GSON); + Integer length = allArguments[0].toString().length(); + span.tag("length", length.toString()); + + } + + @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/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/define/GsonFromJsonInstrumentation.java b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/define/GsonFromJsonInstrumentation.java new file mode 100644 index 0000000000..6aab959dd5 --- /dev/null +++ b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/define/GsonFromJsonInstrumentation.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.gson.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.MultiClassNameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; + + +/** + * @author withlin + */ +public class GsonFromJsonInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + public static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.gson.GsonFromJsonInterceptor"; + public static final String ENHANCE_CLASS = "com.google.gson.Gson"; + public static final String ENHANCE_METHOD_DISPATCH = "fromJson"; + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override public ElementMatcher getMethodsMatcher() { + return named(ENHANCE_METHOD_DISPATCH) + .and(takesArgumentWithType(0,"com.google.gson.stream.JsonReader")); + } + + + @Override public String getMethodsInterceptor() { + return INTERCEPTOR_CLASS; + } + + @Override public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + protected ClassMatch enhanceClass() { + return MultiClassNameMatch.byMultiClassMatch(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/define/GsonToJsonInstrumentation.java b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/define/GsonToJsonInstrumentation.java new file mode 100644 index 0000000000..4ac03826e1 --- /dev/null +++ b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/gson/define/GsonToJsonInstrumentation.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.gson.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.MultiClassNameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; + + +/** + * @author withlin + */ +public class GsonToJsonInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + public static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.gson.GsonToJsonInterceptor"; + public static final String ENHANCE_CLASS = "com.google.gson.Gson"; + public static final String ENHANCE_METHOD = "toJson"; + + @Override + protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override public ElementMatcher getMethodsMatcher() { + return named(ENHANCE_METHOD) + .and(takesArgumentWithType(2,"com.google.gson.stream.JsonWriter")); + } + + + @Override public String getMethodsInterceptor() { + return INTERCEPTOR_CLASS; + } + + @Override public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named(ENHANCE_METHOD) + .and(takesArgumentWithType(1,"com.google.gson.stream.JsonWriter")); + } + + @Override + public String getMethodsInterceptor() { + return INTERCEPTOR_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + protected ClassMatch enhanceClass() { + return MultiClassNameMatch.byMultiClassMatch(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/resources/skywalking-plugin.def new file mode 100644 index 0000000000..0c28770bac --- /dev/null +++ b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/main/resources/skywalking-plugin.def @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +gson-2.8.x=org.apache.skywalking.apm.plugin.gson.define.GsonToJsonInstrumentation +gson-2.8.x=org.apache.skywalking.apm.plugin.gson.define.GsonFromJsonInstrumentation \ No newline at end of file diff --git a/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/gson/GsonFromJsonInterceptorTest.java b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/gson/GsonFromJsonInterceptorTest.java new file mode 100644 index 0000000000..f08d7df6dd --- /dev/null +++ b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/gson/GsonFromJsonInterceptorTest.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.plugin.gson; + +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; +import org.apache.skywalking.apm.agent.test.helper.SegmentHelper; +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.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; + +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + + + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class GsonFromJsonInterceptorTest { + + private GsonFromJsonInterceptor gsonFromJsonInterceptor; + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + private Object[] arguments; + + private Class[] argumentType; + + + + + @Before + public void setUp() { + gsonFromJsonInterceptor = new GsonFromJsonInterceptor(); + arguments = new Object[] {"100"}; + } + + @Test + public void testSendMessage() throws Throwable { + gsonFromJsonInterceptor.beforeMethod(null, null, arguments, null, null); + gsonFromJsonInterceptor.afterMethod(null, null, arguments, null, null); + + List traceSegmentList = segmentStorage.getTraceSegments(); + assertThat(traceSegmentList.size(), is(1)); + + TraceSegment segment = traceSegmentList.get(0); + List spans = SegmentHelper.getSpans(segment); + assertThat(spans.size(), is(1)); + } + +} \ No newline at end of file diff --git a/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/gson/GsonToJsonInterceptorTest.java b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/gson/GsonToJsonInterceptorTest.java new file mode 100644 index 0000000000..ce4b68c3c5 --- /dev/null +++ b/apm-sniffer/optional-plugins/gson-2.8.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/gson/GsonToJsonInterceptorTest.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.gson; + +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; +import org.apache.skywalking.apm.agent.test.helper.SegmentHelper; +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.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; + +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class GsonToJsonInterceptorTest { + + private GsonToJsonInterceptor gsonToJsonInterceptor; + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + private Object[] arguments; + + private Class[] argumentType; + + + + + @Before + public void setUp() { + gsonToJsonInterceptor = new GsonToJsonInterceptor(); + arguments = new Object[] {"100"}; + } + + @Test + public void testSendMessage() throws Throwable { + gsonToJsonInterceptor.beforeMethod(null, null, arguments, null, null); + gsonToJsonInterceptor.afterMethod(null, null, arguments, null, null); + + List traceSegmentList = segmentStorage.getTraceSegments(); + assertThat(traceSegmentList.size(), is(1)); + + TraceSegment segment = traceSegmentList.get(0); + List spans = SegmentHelper.getSpans(segment); + assertThat(spans.size(), is(1)); + } + +} diff --git a/apm-sniffer/optional-plugins/pom.xml b/apm-sniffer/optional-plugins/pom.xml index cced450014..1e5f2ea3b3 100644 --- a/apm-sniffer/optional-plugins/pom.xml +++ b/apm-sniffer/optional-plugins/pom.xml @@ -43,6 +43,7 @@ optional-spring-plugins trace-ignore-plugin + gson-2.8.x-plugin diff --git a/docker/config/component-libraries.yml b/docker/config/component-libraries.yml index 0c1c670936..9bb3c843e6 100644 --- a/docker/config/component-libraries.yml +++ b/docker/config/component-libraries.yml @@ -189,6 +189,9 @@ rabbitmq-consumer: Canal: id: 54 languages: Java +Gson: + id: 55 + languages: Java # .NET/.NET Core components # [3000, 4000) for C#/.NET only diff --git a/docs/en/setup/service-agent/java-agent/README.md b/docs/en/setup/service-agent/java-agent/README.md index f9d0653875..948e1b3e84 100644 --- a/docs/en/setup/service-agent/java-agent/README.md +++ b/docs/en/setup/service-agent/java-agent/README.md @@ -86,6 +86,7 @@ Now, we have the following known optional plugins. * [Trace Spring beans](agent-optional-plugins/Spring-bean-plugins.md) * [Trace Oracle and Resin](agent-optional-plugins/Oracle-Resin-plugins.md) * [Filter traces through specified endpoint name patterns](agent-optional-plugins/trace-ignore-plugin.md) +* Gson serialization lib in optional plugin folder ## Advanced Features * Set the settings through system properties for config file override. Read [setting override](Setting-override.md). 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 fce00569ac..a9ff4cbec4 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 @@ * [Elastic Job](https://github.com/elasticjob/elastic-job) 2.x * OpenTracing community supported * [Canal: Alibaba mysql database binlog incremental subscription & consumer components](https://github.com/alibaba/canal) 1.0.25 -> 1.1.2 +* JSON + * [GSON](https://github.com/google/gson) 2.8.x (Optional²) ¹Due to license incompatibilities/restrictions these plugins are hosted and released in 3rd part repository, go to [OpenSkywalking java plugin extension repository](https://github.com/OpenSkywalking/java-plugin-extensions) to get these. 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 ba03e966fa..86f41c7041 100644 --- a/oap-server/server-starter/src/main/resources/component-libraries.yml +++ b/oap-server/server-starter/src/main/resources/component-libraries.yml @@ -189,6 +189,9 @@ rabbitmq-consumer: Canal: id: 54 languages: Java +Gson: + id: 55 + languages: Java # .NET/.NET Core components -- GitLab