diff --git a/.github/workflows/plugins-test.0.yaml b/.github/workflows/plugins-test.0.yaml index 24fba53bf..a6692a2b9 100644 --- a/.github/workflows/plugins-test.0.yaml +++ b/.github/workflows/plugins-test.0.yaml @@ -85,6 +85,7 @@ jobs: - springmvc-reactive-scenario - springmvc-reactive-devtools-scenario - fastjson-scenario + - jackson-scenario steps: - uses: actions/checkout@v2 with: diff --git a/CHANGES.md b/CHANGES.md index f5213f7b7..49c9f6fd6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -23,6 +23,7 @@ Release Notes. * Provide HikariCP database connection pool plugin. * Fix NumberFormat exception in jdbc-commons plugin when MysqlURLParser parser jdbcurl * Provide Alibaba Fastjson parser/generator plugin. +* Provide Jackson serialization and deserialization plugin. * Fix a tracing context leak of SpringMVC plugin, when an internal exception throws due to response can't be found. * Make GRPC log reporter sharing GRPC channel with other reporters of agent. Remove config items of `agent.conf`, `plugin.toolkit.log.grpc.reporter.server_host`, `plugin.toolkit.log.grpc.reporter.server_port`, and `plugin.toolkit.log.grpc.reporter.upstream_timeout`. rename `plugin.toolkit.log.grpc.reporter.max_message_size` to `log.max_message_size`. 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 8f8796117..35902c955 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 @@ -213,4 +213,6 @@ public class ComponentsDefine { public static final OfficialComponent FASTJSON = new OfficialComponent(117, "Fastjson"); + public static final OfficialComponent JACKSON = new OfficialComponent(118, "Jackson"); + } diff --git a/apm-sniffer/optional-plugins/fastjson-1.2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/fastjson/define/FastjsonInstrumentation.java b/apm-sniffer/optional-plugins/fastjson-1.2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/fastjson/define/FastjsonInstrumentation.java index 00d23c071..a69a528b9 100644 --- a/apm-sniffer/optional-plugins/fastjson-1.2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/fastjson/define/FastjsonInstrumentation.java +++ b/apm-sniffer/optional-plugins/fastjson-1.2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/fastjson/define/FastjsonInstrumentation.java @@ -43,25 +43,25 @@ import static net.bytebuddy.matcher.ElementMatchers.named; */ public class FastjsonInstrumentation extends ClassStaticMethodsEnhancePluginDefine { - public static final String ENHANCE_CLASS = "com.alibaba.fastjson.JSON"; + private static final String ENHANCE_CLASS = "com.alibaba.fastjson.JSON"; - private static final Map ENHANCE_METHODS = new HashMap() { - { - put("parseArray", "org.apache.skywalking.apm.plugin.fastjson.ParseArrayInterceptor"); - put("parse", "org.apache.skywalking.apm.plugin.fastjson.ParseInterceptor"); - put("parseObject", "org.apache.skywalking.apm.plugin.fastjson.ParseObjectInterceptor"); - put("toJavaObject", "org.apache.skywalking.apm.plugin.fastjson.ToJavaObjectInterceptor"); - put("toJSONBytes", "org.apache.skywalking.apm.plugin.fastjson.ToJsonBytesInterceptor"); - put("toJSON", "org.apache.skywalking.apm.plugin.fastjson.ToJsonInterceptor"); - put("toJSONString", "org.apache.skywalking.apm.plugin.fastjson.ToJsonStringInterceptor"); - put("writeJSONString", "org.apache.skywalking.apm.plugin.fastjson.WriteJsonStringInterceptor"); - } - }; + private static final Map ENHANCE_METHODS = new HashMap<>(); + + static { + ENHANCE_METHODS.put("parseArray", "org.apache.skywalking.apm.plugin.fastjson.ParseArrayInterceptor"); + ENHANCE_METHODS.put("parse", "org.apache.skywalking.apm.plugin.fastjson.ParseInterceptor"); + ENHANCE_METHODS.put("parseObject", "org.apache.skywalking.apm.plugin.fastjson.ParseObjectInterceptor"); + ENHANCE_METHODS.put("toJavaObject", "org.apache.skywalking.apm.plugin.fastjson.ToJavaObjectInterceptor"); + ENHANCE_METHODS.put("toJSONBytes", "org.apache.skywalking.apm.plugin.fastjson.ToJsonBytesInterceptor"); + ENHANCE_METHODS.put("toJSON", "org.apache.skywalking.apm.plugin.fastjson.ToJsonInterceptor"); + ENHANCE_METHODS.put("toJSONString", "org.apache.skywalking.apm.plugin.fastjson.ToJsonStringInterceptor"); + ENHANCE_METHODS.put("writeJSONString", "org.apache.skywalking.apm.plugin.fastjson.WriteJsonStringInterceptor"); + } @Override public StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() { - final List points = new ArrayList(ENHANCE_METHODS.size()); + final List points = new ArrayList<>(ENHANCE_METHODS.size()); for (Map.Entry entry : ENHANCE_METHODS.entrySet()) { final StaticMethodsInterceptPoint point = new StaticMethodsInterceptPoint() { @@ -83,7 +83,7 @@ public class FastjsonInstrumentation extends ClassStaticMethodsEnhancePluginDefi points.add(point); } - return points.toArray(new StaticMethodsInterceptPoint[points.size()]); + return points.toArray(new StaticMethodsInterceptPoint[0]); } @Override diff --git a/apm-sniffer/optional-plugins/jackson-2.x-plugin/pom.xml b/apm-sniffer/optional-plugins/jackson-2.x-plugin/pom.xml new file mode 100644 index 000000000..9323f6aee --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/pom.xml @@ -0,0 +1,52 @@ + + + + + 4.0.0 + + org.apache.skywalking + optional-plugins + 8.8.0-SNAPSHOT + + + apm-jackson-2.x-plugin + jackson-2.x-plugin + jar + + + UTF-8 + 2.9.9 + + + + + com.fasterxml.jackson.core + jackson-core + ${jackson.version} + provided + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + provided + + + diff --git a/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/BasicMethodsInterceptor.java b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/BasicMethodsInterceptor.java new file mode 100644 index 000000000..92bb50885 --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/BasicMethodsInterceptor.java @@ -0,0 +1,55 @@ +/* + * 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.jackson; + +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; + +public class BasicMethodsInterceptor implements InstanceMethodsAroundInterceptor { + + public static final String OPERATION_NAME_JACKSON = "Jackson/"; + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + + AbstractSpan span = ContextManager.createLocalSpan(OPERATION_NAME_JACKSON + + method.getDeclaringClass().getSimpleName() + "." + method.getName()); + span.setComponent(ComponentsDefine.JACKSON); + } + + @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().log(t); + } +} \ No newline at end of file diff --git a/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/ReadValueInterceptor.java b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/ReadValueInterceptor.java new file mode 100644 index 000000000..3668ed5b0 --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/ReadValueInterceptor.java @@ -0,0 +1,66 @@ +/* + * 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.jackson; + +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.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.io.File; +import java.lang.reflect.Method; + +public class ReadValueInterceptor implements InstanceMethodsAroundInterceptor { + + public static final String OPERATION_NAME_JACKSON = "Jackson/"; + public static final String SPAN_TAG_KEY_LENGTH = "length"; + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + + AbstractSpan span = ContextManager.createLocalSpan(OPERATION_NAME_JACKSON + + method.getDeclaringClass().getSimpleName() + "." + method.getName()); + span.setComponent(ComponentsDefine.JACKSON); + + if (allArguments[0] instanceof String) { + span.tag(Tags.ofKey(SPAN_TAG_KEY_LENGTH), Integer.toString(((String) allArguments[0]).length())); + } else if (allArguments[0] instanceof byte[]) { + span.tag(Tags.ofKey(SPAN_TAG_KEY_LENGTH), Integer.toString(((byte[]) allArguments[0]).length)); + } else if (allArguments[0] instanceof File) { + span.tag(Tags.ofKey(SPAN_TAG_KEY_LENGTH), Long.toString(((File) allArguments[0]).length())); + } + } + + @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().log(t); + } +} \ No newline at end of file diff --git a/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/WriteValueAsBytesInterceptor.java b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/WriteValueAsBytesInterceptor.java new file mode 100644 index 000000000..1d18cd017 --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/WriteValueAsBytesInterceptor.java @@ -0,0 +1,64 @@ +/* + * 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.jackson; + +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.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; + +public class WriteValueAsBytesInterceptor implements InstanceMethodsAroundInterceptor { + + public static final String OPERATION_NAME_JACKSON = "Jackson/"; + public static final String SPAN_TAG_KEY_LENGTH = "length"; + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + + AbstractSpan span = ContextManager.createLocalSpan(OPERATION_NAME_JACKSON + + method.getDeclaringClass().getSimpleName() + "." + method.getName()); + span.setComponent(ComponentsDefine.JACKSON); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Object ret) throws Throwable { + + if (!ContextManager.isActive()) { + return ret; + } + if (ret instanceof byte[]) { + ContextManager.activeSpan().tag(Tags.ofKey(SPAN_TAG_KEY_LENGTH), Integer.toString(((byte[]) ret).length)); + } + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().log(t); + } +} \ No newline at end of file diff --git a/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/WriteValueAsStringInterceptor.java b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/WriteValueAsStringInterceptor.java new file mode 100644 index 000000000..9eb2403ba --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/WriteValueAsStringInterceptor.java @@ -0,0 +1,64 @@ +/* + * 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.jackson; + +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.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; + +public class WriteValueAsStringInterceptor implements InstanceMethodsAroundInterceptor { + + public static final String OPERATION_NAME_JACKSON = "Jackson/"; + public static final String SPAN_TAG_KEY_LENGTH = "length"; + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, MethodInterceptResult result) throws Throwable { + + AbstractSpan span = ContextManager.createLocalSpan(OPERATION_NAME_JACKSON + + method.getDeclaringClass().getSimpleName() + "." + method.getName()); + span.setComponent(ComponentsDefine.JACKSON); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Object ret) throws Throwable { + + if (!ContextManager.isActive()) { + return ret; + } + if (ret instanceof String) { + ContextManager.activeSpan().tag(Tags.ofKey(SPAN_TAG_KEY_LENGTH), Integer.toString(((String) ret).length())); + } + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().log(t); + } +} \ No newline at end of file diff --git a/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/define/AbstractInstrumentation.java b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/define/AbstractInstrumentation.java new file mode 100644 index 000000000..05e1abb4d --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/define/AbstractInstrumentation.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.jackson.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 java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +/** + * Jackson provides a "one stop" solution for json serialization and deserialization solution + * basic requirements. + */ + +public abstract class AbstractInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + + final List points = new ArrayList<>(this.enhanceMethods().size()); + + for (Map.Entry entry : this.enhanceMethods().entrySet()) { + final InstanceMethodsInterceptPoint point = new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named(entry.getKey()); + } + + @Override + public String getMethodsInterceptor() { + return entry.getValue(); + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }; + points.add(point); + } + + return points.toArray(new InstanceMethodsInterceptPoint[0]); + } + + /** + * Define the method and interceptor kev/value. method name is key, interceptor class is value. + * + * @return enhance methods + */ + protected abstract Map enhanceMethods(); + +} diff --git a/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/define/ObjectMapperInstrumentation.java b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/define/ObjectMapperInstrumentation.java new file mode 100644 index 000000000..81b7e79a2 --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/define/ObjectMapperInstrumentation.java @@ -0,0 +1,56 @@ +/* + * 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.jackson.define; + +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import java.util.HashMap; +import java.util.Map; + +/** + * Jackson provides a "one stop" solution for json serialization and deserialization solution + * basic requirements. + *

