From b934a143c2a08dd34bba12039f0380776fec9902 Mon Sep 17 00:00:00 2001 From: liqiangz Date: Thu, 6 May 2021 18:54:39 +0800 Subject: [PATCH] Add `okhttp-4.x` plugin (#6899) --- CHANGES.md | 1 + .../apm-sdk-plugin/okhttp-3.x-plugin/pom.xml | 6 + .../define/AbstractOkhttpInstrumentation.java | 29 +++++ .../v3/define/AsyncCallInstrumentation.java | 7 +- .../v3/define/CallbackInstrumentation.java | 7 +- .../v3/define/RealCallInstrumentation.java | 7 +- .../okhttp/v3/RealCallInterceptorTest.java | 1 + .../apm-sdk-plugin/okhttp-4.x-plugin/pom.xml | 52 +++++++++ .../define/AbstractOkhttpInstrumentation.java | 29 +++++ .../v4/define/AsyncCallInstrumentation.java | 76 +++++++++++++ .../v4/define/CallbackInstrumentation.java | 78 +++++++++++++ .../v4/define/RealCallInstrumentation.java | 103 ++++++++++++++++++ .../src/main/resources/skywalking-plugin.def | 19 ++++ .../apm-sdk-plugin/okhttp-common/pom.xml | 56 ++++++++++ .../okhttp/common}/AsyncCallInterceptor.java | 7 +- .../okhttp/common}/EnhanceRequiredInfo.java | 2 +- .../okhttp/common}/EnqueueInterceptor.java | 5 +- .../okhttp/common}/OnFailureInterceptor.java | 5 +- .../okhttp/common}/OnResponseInterceptor.java | 5 +- .../okhttp/common}/RealCallInterceptor.java | 13 ++- apm-sniffer/apm-sdk-plugin/pom.xml | 2 + .../service-agent/java-agent/Plugin-list.md | 1 + .../java-agent/Supported-list.md | 2 +- .../okhttp-scenario/support-version.list | 8 +- 24 files changed, 491 insertions(+), 30 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/AbstractOkhttpInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/pom.xml create mode 100644 apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/AbstractOkhttpInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/AsyncCallInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/CallbackInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/RealCallInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/resources/skywalking-plugin.def create mode 100644 apm-sniffer/apm-sdk-plugin/okhttp-common/pom.xml rename apm-sniffer/apm-sdk-plugin/{okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3 => okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common}/AsyncCallInterceptor.java (98%) rename apm-sniffer/apm-sdk-plugin/{okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3 => okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common}/EnhanceRequiredInfo.java (96%) rename apm-sniffer/apm-sdk-plugin/{okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3 => okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common}/EnqueueInterceptor.java (98%) rename apm-sniffer/apm-sdk-plugin/{okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3 => okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common}/OnFailureInterceptor.java (97%) rename apm-sniffer/apm-sdk-plugin/{okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3 => okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common}/OnResponseInterceptor.java (97%) rename apm-sniffer/apm-sdk-plugin/{okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3 => okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common}/RealCallInterceptor.java (93%) diff --git a/CHANGES.md b/CHANGES.md index d76142aa23..c6bc4d69bd 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -22,6 +22,7 @@ Release Notes. * fix a bug that spring-mvc set an error endpoint name if the controller class annotation implements an interface. * Add an optional agent plugin to support mybatis. * Add `spring-cloud-gateway-3.x` optional plugin. +* Add `okhttp-4.x` plugin. #### OAP-Backend * BugFix: filter invalid Envoy access logs whose socket address is empty. diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/pom.xml index ee59cc3d60..784e29f781 100644 --- a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/pom.xml @@ -40,5 +40,11 @@ ${okhttp.version} provided + + org.apache.skywalking + apm-okhttp-common + ${project.version} + provided + diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/AbstractOkhttpInstrumentation.java b/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/AbstractOkhttpInstrumentation.java new file mode 100644 index 0000000000..d3df0ac5f8 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/AbstractOkhttpInstrumentation.java @@ -0,0 +1,29 @@ +/* + * 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.okhttp.v3.define; + +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; + +public abstract class AbstractOkhttpInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + public static final String WITHNESS_CLASSES = "okhttp3.RealCall"; + + @Override + protected final String[] witnessClasses() { + return new String[] {WITHNESS_CLASSES}; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/AsyncCallInstrumentation.java b/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/AsyncCallInstrumentation.java index 0bcf20ece9..9c9303ad88 100644 --- a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/AsyncCallInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/AsyncCallInstrumentation.java @@ -22,14 +22,13 @@ import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; -import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.any; import static net.bytebuddy.matcher.ElementMatchers.named; import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; -public class AsyncCallInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { +public class AsyncCallInstrumentation extends AbstractOkhttpInstrumentation { @Override public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { @@ -42,7 +41,7 @@ public class AsyncCallInstrumentation extends ClassInstanceMethodsEnhancePluginD @Override public String getConstructorInterceptor() { - return "org.apache.skywalking.apm.plugin.okhttp.v3.AsyncCallInterceptor"; + return "org.apache.skywalking.apm.plugin.okhttp.common.AsyncCallInterceptor"; } } }; @@ -59,7 +58,7 @@ public class AsyncCallInstrumentation extends ClassInstanceMethodsEnhancePluginD @Override public String getMethodsInterceptor() { - return "org.apache.skywalking.apm.plugin.okhttp.v3.AsyncCallInterceptor"; + return "org.apache.skywalking.apm.plugin.okhttp.common.AsyncCallInterceptor"; } @Override diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/CallbackInstrumentation.java b/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/CallbackInstrumentation.java index a245548967..ec75293e82 100644 --- a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/CallbackInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/CallbackInstrumentation.java @@ -22,13 +22,12 @@ import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; -import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import static net.bytebuddy.matcher.ElementMatchers.named; import static org.apache.skywalking.apm.agent.core.plugin.match.HierarchyMatch.byHierarchyMatch; -public class CallbackInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { +public class CallbackInstrumentation extends AbstractOkhttpInstrumentation { @Override public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { return new ConstructorInterceptPoint[0]; @@ -45,7 +44,7 @@ public class CallbackInstrumentation extends ClassInstanceMethodsEnhancePluginDe @Override public String getMethodsInterceptor() { - return "org.apache.skywalking.apm.plugin.okhttp.v3.OnFailureInterceptor"; + return "org.apache.skywalking.apm.plugin.okhttp.common.OnFailureInterceptor"; } @Override @@ -61,7 +60,7 @@ public class CallbackInstrumentation extends ClassInstanceMethodsEnhancePluginDe @Override public String getMethodsInterceptor() { - return "org.apache.skywalking.apm.plugin.okhttp.v3.OnResponseInterceptor"; + return "org.apache.skywalking.apm.plugin.okhttp.common.OnResponseInterceptor"; } @Override diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/RealCallInstrumentation.java b/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/RealCallInstrumentation.java index 7644d9b4c1..0658359b3d 100644 --- a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/RealCallInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/RealCallInstrumentation.java @@ -22,7 +22,6 @@ import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; -import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; @@ -30,7 +29,7 @@ import static net.bytebuddy.matcher.ElementMatchers.any; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArguments; -public class RealCallInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { +public class RealCallInstrumentation extends AbstractOkhttpInstrumentation { /** * Enhance class. @@ -40,7 +39,7 @@ public class RealCallInstrumentation extends ClassInstanceMethodsEnhancePluginDe /** * Intercept class. */ - private static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.okhttp.v3.RealCallInterceptor"; + private static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.okhttp.common.RealCallInterceptor"; @Override protected ClassMatch enhanceClass() { @@ -91,7 +90,7 @@ public class RealCallInstrumentation extends ClassInstanceMethodsEnhancePluginDe @Override public String getMethodsInterceptor() { - return "org.apache.skywalking.apm.plugin.okhttp.v3.EnqueueInterceptor"; + return "org.apache.skywalking.apm.plugin.okhttp.common.EnqueueInterceptor"; } @Override diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/okhttp/v3/RealCallInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/okhttp/v3/RealCallInterceptorTest.java index cc6caaeb48..c7b4dc3c98 100644 --- a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/okhttp/v3/RealCallInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/okhttp/v3/RealCallInterceptorTest.java @@ -25,6 +25,7 @@ import okhttp3.Response; 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.SpanAssert; +import org.apache.skywalking.apm.plugin.okhttp.common.RealCallInterceptor; import org.junit.Before; import org.junit.Rule; import org.junit.Test; diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/pom.xml new file mode 100644 index 0000000000..8a39e18f0e --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/pom.xml @@ -0,0 +1,52 @@ + + + + + + org.apache.skywalking + apm-sdk-plugin + 8.6.0-SNAPSHOT + + 4.0.0 + + apm-okhttp-4.x-plugin + okhttp-4.x-plugin + jar + + + 4.9.0 + + + + + com.squareup.okhttp3 + okhttp + ${okhttp.version} + provided + + + org.apache.skywalking + apm-okhttp-common + ${project.version} + provided + + + \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/AbstractOkhttpInstrumentation.java b/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/AbstractOkhttpInstrumentation.java new file mode 100644 index 0000000000..2796d786be --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/AbstractOkhttpInstrumentation.java @@ -0,0 +1,29 @@ +/* + * 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.okhttp.v4.define; + +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; + +public abstract class AbstractOkhttpInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + public static final String WITHNESS_CLASSES = "okhttp3.internal.connection.RealCall"; + + @Override + protected final String[] witnessClasses() { + return new String[] {WITHNESS_CLASSES}; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/AsyncCallInstrumentation.java b/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/AsyncCallInstrumentation.java new file mode 100644 index 0000000000..25ac4c3c8d --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/AsyncCallInstrumentation.java @@ -0,0 +1,76 @@ +/* + * 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.okhttp.v4.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.match.ClassMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +public class AsyncCallInstrumentation extends AbstractOkhttpInstrumentation { + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return "org.apache.skywalking.apm.plugin.okhttp.common.AsyncCallInterceptor"; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("run"); + } + + @Override + public String getMethodsInterceptor() { + return "org.apache.skywalking.apm.plugin.okhttp.common.AsyncCallInterceptor"; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + protected ClassMatch enhanceClass() { + return byName("okhttp3.internal.connection.RealCall$AsyncCall"); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/CallbackInstrumentation.java b/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/CallbackInstrumentation.java new file mode 100644 index 0000000000..0dca638b80 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/CallbackInstrumentation.java @@ -0,0 +1,78 @@ +/* + * 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.okhttp.v4.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.match.ClassMatch; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static org.apache.skywalking.apm.agent.core.plugin.match.HierarchyMatch.byHierarchyMatch; + +public class CallbackInstrumentation extends AbstractOkhttpInstrumentation { + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("onFailure"); + } + + @Override + public String getMethodsInterceptor() { + return "org.apache.skywalking.apm.plugin.okhttp.common.OnFailureInterceptor"; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("onResponse"); + } + + @Override + public String getMethodsInterceptor() { + return "org.apache.skywalking.apm.plugin.okhttp.common.OnResponseInterceptor"; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + protected ClassMatch enhanceClass() { + return byHierarchyMatch(new String[] {"okhttp3.Callback"}); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/RealCallInstrumentation.java b/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/RealCallInstrumentation.java new file mode 100644 index 0000000000..ea97358b18 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v4/define/RealCallInstrumentation.java @@ -0,0 +1,103 @@ +/* + * 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.okhttp.v4.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.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; + +import static net.bytebuddy.matcher.ElementMatchers.any; +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; + +public class RealCallInstrumentation extends AbstractOkhttpInstrumentation { + + /** + * Enhance class. + */ + private static final String ENHANCE_CLASS = "okhttp3.internal.connection.RealCall"; + + /** + * Intercept class. + */ + private static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.okhttp.common.RealCallInterceptor"; + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[] { + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return any(); + } + + @Override + public String getConstructorInterceptor() { + return INTERCEPT_CLASS; + } + } + }; + } + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("execute"); + } + + @Override + public String getMethodsInterceptor() { + return INTERCEPT_CLASS; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + }, + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("enqueue").and(takesArguments(1)); + } + + @Override + public String getMethodsInterceptor() { + return "org.apache.skywalking.apm.plugin.okhttp.common.EnqueueInterceptor"; + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/okhttp-4.x-plugin/src/main/resources/skywalking-plugin.def new file mode 100644 index 0000000000..2da0decf3f --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/okhttp-4.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. + +okhttp-4.x=org.apache.skywalking.apm.plugin.okhttp.v4.define.RealCallInstrumentation +okhttp-4.x=org.apache.skywalking.apm.plugin.okhttp.v4.define.CallbackInstrumentation +okhttp-4.x=org.apache.skywalking.apm.plugin.okhttp.v4.define.AsyncCallInstrumentation diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-common/pom.xml b/apm-sniffer/apm-sdk-plugin/okhttp-common/pom.xml new file mode 100644 index 0000000000..638b21081b --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/okhttp-common/pom.xml @@ -0,0 +1,56 @@ + + + + + + apm-sdk-plugin + org.apache.skywalking + 8.6.0-SNAPSHOT + + 4.0.0 + + apm-okhttp-common + jar + + okhttp-common + http://maven.apache.org + + + 3.7.0 + + + + + com.squareup.okhttp3 + okhttp + ${okhttp.version} + provided + + + + + + + maven-deploy-plugin + + + + \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/AsyncCallInterceptor.java b/apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/AsyncCallInterceptor.java similarity index 98% rename from apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/AsyncCallInterceptor.java rename to apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/AsyncCallInterceptor.java index 452b5ba463..75656920c2 100644 --- a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/AsyncCallInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/AsyncCallInterceptor.java @@ -16,10 +16,8 @@ * */ -package org.apache.skywalking.apm.plugin.okhttp.v3; +package org.apache.skywalking.apm.plugin.okhttp.common; -import java.lang.reflect.Field; -import java.lang.reflect.Method; import okhttp3.Headers; import okhttp3.HttpUrl; import okhttp3.Request; @@ -35,6 +33,9 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceM import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + /** * {@link AsyncCallInterceptor} get the `EnhanceRequiredInfo` instance from `SkyWalkingDynamicField` and then put it * into `AsyncCall` instance when the `AsyncCall` constructor called. diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/EnhanceRequiredInfo.java b/apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/EnhanceRequiredInfo.java similarity index 96% rename from apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/EnhanceRequiredInfo.java rename to apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/EnhanceRequiredInfo.java index 25046cd486..311cf1cc1e 100644 --- a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/EnhanceRequiredInfo.java +++ b/apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/EnhanceRequiredInfo.java @@ -16,7 +16,7 @@ * */ -package org.apache.skywalking.apm.plugin.okhttp.v3; +package org.apache.skywalking.apm.plugin.okhttp.common; import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/EnqueueInterceptor.java b/apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/EnqueueInterceptor.java similarity index 98% rename from apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/EnqueueInterceptor.java rename to apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/EnqueueInterceptor.java index e4612a97dc..f6900a4cf5 100644 --- a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/EnqueueInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/EnqueueInterceptor.java @@ -16,9 +16,8 @@ * */ -package org.apache.skywalking.apm.plugin.okhttp.v3; +package org.apache.skywalking.apm.plugin.okhttp.common; -import java.lang.reflect.Method; import okhttp3.Request; import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; @@ -26,6 +25,8 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceC import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import java.lang.reflect.Method; + /** * {@link EnqueueInterceptor} create a local span and the prefix of the span operation name is start with `Async` when * the `enqueue` method called and also put the `ContextSnapshot` and `RealCall` instance into the diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/OnFailureInterceptor.java b/apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/OnFailureInterceptor.java similarity index 97% rename from apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/OnFailureInterceptor.java rename to apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/OnFailureInterceptor.java index be4b82df19..8b38b4c8db 100644 --- a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/OnFailureInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/OnFailureInterceptor.java @@ -16,14 +16,15 @@ * */ -package org.apache.skywalking.apm.plugin.okhttp.v3; +package org.apache.skywalking.apm.plugin.okhttp.common; -import java.lang.reflect.Method; import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import java.lang.reflect.Method; + public class OnFailureInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/OnResponseInterceptor.java b/apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/OnResponseInterceptor.java similarity index 97% rename from apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/OnResponseInterceptor.java rename to apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/OnResponseInterceptor.java index 4649d016b8..62728b3647 100644 --- a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/OnResponseInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/OnResponseInterceptor.java @@ -16,15 +16,16 @@ * */ -package org.apache.skywalking.apm.plugin.okhttp.v3; +package org.apache.skywalking.apm.plugin.okhttp.common; -import java.lang.reflect.Method; import okhttp3.Response; import org.apache.skywalking.apm.agent.core.context.ContextManager; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import java.lang.reflect.Method; + /** * {@link OnResponseInterceptor} validate the response code if it is great equal than 400. if so. the transaction status * chang to `error`, or do nothing. diff --git a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/RealCallInterceptor.java b/apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/RealCallInterceptor.java similarity index 93% rename from apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/RealCallInterceptor.java rename to apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/RealCallInterceptor.java index b88f846233..af3a169d96 100644 --- a/apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/RealCallInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/okhttp-common/src/main/java/org/apache/skywalking/apm/plugin/okhttp/common/RealCallInterceptor.java @@ -16,10 +16,8 @@ * */ -package org.apache.skywalking.apm.plugin.okhttp.v3; +package org.apache.skywalking.apm.plugin.okhttp.common; -import java.lang.reflect.Field; -import java.lang.reflect.Method; import okhttp3.Headers; import okhttp3.HttpUrl; import okhttp3.Request; @@ -36,6 +34,9 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceM import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; +import java.lang.reflect.Field; +import java.lang.reflect.Method; + /** * {@link RealCallInterceptor} intercept the synchronous http calls by the discovery of okhttp. */ @@ -47,9 +48,9 @@ public class RealCallInterceptor implements InstanceMethodsAroundInterceptor, In } /** - * Get the {@link okhttp3.Request} from {@link EnhancedInstance}, then create {@link AbstractSpan} and set host, - * port, kind, component, url from {@link okhttp3.Request}. Through the reflection of the way, set the http header - * of context data into {@link okhttp3.Request#headers}. + * Get the {@link Request} from {@link EnhancedInstance}, then create {@link AbstractSpan} and set host, + * port, kind, component, url from {@link Request}. Through the reflection of the way, set the http header + * of context data into {@link Request#headers}. * * @param result change this result, if you want to truncate the method. */ diff --git a/apm-sniffer/apm-sdk-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/pom.xml index 57464edad0..36fbf91a65 100644 --- a/apm-sniffer/apm-sdk-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/pom.xml @@ -39,6 +39,8 @@ mongodb-4.x-plugin feign-default-http-9.x-plugin okhttp-3.x-plugin + okhttp-4.x-plugin + okhttp-common spring-plugins struts2-2.x-plugin nutz-plugins 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 a29d325572..43618d1b65 100644 --- a/docs/en/setup/service-agent/java-agent/Plugin-list.md +++ b/docs/en/setup/service-agent/java-agent/Plugin-list.md @@ -54,6 +54,7 @@ - nutz-http-1.x - nutz-mvc-annotation-1.x - okhttp-3.x +- okhttp-4.x - play-2.x - postgresql-8.x - pulsar 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 e5a9501f2b..625a5662b2 100644 --- a/docs/en/setup/service-agent/java-agent/Supported-list.md +++ b/docs/en/setup/service-agent/java-agent/Supported-list.md @@ -22,7 +22,7 @@ metrics based on the tracing data. * HTTP Client * [Feign](https://github.com/OpenFeign/feign) 9.x * [Netflix Spring Cloud Feign](https://github.com/spring-cloud/spring-cloud-openfeign) 1.1.x -> 2.x - * [Okhttp](https://github.com/square/okhttp) 3.x + * [Okhttp](https://github.com/square/okhttp) 3.x -> 4.x * [Apache httpcomponent HttpClient](http://hc.apache.org/) 2.0 -> 3.1, 4.2, 4.3 * [Spring RestTemplete](https://github.com/spring-projects/spring-framework) 4.x * [Jetty Client](http://www.eclipse.org/jetty/) 9 diff --git a/test/plugin/scenarios/okhttp-scenario/support-version.list b/test/plugin/scenarios/okhttp-scenario/support-version.list index cd7736b31c..061f66549a 100644 --- a/test/plugin/scenarios/okhttp-scenario/support-version.list +++ b/test/plugin/scenarios/okhttp-scenario/support-version.list @@ -28,4 +28,10 @@ 3.11.0 3.12.6 3.13.1 -3.14.4 \ No newline at end of file +3.14.4 +4.5.0 +4.6.0 +4.7.0 +4.8.0 +4.9.0 +4.9.1 \ No newline at end of file -- GitLab