You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2023/02/09 03:28:29 UTC
[skywalking-java] branch main updated: Enhance kotlin coroutine plugin for stack tracing (#451)
This is an automated email from the ASF dual-hosted git repository.
wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git
The following commit(s) were added to refs/heads/main by this push:
new ad1389347a Enhance kotlin coroutine plugin for stack tracing (#451)
ad1389347a is described below
commit ad1389347a714a6761efd231ea0f15b2a88e92b4
Author: Kanro <hi...@live.cn>
AuthorDate: Thu Feb 9 11:28:23 2023 +0800
Enhance kotlin coroutine plugin for stack tracing (#451)
---
CHANGES.md | 1 +
...ava => DispatchedTaskExceptionInterceptor.java} | 33 ++++++---
.../coroutine/DispatchedTaskRunInterceptor.java | 73 ++++++++++++++++++
.../kotlin/coroutine/DispatcherInterceptor.java | 27 +++++--
.../plugin/kotlin/coroutine/TracingRunnable.java | 7 +-
.../apm/plugin/kotlin/coroutine/Utils.java | 65 ++++++++++++++++
.../define/DispatchedTaskInstrumentation.java | 86 ++++++++++++++++++++++
.../src/main/resources/skywalking-plugin.def | 3 +-
.../config/expectedData.yaml | 2 +-
9 files changed, 276 insertions(+), 21 deletions(-)
diff --git a/CHANGES.md b/CHANGES.md
index 0253fb8823..cb56e7f0fd 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -12,6 +12,7 @@ Release Notes.
* Remove Powermock entirely from the test cases.
* Fix H2 instrumentation point
* Refactor pipeline in jedis-plugin.
+* Enhance kotlin coroutine plugin for stack tracing.
#### Documentation
* Update docs of Tracing APIs, reorganize the API docs into six parts.
diff --git a/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatcherInterceptor.java b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatchedTaskExceptionInterceptor.java
similarity index 53%
copy from apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatcherInterceptor.java
copy to apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatchedTaskExceptionInterceptor.java
index 2f9348e25c..54395236c7 100644
--- a/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatcherInterceptor.java
+++ b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatchedTaskExceptionInterceptor.java
@@ -18,28 +18,43 @@
package org.apache.skywalking.apm.plugin.kotlin.coroutine;
+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 java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+public class DispatchedTaskExceptionInterceptor implements InstanceMethodsAroundInterceptor {
-public class DispatcherInterceptor implements InstanceMethodsAroundInterceptor {
@Override
- public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
- MethodInterceptResult result) {
- // Wrapping runnable with current context snapshot
- allArguments[1] = TracingRunnable.wrapOrNot((Runnable) allArguments[1]);
+ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) {
}
@Override
- public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
- Object ret) {
+ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) {
+ if (!(ret instanceof Throwable)) return ret;
+ Throwable exception = (Throwable) ret;
+
+ if (ContextManager.isActive() && objInst.getSkyWalkingDynamicField() instanceof AbstractSpan) {
+ AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
+ String[] elements = Utils.getCoroutineStackTraceElements(objInst);
+ if (elements.length > 0) {
+ Map<String, String> eventMap = new HashMap<>();
+ eventMap.put("coroutine.stack", String.join("\n", elements));
+ span.log(System.currentTimeMillis(), eventMap);
+ }
+
+ objInst.setSkyWalkingDynamicField(exception);
+ span.errorOccurred().log(exception);
+ }
return ret;
}
@Override
- public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
- Class<?>[] argumentsTypes, Throwable t) {
+ public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}
diff --git a/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatchedTaskRunInterceptor.java b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatchedTaskRunInterceptor.java
new file mode 100644
index 0000000000..83c4d312c8
--- /dev/null
+++ b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatchedTaskRunInterceptor.java
@@ -0,0 +1,73 @@
+/*
+ * 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.kotlin.coroutine;
+
+import org.apache.skywalking.apm.agent.core.context.ContextManager;
+import org.apache.skywalking.apm.agent.core.context.ContextSnapshot;
+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 DispatchedTaskRunInterceptor implements InstanceMethodsAroundInterceptor {
+
+ @Override
+ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) {
+ if (objInst.getSkyWalkingDynamicField() instanceof ContextSnapshot) {
+ ContextSnapshot snapshot = (ContextSnapshot) objInst.getSkyWalkingDynamicField();
+
+ if (ContextManager.isActive() && snapshot.isFromCurrent()) {
+ // Thread not switched, skip restore snapshot.
+ return;
+ }
+
+ // Create local coroutine span
+ AbstractSpan span = ContextManager.createLocalSpan(TracingRunnable.COROUTINE);
+ span.setComponent(ComponentsDefine.KT_COROUTINE);
+ objInst.setSkyWalkingDynamicField(span);
+
+ // Recover with snapshot
+ ContextManager.continued(snapshot);
+ }
+ }
+
+ @Override
+ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) {
+ if (ContextManager.isActive() && objInst.getSkyWalkingDynamicField() instanceof AbstractSpan) {
+ AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
+ if (span != null) {
+ ContextManager.stopSpan(span);
+ }
+ }
+ return ret;
+ }
+
+ @Override
+ public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
+ if (ContextManager.isActive() && objInst.getSkyWalkingDynamicField() instanceof AbstractSpan) {
+ AbstractSpan span = (AbstractSpan) objInst.getSkyWalkingDynamicField();
+ if (span != null) {
+ ContextManager.stopSpan(span.errorOccurred().log(t));
+ }
+ }
+ }
+}
diff --git a/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatcherInterceptor.java b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatcherInterceptor.java
index 2f9348e25c..cc31f47e89 100644
--- a/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatcherInterceptor.java
+++ b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/DispatcherInterceptor.java
@@ -18,6 +18,7 @@
package org.apache.skywalking.apm.plugin.kotlin.coroutine;
+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;
@@ -25,21 +26,31 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInt
import java.lang.reflect.Method;
public class DispatcherInterceptor implements InstanceMethodsAroundInterceptor {
+
@Override
- public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
- MethodInterceptResult result) {
- // Wrapping runnable with current context snapshot
- allArguments[1] = TracingRunnable.wrapOrNot((Runnable) allArguments[1]);
+ public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) {
+ if (!ContextManager.isActive()) {
+ return;
+ }
+
+ Runnable runnable = (Runnable) allArguments[1];
+
+ if (Utils.isDispatchedTask(runnable)) {
+ // Using instrumentation for DispatchedContinuation
+ EnhancedInstance enhancedRunnable = (EnhancedInstance) runnable;
+ enhancedRunnable.setSkyWalkingDynamicField(ContextManager.capture());
+ } else {
+ // Wrapping runnable with current context snapshot
+ allArguments[1] = TracingRunnable.wrapOrNot(runnable);
+ }
}
@Override
- public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
- Object ret) {
+ public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) {
return ret;
}
@Override
- public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
- Class<?>[] argumentsTypes, Throwable t) {
+ public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
}
}
diff --git a/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/TracingRunnable.java b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/TracingRunnable.java
index 9d23f5e6dc..8fb09e9a16 100644
--- a/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/TracingRunnable.java
+++ b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/TracingRunnable.java
@@ -30,7 +30,7 @@ import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
* A class implementation will be cheaper cost than lambda with captured variables implementation.
*/
class TracingRunnable implements Runnable {
- private static final String COROUTINE = "/Kotlin/Coroutine";
+ public static final String COROUTINE = "Kotlin/Coroutine";
private ContextSnapshot snapshot;
private Runnable delegate;
@@ -48,7 +48,7 @@ class TracingRunnable implements Runnable {
*/
public static Runnable wrapOrNot(Runnable delegate) {
// Just wrap continuation with active trace context
- if (ContextManager.isActive()) {
+ if (ContextManager.isActive() && !(delegate instanceof TracingRunnable)) {
return new TracingRunnable(ContextManager.capture(), delegate);
} else {
return delegate;
@@ -72,6 +72,9 @@ class TracingRunnable implements Runnable {
try {
delegate.run();
+ } catch (Throwable e) {
+ span.errorOccurred().log(e);
+ throw e;
} finally {
ContextManager.stopSpan(span);
}
diff --git a/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/Utils.java b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/Utils.java
new file mode 100644
index 0000000000..6698b63ae1
--- /dev/null
+++ b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/Utils.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.apm.plugin.kotlin.coroutine;
+
+import kotlin.coroutines.jvm.internal.CoroutineStackFrame;
+import org.apache.skywalking.apm.plugin.kotlin.coroutine.define.DispatchedTaskInstrumentation;
+
+import java.util.ArrayList;
+
+public class Utils {
+ private static Class<?> DISPATCHED_TASK_CLASS = null;
+ private static Boolean IS_DISPATCHED_TASK_CLASS_LOADED = false;
+
+ private static void loadDispatchedTaskClass() {
+ if (IS_DISPATCHED_TASK_CLASS_LOADED) return;
+ try {
+ DISPATCHED_TASK_CLASS = Class.forName(DispatchedTaskInstrumentation.ENHANCE_CLASS);
+ } catch (ClassNotFoundException ignored) {
+ } finally {
+ IS_DISPATCHED_TASK_CLASS_LOADED = true;
+ }
+ }
+
+ public static boolean isDispatchedTask(Runnable runnable) {
+ loadDispatchedTaskClass();
+ if (DISPATCHED_TASK_CLASS == null) return false;
+ return DISPATCHED_TASK_CLASS.isAssignableFrom(runnable.getClass());
+ }
+
+ public static String[] getCoroutineStackTraceElements(Object runnable) {
+ if (!(runnable instanceof CoroutineStackFrame)) {
+ return new String[0];
+ }
+
+ ArrayList<String> elements = new ArrayList<>();
+ CoroutineStackFrame frame = (CoroutineStackFrame) runnable;
+ while (frame != null) {
+ StackTraceElement element = frame.getStackTraceElement();
+ frame = frame.getCallerFrame();
+
+ if (element != null) {
+ elements.add(element.toString());
+ } else {
+ elements.add("Unknown Source");
+ }
+ }
+ return elements.toArray(new String[0]);
+ }
+}
diff --git a/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/define/DispatchedTaskInstrumentation.java b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/define/DispatchedTaskInstrumentation.java
new file mode 100644
index 0000000000..62177e2c68
--- /dev/null
+++ b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/define/DispatchedTaskInstrumentation.java
@@ -0,0 +1,86 @@
+/*
+ * 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.kotlin.coroutine.define;
+
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.matcher.ElementMatcher;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
+import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
+
+import static net.bytebuddy.matcher.ElementMatchers.named;
+import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments;
+import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
+
+public class DispatchedTaskInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
+ public static final String ENHANCE_CLASS = "kotlinx.coroutines.DispatchedTask";
+ public static final String RUN_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.kotlin.coroutine.DispatchedTaskRunInterceptor";
+ public static final String ENHANCE_METHOD_RUN = "run";
+ public static final String EXCEPTION_INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.kotlin.coroutine.DispatchedTaskExceptionInterceptor";
+ public static final String ENHANCE_METHOD_GET_EXCEPTIONAL_RESULT = "getExceptionalResult$kotlinx_coroutines_core";
+
+ @Override
+ protected ClassMatch enhanceClass() {
+ return byName(ENHANCE_CLASS);
+ }
+
+ @Override
+ public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
+ return new ConstructorInterceptPoint[0];
+ }
+
+ @Override
+ public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
+ return new InstanceMethodsInterceptPoint[]{
+ new InstanceMethodsInterceptPoint() {
+ @Override
+ public ElementMatcher<MethodDescription> getMethodsMatcher() {
+ return named(ENHANCE_METHOD_RUN).and(takesNoArguments());
+ }
+
+ @Override
+ public String getMethodsInterceptor() {
+ return RUN_INTERCEPTOR_CLASS;
+ }
+
+ @Override
+ public boolean isOverrideArgs() {
+ return true;
+ }
+ },
+ new InstanceMethodsInterceptPoint() {
+ @Override
+ public ElementMatcher<MethodDescription> getMethodsMatcher() {
+ return named(ENHANCE_METHOD_GET_EXCEPTIONAL_RESULT);
+ }
+
+ @Override
+ public String getMethodsInterceptor() {
+ return EXCEPTION_INTERCEPTOR_CLASS;
+ }
+
+ @Override
+ public boolean isOverrideArgs() {
+ return false;
+ }
+ }
+ };
+ }
+}
diff --git a/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/resources/skywalking-plugin.def
index a9d7b1bc82..1cb46e0789 100644
--- a/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/resources/skywalking-plugin.def
+++ b/apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/resources/skywalking-plugin.def
@@ -14,4 +14,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-kotlin-coroutine=org.apache.skywalking.apm.plugin.kotlin.coroutine.define.DispatcherInstrumentation
\ No newline at end of file
+kotlin-coroutine=org.apache.skywalking.apm.plugin.kotlin.coroutine.define.DispatcherInstrumentation
+kotlin-coroutine=org.apache.skywalking.apm.plugin.kotlin.coroutine.define.DispatchedTaskInstrumentation
\ No newline at end of file
diff --git a/test/plugin/scenarios/kotlin-coroutine-scenario/config/expectedData.yaml b/test/plugin/scenarios/kotlin-coroutine-scenario/config/expectedData.yaml
index d0cb831780..62646ffe08 100644
--- a/test/plugin/scenarios/kotlin-coroutine-scenario/config/expectedData.yaml
+++ b/test/plugin/scenarios/kotlin-coroutine-scenario/config/expectedData.yaml
@@ -49,7 +49,7 @@ segmentItems:
- {key: db.instance, value: demo}
- {key: db.statement, value: ''}
skipAnalysis: 'false'
- - operationName: /Kotlin/Coroutine
+ - operationName: Kotlin/Coroutine
parentSpanId: -1
spanId: 0
startTime: nq 0