+ * ObjectMapper: writeValue()\writeValueAsString()\writeValueAsBytes()\readValue() + */ + +public class ObjectMapperInstrumentation extends AbstractInstrumentation { + + private static final String ENHANCE_CLASS = "com.fasterxml.jackson.databind.ObjectMapper"; + + private static final Map ENHANCE_METHODS = new HashMap<>(); + + static { + ENHANCE_METHODS.put("writeValue", "org.apache.skywalking.apm.plugin.jackson.BasicMethodsInterceptor"); + ENHANCE_METHODS.put("writeValueAsString", "org.apache.skywalking.apm.plugin.jackson.WriteValueAsStringInterceptor"); + ENHANCE_METHODS.put("writeValueAsBytes", "org.apache.skywalking.apm.plugin.jackson.WriteValueAsBytesInterceptor"); + ENHANCE_METHODS.put("readValue", "org.apache.skywalking.apm.plugin.jackson.ReadValueInterceptor"); + } + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + protected Map enhanceMethods() { + return ENHANCE_METHODS; + } +} diff --git a/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/define/ObjectReaderInstrumentation.java b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/define/ObjectReaderInstrumentation.java new file mode 100644 index 000000000..38597b8f4 --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/define/ObjectReaderInstrumentation.java @@ -0,0 +1,54 @@ +/* + * 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.jackson.define; + +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import java.util.HashMap; +import java.util.Map; + +/** + * Jackson provides a "one stop" solution for json serialization and deserialization solution + * basic requirements. + *

+ * ObjectReader: readValue()\readValues() + */ + +public class ObjectReaderInstrumentation extends AbstractInstrumentation { + + private static final String ENHANCE_CLASS = "com.fasterxml.jackson.databind.ObjectReader"; + + private static final Map ENHANCE_METHODS = new HashMap<>(); + + static { + ENHANCE_METHODS.put("readValue", "org.apache.skywalking.apm.plugin.jackson.ReadValueInterceptor"); + ENHANCE_METHODS.put("readValues", "org.apache.skywalking.apm.plugin.jackson.ReadValueInterceptor"); + } + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + protected Map enhanceMethods() { + return ENHANCE_METHODS; + } +} diff --git a/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/define/ObjectWriterInstrumentation.java b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/define/ObjectWriterInstrumentation.java new file mode 100644 index 000000000..77d9d7d4c --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/jackson/define/ObjectWriterInstrumentation.java @@ -0,0 +1,55 @@ +/* + * 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.jackson.define; + +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import java.util.HashMap; +import java.util.Map; + +/** + * Jackson provides a "one stop" solution for json serialization and deserialization solution + * basic requirements. + *

+ * ObjectWriter: writeValue()\writeValueAsString()\writeValueAsBytes() + */ + +public class ObjectWriterInstrumentation extends AbstractInstrumentation { + + private static final String ENHANCE_CLASS = "com.fasterxml.jackson.databind.ObjectWriter"; + + private static final Map ENHANCE_METHODS = new HashMap<>(); + + static { + ENHANCE_METHODS.put("writeValue", "org.apache.skywalking.apm.plugin.jackson.BasicMethodsInterceptor"); + ENHANCE_METHODS.put("writeValueAsString", "org.apache.skywalking.apm.plugin.jackson.WriteValueAsStringInterceptor"); + ENHANCE_METHODS.put("writeValueAsBytes", "org.apache.skywalking.apm.plugin.jackson.WriteValueAsBytesInterceptor"); + } + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + protected Map enhanceMethods() { + return ENHANCE_METHODS; + } +} diff --git a/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/main/resources/skywalking-plugin.def new file mode 100644 index 000000000..0d47432b6 --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.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. + +jackson-2.x=org.apache.skywalking.apm.plugin.jackson.define.ObjectMapperInstrumentation +jackson-2.x=org.apache.skywalking.apm.plugin.jackson.define.ObjectReaderInstrumentation +jackson-2.x=org.apache.skywalking.apm.plugin.jackson.define.ObjectWriterInstrumentation \ No newline at end of file diff --git a/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jackson/BasicMethodsInterceptorTest.java b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jackson/BasicMethodsInterceptorTest.java new file mode 100644 index 000000000..03285ef68 --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jackson/BasicMethodsInterceptorTest.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.jackson; + +import com.fasterxml.jackson.databind.ObjectMapper; +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.io.File; +import java.lang.reflect.Method; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class BasicMethodsInterceptorTest { + + private BasicMethodsInterceptor basicMethodsInterceptor; + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + private Object[] arguments; + + private Class[] argumentType; + + @Before + public void setUp() { + basicMethodsInterceptor = new BasicMethodsInterceptor(); + arguments = new Object[0]; + } + + @Test + public void testSendMessage() throws Throwable { + + Method method = ObjectMapper.class.getMethod("writeValue", File.class, Object.class); + basicMethodsInterceptor.beforeMethod(null, method, arguments, null, null); + basicMethodsInterceptor.afterMethod(null, method, 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/jackson-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jackson/ReadValueInterceptorTest.java b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jackson/ReadValueInterceptorTest.java new file mode 100644 index 000000000..786ee613a --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jackson/ReadValueInterceptorTest.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.jackson; + +import com.fasterxml.jackson.databind.ObjectReader; +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.lang.reflect.Method; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class ReadValueInterceptorTest { + + private ReadValueInterceptor readValueInterceptor; + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + private Object[] arguments; + + private Class[] argumentType; + + @Before + public void setUp() { + readValueInterceptor = new ReadValueInterceptor(); + arguments = new Object[]{"{\"key\": 123}"}; + } + + @Test + public void testSendMessage() throws Throwable { + + Method method = ObjectReader.class.getMethod("readValue", String.class); + readValueInterceptor.beforeMethod(null, method, arguments, null, null); + readValueInterceptor.afterMethod(null, method, 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/jackson-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jackson/WriteValueAsBytesInterceptorTest.java b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jackson/WriteValueAsBytesInterceptorTest.java new file mode 100644 index 000000000..e3d8c1fb8 --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jackson/WriteValueAsBytesInterceptorTest.java @@ -0,0 +1,89 @@ +/* + * 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.jackson; + +import com.fasterxml.jackson.databind.ObjectWriter; +import lombok.Data; +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.lang.reflect.Method; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class WriteValueAsBytesInterceptorTest { + + private WriteValueAsBytesInterceptor writeValueAsBytesInterceptor; + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + private Object[] arguments; + + private Object result; + + private Class[] argumentType; + + @Data + private class TestBean { + private int key; + } + + @Before + public void setUp() { + writeValueAsBytesInterceptor = new WriteValueAsBytesInterceptor(); + arguments = new Object[]{new TestBean()}; + result = new byte[0]; + } + + @Test + public void testSendMessage() throws Throwable { + + Method method = ObjectWriter.class.getMethod("writeValueAsBytes", Object.class); + + writeValueAsBytesInterceptor.beforeMethod(null, method, arguments, null, null); + writeValueAsBytesInterceptor.afterMethod(null, method, arguments, null, result); + + 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/jackson-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jackson/WriteValueAsStringInterceptorTest.java b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jackson/WriteValueAsStringInterceptorTest.java new file mode 100644 index 000000000..3464f2e6b --- /dev/null +++ b/apm-sniffer/optional-plugins/jackson-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/jackson/WriteValueAsStringInterceptorTest.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.jackson; + +import com.fasterxml.jackson.databind.ObjectWriter; +import lombok.Data; +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.lang.reflect.Method; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class WriteValueAsStringInterceptorTest { + + private WriteValueAsStringInterceptor writeValueAsStringInterceptor; + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + private Object[] arguments; + + private Object result; + + private Class[] argumentType; + + @Data + private class TestBean { + private int key; + } + + @Before + public void setUp() { + writeValueAsStringInterceptor = new WriteValueAsStringInterceptor(); + arguments = new Object[]{new TestBean()}; + result = "{\"key\": 123}"; + } + + @Test + public void testSendMessage() throws Throwable { + + Method method = ObjectWriter.class.getMethod("writeValueAsString", Object.class); + writeValueAsStringInterceptor.beforeMethod(null, method, arguments, null, null); + writeValueAsStringInterceptor.afterMethod(null, method, arguments, null, result); + + 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/pom.xml b/apm-sniffer/optional-plugins/pom.xml index 8bef01907..896494d56 100644 --- a/apm-sniffer/optional-plugins/pom.xml +++ b/apm-sniffer/optional-plugins/pom.xml @@ -54,6 +54,7 @@ ehcache-2.x-plugin guava-cache-plugin fastjson-1.2.x-plugin + jackson-2.x-plugin diff --git a/docs/en/setup/service-agent/java-agent/Plugin-list.md b/docs/en/setup/service-agent/java-agent/Plugin-list.md index 44ad25aed..81a09cc7b 100644 --- a/docs/en/setup/service-agent/java-agent/Plugin-list.md +++ b/docs/en/setup/service-agent/java-agent/Plugin-list.md @@ -35,6 +35,7 @@ - httpclient-5.x - hystrix-1.x - influxdb-2.x +- jackson-2.x - jdk-http-plugin - jdk-threading-plugin - jedis-2.x diff --git a/docs/en/setup/service-agent/java-agent/README.md b/docs/en/setup/service-agent/java-agent/README.md index 8f60dcba2..1d40bcce8 100755 --- a/docs/en/setup/service-agent/java-agent/README.md +++ b/docs/en/setup/service-agent/java-agent/README.md @@ -88,6 +88,7 @@ Now, we have the following known optional plugins. * Plugin of ehcache-2.x in the optional plugin folder. The reason for being an optional plugin is, this plugin enhanced cache framework, generates large number of local spans, which have a potential performance impact. * Plugin of guava-cache in the optional plugin folder. The reason for being an optional plugin is, this plugin enhanced cache framework, generates large number of local spans, which have a potential performance impact. * Plugin of fastjson serialization lib in optional plugin folder. +* Plugin of jackson serialization lib in optional plugin folder. ## Bootstrap class plugins All bootstrap plugins are optional, due to unexpected risk. Bootstrap plugins are provided in `bootstrap-plugins` folder. 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 c7bf9f9d4..a35263f8d 100644 --- a/docs/en/setup/service-agent/java-agent/Supported-list.md +++ b/docs/en/setup/service-agent/java-agent/Supported-list.md @@ -107,6 +107,7 @@ metrics based on the tracing data. * JSON * [GSON](https://github.com/google/gson) 2.8.x (Optional²) * [Fastjson](https://github.com/alibaba/fastjson) 1.2.x (Optional²) + * [Jackson](https://github.com/FasterXML/jackson) 2.x (Optional²) * Vert.x Ecosystem * Vert.x Eventbus 3.2+ * Vert.x Web 3.x diff --git a/test/plugin/scenarios/jackson-scenario/bin/startup.sh b/test/plugin/scenarios/jackson-scenario/bin/startup.sh new file mode 100644 index 000000000..cee7ddb5b --- /dev/null +++ b/test/plugin/scenarios/jackson-scenario/bin/startup.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +home="$(cd "$(dirname $0)"; pwd)" + +java -jar ${agent_opts} ${home}/../libs/jackson-scenario.jar & \ No newline at end of file diff --git a/test/plugin/scenarios/jackson-scenario/config/expectedData.yaml b/test/plugin/scenarios/jackson-scenario/config/expectedData.yaml new file mode 100644 index 000000000..42e359c76 --- /dev/null +++ b/test/plugin/scenarios/jackson-scenario/config/expectedData.yaml @@ -0,0 +1,150 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +segmentItems: +- serviceName: jackson-scenario + segmentSize: ge 2 + segments: + - segmentId: nq 0 + spans: + - operationName: Jackson/ObjectMapper.writeValue + parentSpanId: 0 + spanId: 1 + spanLayer: Unknown + startTime: nq 0 + endTime: nq 0 + componentId: 118 + isError: false + spanType: Local + peer: '' + - operationName: Jackson/ObjectMapper.writeValueAsString + parentSpanId: 0 + spanId: 2 + spanLayer: Unknown + startTime: nq 0 + endTime: nq 0 + componentId: 118 + isError: false + spanType: Local + peer: '' + tags: + - {key: length, value: '20'} + - operationName: Jackson/ObjectMapper.writeValueAsBytes + parentSpanId: 0 + spanId: 3 + spanLayer: Unknown + startTime: nq 0 + endTime: nq 0 + componentId: 118 + isError: false + spanType: Local + peer: '' + tags: + - {key: length, value: '20'} + - operationName: Jackson/ObjectMapper.readValue + parentSpanId: 0 + spanId: 4 + spanLayer: Unknown + startTime: nq 0 + endTime: nq 0 + componentId: 118 + isError: false + spanType: Local + peer: '' + tags: + - {key: length, value: '26'} + - operationName: Jackson/ObjectReader.readValue + parentSpanId: 0 + spanId: 5 + spanLayer: Unknown + startTime: nq 0 + endTime: nq 0 + componentId: 118 + isError: false + spanType: Local + peer: '' + tags: + - {key: length, value: '26'} + - operationName: Jackson/ObjectReader.readValue + parentSpanId: 0 + spanId: 6 + spanLayer: Unknown + startTime: nq 0 + endTime: nq 0 + componentId: 118 + isError: false + spanType: Local + peer: '' + tags: + - {key: length, value: '26'} + - operationName: Jackson/ObjectReader.readValues + parentSpanId: 0 + spanId: 7 + spanLayer: Unknown + startTime: nq 0 + endTime: nq 0 + componentId: 118 + isError: false + spanType: Local + peer: '' + tags: + - {key: length, value: '26'} + - operationName: Jackson/ObjectWriter.writeValue + parentSpanId: 0 + spanId: 8 + spanLayer: Unknown + startTime: nq 0 + endTime: nq 0 + componentId: 118 + isError: false + spanType: Local + peer: '' + - operationName: Jackson/ObjectWriter.writeValueAsString + parentSpanId: 0 + spanId: 9 + spanLayer: Unknown + startTime: nq 0 + endTime: nq 0 + componentId: 118 + isError: false + spanType: Local + peer: '' + tags: + - {key: length, value: '20'} + - operationName: Jackson/ObjectWriter.writeValueAsBytes + parentSpanId: 0 + spanId: 10 + spanLayer: Unknown + startTime: nq 0 + endTime: nq 0 + componentId: 118 + isError: false + spanType: Local + peer: '' + tags: + - {key: length, value: '20'} + - operationName: GET:/jackson-scenario/case/jackson-scenario + parentSpanId: -1 + spanId: 0 + spanLayer: Http + startTime: nq 0 + endTime: nq 0 + componentId: 1 + isError: false + spanType: Entry + peer: '' + tags: + - {key: url, value: 'http://localhost:8080/jackson-scenario/case/jackson-scenario'} + - {key: http.method, value: GET} diff --git a/test/plugin/scenarios/jackson-scenario/configuration.yml b/test/plugin/scenarios/jackson-scenario/configuration.yml new file mode 100644 index 000000000..5a29caaf8 --- /dev/null +++ b/test/plugin/scenarios/jackson-scenario/configuration.yml @@ -0,0 +1,24 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +type: jvm +entryService: http://localhost:8080/jackson-scenario/case/jackson-scenario +healthCheck: http://localhost:8080/jackson-scenario/case/healthCheck +startScript: ./bin/startup.sh +runningMode: with_optional +withPlugins: apm-jackson-2.x-plugin-*.jar +environment: +dependencies: diff --git a/test/plugin/scenarios/jackson-scenario/pom.xml b/test/plugin/scenarios/jackson-scenario/pom.xml new file mode 100644 index 000000000..3baf3563a --- /dev/null +++ b/test/plugin/scenarios/jackson-scenario/pom.xml @@ -0,0 +1,130 @@ + + + + + org.apache.skywalking.apm.testcase + jackson-scenario + 1.0.0 + jar + + 4.0.0 + + + UTF-8 + 2.1.6.RELEASE + 2.9.9 + ${test.framework.version} + 2.6.2 + 4.3.8.RELEASE + + + skywalking-jackson-scenario + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot-version} + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + + com.fasterxml.jackson.core + jackson-core + ${test.framework.version} + + + com.fasterxml.jackson.core + jackson-annotations + ${test.framework.version} + + + com.fasterxml.jackson.core + jackson-databind + ${test.framework.version} + + + + + + jackson-scenario + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + maven-compiler-plugin + + ${compiler.version} + ${compiler.version} + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-assembly-plugin + + + assemble + package + + single + + + + src/main/assembly/assembly.xml + + ./target/ + + + + + + + \ No newline at end of file diff --git a/test/plugin/scenarios/jackson-scenario/src/main/assembly/assembly.xml b/test/plugin/scenarios/jackson-scenario/src/main/assembly/assembly.xml new file mode 100644 index 000000000..422836340 --- /dev/null +++ b/test/plugin/scenarios/jackson-scenario/src/main/assembly/assembly.xml @@ -0,0 +1,41 @@ + + + + + zip + + + + + ./bin + 0775 + + + + + + ${project.build.directory}/jackson-scenario.jar + ./libs + 0775 + + + diff --git a/test/plugin/scenarios/jackson-scenario/src/main/java/org/apache/skywalking/apm/testcase/jackson/Application.java b/test/plugin/scenarios/jackson-scenario/src/main/java/org/apache/skywalking/apm/testcase/jackson/Application.java new file mode 100644 index 000000000..027431da3 --- /dev/null +++ b/test/plugin/scenarios/jackson-scenario/src/main/java/org/apache/skywalking/apm/testcase/jackson/Application.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.apm.testcase.jackson; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + try { + SpringApplication.run(Application.class, args); + } catch (Exception e) { + // Never do this + } + } +} diff --git a/test/plugin/scenarios/jackson-scenario/src/main/java/org/apache/skywalking/apm/testcase/jackson/controller/CaseController.java b/test/plugin/scenarios/jackson-scenario/src/main/java/org/apache/skywalking/apm/testcase/jackson/controller/CaseController.java new file mode 100644 index 000000000..423d2ce64 --- /dev/null +++ b/test/plugin/scenarios/jackson-scenario/src/main/java/org/apache/skywalking/apm/testcase/jackson/controller/CaseController.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.testcase.jackson.controller; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.skywalking.apm.testcase.jackson.service.CaseService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; + +@RestController +@RequestMapping("/case") +public class CaseController { + + @Autowired + CaseService caseService; + + private static final Logger LOGGER = LogManager.getLogger(CaseController.class); + + private static final String SUCCESS = "Success"; + + @RequestMapping("/jackson-scenario") + @ResponseBody + public String testcase() throws IOException { + try { + caseService.mapperCase(); + caseService.readerCase(); + caseService.writerCase(); + } catch (IOException e) { + LOGGER.error("Failed to parse json.", e); + throw e; + } + return SUCCESS; + } + + @RequestMapping("/healthCheck") + @ResponseBody + public String healthCheck() { + return SUCCESS; + } + +} diff --git a/test/plugin/scenarios/jackson-scenario/src/main/java/org/apache/skywalking/apm/testcase/jackson/entity/CaseEntity.java b/test/plugin/scenarios/jackson-scenario/src/main/java/org/apache/skywalking/apm/testcase/jackson/entity/CaseEntity.java new file mode 100644 index 000000000..fb13e4aee --- /dev/null +++ b/test/plugin/scenarios/jackson-scenario/src/main/java/org/apache/skywalking/apm/testcase/jackson/entity/CaseEntity.java @@ -0,0 +1,41 @@ +/* + * 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.jackson.entity; + +public class CaseEntity { + + private int key; + private String msg; + + public int getKey() { + return key; + } + + public void setKey(int key) { + this.key = key; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } +} diff --git a/test/plugin/scenarios/jackson-scenario/src/main/java/org/apache/skywalking/apm/testcase/jackson/service/CaseService.java b/test/plugin/scenarios/jackson-scenario/src/main/java/org/apache/skywalking/apm/testcase/jackson/service/CaseService.java new file mode 100644 index 000000000..c454533d3 --- /dev/null +++ b/test/plugin/scenarios/jackson-scenario/src/main/java/org/apache/skywalking/apm/testcase/jackson/service/CaseService.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.testcase.jackson.service; + +import com.fasterxml.jackson.databind.MappingIterator; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.databind.ObjectWriter; +import org.apache.skywalking.apm.testcase.jackson.entity.CaseEntity; +import org.springframework.stereotype.Service; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +@Service +public class CaseService { + + public void mapperCase() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + CaseEntity entity = new CaseEntity(); + mapper.writeValue(out, entity); + + Object jsonStr = mapper.writeValueAsString(entity); + assert jsonStr instanceof String; + Object jsonByte = mapper.writeValueAsBytes(entity); + assert jsonByte instanceof byte[]; + + String jsonString = "{\"key\":123, \"msg\":\"test\" }"; + Object jsonObj = mapper.readValue(jsonString, CaseEntity.class); + assert jsonObj instanceof CaseEntity; + } + + public void readerCase() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + ObjectReader reader = mapper.readerFor(CaseEntity.class); + + String jsonString = "{\"key\":123, \"msg\":\"test\" }"; + byte[] jsonBytes = jsonString.getBytes(); + + Object jsonObj1 = reader.readValue(jsonString); + assert jsonObj1 instanceof CaseEntity; + Object jsonObj2 = reader.readValue(jsonBytes); + assert jsonObj2 instanceof CaseEntity; + Object jsonObj3 = reader.readValues(jsonString); + assert jsonObj3 instanceof MappingIterator; + } + + public void writerCase() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + ObjectWriter writer = mapper.writerFor(CaseEntity.class); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + CaseEntity entity = new CaseEntity(); + writer.writeValue(out, entity); + + Object jsonStr = writer.writeValueAsString(entity); + assert jsonStr instanceof String; + Object jsonByte = writer.writeValueAsBytes(entity); + assert jsonByte instanceof byte[]; + } +} diff --git a/test/plugin/scenarios/jackson-scenario/src/main/resources/application.yaml b/test/plugin/scenarios/jackson-scenario/src/main/resources/application.yaml new file mode 100644 index 000000000..60ac33452 --- /dev/null +++ b/test/plugin/scenarios/jackson-scenario/src/main/resources/application.yaml @@ -0,0 +1,23 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# +server: + port: 8080 + servlet: + context-path: /jackson-scenario +logging: + config: classpath:log4j2.xml \ No newline at end of file diff --git a/test/plugin/scenarios/jackson-scenario/src/main/resources/log4j2.xml b/test/plugin/scenarios/jackson-scenario/src/main/resources/log4j2.xml new file mode 100644 index 000000000..9849ed5a8 --- /dev/null +++ b/test/plugin/scenarios/jackson-scenario/src/main/resources/log4j2.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/plugin/scenarios/jackson-scenario/support-version.list b/test/plugin/scenarios/jackson-scenario/support-version.list new file mode 100644 index 000000000..8ab826a36 --- /dev/null +++ b/test/plugin/scenarios/jackson-scenario/support-version.list @@ -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. + +2.9.9