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 2022/03/25 08:23:51 UTC

[skywalking-java] branch parent-endpoint created (now a1a2124)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a change to branch parent-endpoint
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git.


      at a1a2124  Make sure the parent endpoint in tracing context from existing first ENTRY span, rather than first span only.

This branch includes the following new commits:

     new a1a2124  Make sure the parent endpoint in tracing context from existing first ENTRY span, rather than first span only.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[skywalking-java] 01/01: Make sure the parent endpoint in tracing context from existing first ENTRY span, rather than first span only.

Posted by wu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch parent-endpoint
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git

commit a1a2124bae0bcb733a11d283a895f7bd3623b2d2
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Fri Mar 25 16:23:43 2022 +0800

    Make sure the parent endpoint in tracing context from existing first ENTRY span, rather than first span only.
---
 CHANGES.md                                         |  1 +
 .../apm/agent/core/context/TracingContext.java     | 48 ++++++++++++++++------
 2 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 6ed0bac..08b1377 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -33,6 +33,7 @@ Release Notes.
 * The namespace and cluster would be reported as instance properties, keys are `namespace` and `cluster`. Notice, if
   instance_properties_json includes these two keys, they would be overrided by the agent core.
 * [Breaking Change] Remove the namespace from `cross process propagation` key.
+* Make sure the parent endpoint in tracing context from existing first ENTRY span, rather than first span only.
 
 #### Documentation
 
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
index ec9c4e1..9cba2d0 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
@@ -78,12 +78,11 @@ public class TracingContext implements AbstractTracerContext {
      * LinkedList#getLast()} instead of {@link #pop()}, {@link #push(AbstractSpan)}, {@link #peek()}
      */
     private LinkedList<AbstractSpan> activeSpanStack = new LinkedList<>();
+
     /**
-     * @since 7.0.0 SkyWalking support lazy injection through {@link ExitTypeSpan#inject(ContextCarrier)}. Due to that,
-     * the {@link #activeSpanStack} could be blank by then, this is a pointer forever to the first span, even the main
-     * thread tracing has been finished.
+     * @since 8.10.0 replace the removed "firstSpan"(before 8.10.0) reference. see {@link PrimaryEndpoint} for more details.
      */
-    private AbstractSpan firstSpan = null;
+    private PrimaryEndpoint primaryEndpoint = null;
 
     /**
      * A counter for the next span.
@@ -175,7 +174,7 @@ public class TracingContext implements AbstractTracerContext {
         carrier.setSpanId(exitSpan.getSpanId());
         carrier.setParentService(Config.Agent.SERVICE_NAME);
         carrier.setParentServiceInstance(Config.Agent.INSTANCE_NAME);
-        carrier.setParentEndpoint(first().getOperationName());
+        carrier.setParentEndpoint(primaryEndpoint.getName());
         carrier.setAddressUsedAtClient(peer);
 
         this.correlationContext.inject(carrier);
@@ -212,7 +211,7 @@ public class TracingContext implements AbstractTracerContext {
             segment.getTraceSegmentId(),
             activeSpan().getSpanId(),
             getPrimaryTraceId(),
-            first().getOperationName(),
+            primaryEndpoint.getName(),
             this.correlationContext,
             this.extensionContext
         );
@@ -527,8 +526,10 @@ public class TracingContext implements AbstractTracerContext {
      * @param span the {@code span} to push
      */
     private AbstractSpan push(AbstractSpan span) {
-        if (firstSpan == null) {
-            firstSpan = span;
+        if (primaryEndpoint == null) {
+            primaryEndpoint = new PrimaryEndpoint(span.isEntry(), span.getOperationName());
+        } else {
+            primaryEndpoint.set(span.isEntry(), span.getOperationName());
         }
         activeSpanStack.addLast(span);
         this.extensionContext.handle(span);
@@ -545,10 +546,6 @@ public class TracingContext implements AbstractTracerContext {
         return activeSpanStack.getLast();
     }
 
-    private AbstractSpan first() {
-        return firstSpan;
-    }
-
     private boolean isLimitMechanismWorking() {
         if (spanIdGenerator >= spanLimitWatcher.getSpanLimit()) {
             long currentTimeMillis = System.currentTimeMillis();
@@ -572,4 +569,31 @@ public class TracingContext implements AbstractTracerContext {
     public ProfileStatusReference profileStatus() {
         return this.profileStatus;
     }
+
+    /**
+     * Primary endpoint name is used for endpoint dependency. The name pick policy according to priority is
+     * 1. Use the first entry span's operation name
+     * 2. Use the first span's operation name
+     *
+     * @since 8.10.0
+     */
+    private class PrimaryEndpoint {
+        private boolean isEntry;
+        @Getter
+        private String name;
+
+        private PrimaryEndpoint(final boolean isEntry, final String name) {
+            this.isEntry = isEntry;
+            this.name = name;
+        }
+
+        /**
+         * Set endpoint name according to priority
+         */
+        private void set(final boolean isEntry, final String name) {
+            if (!this.isEntry && isEntry) {
+                this.name = name;
+            }
+        }
+    }
 }