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

[GitHub] [skywalking] dmsolr commented on a change in pull request #4165: Add kotlin coroutine plugin

dmsolr commented on a change in pull request #4165: Add kotlin coroutine plugin
URL: https://github.com/apache/skywalking/pull/4165#discussion_r362721134
 
 

 ##########
 File path: apm-sniffer/optional-plugins/kotlin-coroutine-plugin/src/main/java/org/apache/skywalking/apm/plugin/kotlin/coroutine/TracingRunnable.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.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.network.trace.component.ComponentsDefine;
+
+/**
+ * {@link Runnable} wrapper with trace context snapshot, it will create span
+ * with context snapshot around {@link Runnable} runs.
+ * <p>
+ * A class implementation will be cheaper cost than lambda with captured
+ * variables implementation.
+ */
+class TracingRunnable implements Runnable {
+    private static final String COROUTINE = "/Kotlin/Coroutine";
+
+    private ContextSnapshot snapshot;
+    private Runnable delegate;
+    private String tracingId;
+
+    private TracingRunnable(ContextSnapshot snapshot, Runnable delegate, String tracingId) {
+        this.snapshot = snapshot;
+        this.delegate = delegate;
+        this.tracingId = tracingId;
+    }
+
+    private TracingRunnable(ContextSnapshot snapshot, Runnable delegate) {
+        this(snapshot, delegate, ContextManager.getGlobalTraceId());
+    }
+
+    /**
+     * Wrap {@link Runnable} by {@link TracingRunnable} if active trace context
+     * existed.
+     *
+     * @param delegate {@link Runnable} to wrap.
+     *
+     * @return Wrapped {@link TracingRunnable} or original {@link Runnable} if
+     * trace context not existed.
+     */
+    public static Runnable wrapOrNot(Runnable delegate) {
+        // Just wrap continuation with active trace context
+        if (ContextManager.isActive()) {
+            return new TracingRunnable(ContextManager.capture(), delegate);
+        } else {
+            return delegate;
+        }
+    }
+
+    @Override
+    public void run() {
+        if (ContextManager.getGlobalTraceId().equals(tracingId)) {
+            // Trace id same with before dispatching, skip restore snapshot.
+            delegate.run();
+            return;
+        }
 
 Review comment:
   Do we need it?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services