From 1c32d5954253f8229c551dcb35a5f99f575d5529 Mon Sep 17 00:00:00 2001 From: "Xin,Zhang" Date: Wed, 27 Feb 2019 20:19:18 +0800 Subject: [PATCH] Add peer to entry span (#2287) * Add peer to entry span * Fix CI --- .../core/context/trace/AbstractSpan.java | 2 + .../agent/core/context/trace/ExitSpan.java | 32 ++-------- .../agent/core/context/trace/LocalSpan.java | 4 ++ .../agent/core/context/trace/NoopSpan.java | 4 ++ .../context/trace/StackBasedTracingSpan.java | 61 +++++++++++++++++++ 5 files changed, 75 insertions(+), 28 deletions(-) diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/AbstractSpan.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/AbstractSpan.java index c6e20396d..17e8aefb7 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/AbstractSpan.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/AbstractSpan.java @@ -127,4 +127,6 @@ public interface AbstractSpan { void ref(TraceSegmentRef ref); AbstractSpan start(long starttime); + + AbstractSpan setPeer(String remotePeer); } diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/ExitSpan.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/ExitSpan.java index 5efe52150..29a5aafe7 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/ExitSpan.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/ExitSpan.java @@ -20,8 +20,6 @@ package org.apache.skywalking.apm.agent.core.context.trace; import org.apache.skywalking.apm.agent.core.context.tag.AbstractTag; -import org.apache.skywalking.apm.agent.core.dictionary.DictionaryUtil; -import org.apache.skywalking.apm.network.language.agent.v2.SpanObjectV2; import org.apache.skywalking.apm.network.trace.component.Component; /** @@ -38,31 +36,21 @@ import org.apache.skywalking.apm.network.trace.component.Component; * @author wusheng */ public class ExitSpan extends StackBasedTracingSpan implements WithPeerInfo { - private String peer; - private int peerId; public ExitSpan(int spanId, int parentSpanId, String operationName, String peer) { - super(spanId, parentSpanId, operationName); - this.peer = peer; - this.peerId = DictionaryUtil.nullValue(); + super(spanId, parentSpanId, operationName, peer); } public ExitSpan(int spanId, int parentSpanId, int operationId, int peerId) { - super(spanId, parentSpanId, operationId); - this.peer = null; - this.peerId = peerId; + super(spanId, parentSpanId, operationId, peerId); } public ExitSpan(int spanId, int parentSpanId, int operationId, String peer) { - super(spanId, parentSpanId, operationId); - this.peer = peer; - this.peerId = DictionaryUtil.nullValue(); + super(spanId, parentSpanId, operationId, peer); } public ExitSpan(int spanId, int parentSpanId, String operationName, int peerId) { - super(spanId, parentSpanId, operationName); - this.peer = null; - this.peerId = peerId; + super(spanId, parentSpanId, operationName, peerId); } /** @@ -126,18 +114,6 @@ public class ExitSpan extends StackBasedTracingSpan implements WithPeerInfo { return this; } - @Override public SpanObjectV2.Builder transform() { - SpanObjectV2.Builder spanBuilder = super.transform(); - if (peerId != DictionaryUtil.nullValue()) { - spanBuilder.setPeerId(peerId); - } else { - if (peer != null) { - spanBuilder.setPeer(peer); - } - } - return spanBuilder; - } - @Override public AbstractTracingSpan setOperationName(String operationName) { if (stackDepth == 1) { diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/LocalSpan.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/LocalSpan.java index fea4d2a72..1bd4a77ea 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/LocalSpan.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/LocalSpan.java @@ -53,4 +53,8 @@ public class LocalSpan extends AbstractTracingSpan { @Override public boolean isExit() { return false; } + + @Override public AbstractSpan setPeer(String remotePeer) { + return this; + } } diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/NoopSpan.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/NoopSpan.java index d063b629e..774ebca69 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/NoopSpan.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/NoopSpan.java @@ -111,4 +111,8 @@ public class NoopSpan implements AbstractSpan { @Override public AbstractSpan start(long startTime) { return this; } + + @Override public AbstractSpan setPeer(String remotePeer) { + return this; + } } diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/StackBasedTracingSpan.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/StackBasedTracingSpan.java index dc0899a3c..afb0dfa1e 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/StackBasedTracingSpan.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/trace/StackBasedTracingSpan.java @@ -21,6 +21,7 @@ package org.apache.skywalking.apm.agent.core.context.trace; import org.apache.skywalking.apm.agent.core.dictionary.DictionaryManager; import org.apache.skywalking.apm.agent.core.dictionary.DictionaryUtil; import org.apache.skywalking.apm.agent.core.dictionary.PossibleFound; +import org.apache.skywalking.apm.network.language.agent.v2.SpanObjectV2; /** * The StackBasedTracingSpan represents a span with an inside stack construction. @@ -31,15 +32,58 @@ import org.apache.skywalking.apm.agent.core.dictionary.PossibleFound; */ public abstract class StackBasedTracingSpan extends AbstractTracingSpan { protected int stackDepth; + protected String peer; + protected int peerId; protected StackBasedTracingSpan(int spanId, int parentSpanId, String operationName) { super(spanId, parentSpanId, operationName); this.stackDepth = 0; + this.peer = null; + this.peerId = DictionaryUtil.nullValue(); } protected StackBasedTracingSpan(int spanId, int parentSpanId, int operationId) { super(spanId, parentSpanId, operationId); this.stackDepth = 0; + this.peer = null; + this.peerId = DictionaryUtil.nullValue(); + } + + public StackBasedTracingSpan(int spanId, int parentSpanId, int operationId, int peerId) { + super(spanId, parentSpanId, operationId); + this.peer = null; + this.peerId = peerId; + } + + public StackBasedTracingSpan(int spanId, int parentSpanId, int operationId, String peer) { + super(spanId, parentSpanId, operationId); + this.peer = peer; + this.peerId = DictionaryUtil.nullValue(); + } + + protected StackBasedTracingSpan(int spanId, int parentSpanId, String operationName, String peer) { + super(spanId, parentSpanId, operationName); + this.peer = peer; + this.peerId = DictionaryUtil.nullValue(); + } + + protected StackBasedTracingSpan(int spanId, int parentSpanId, String operationName, int peerId) { + super(spanId, parentSpanId, operationName); + this.peer = null; + this.peerId = peerId; + } + + @Override + public SpanObjectV2.Builder transform() { + SpanObjectV2.Builder spanBuilder = super.transform(); + if (peerId != DictionaryUtil.nullValue()) { + spanBuilder.setPeerId(peerId); + } else { + if (peer != null) { + spanBuilder.setPeer(peer); + } + } + return spanBuilder; } @Override @@ -66,4 +110,21 @@ public abstract class StackBasedTracingSpan extends AbstractTracingSpan { return false; } } + + @Override public AbstractSpan setPeer(final String remotePeer) { + DictionaryManager.findNetworkAddressSection().find(remotePeer).doInCondition( + new PossibleFound.Found() { + @Override + public void doProcess(int remotePeerId) { + peerId = remotePeerId; + } + }, new PossibleFound.NotFound() { + @Override + public void doProcess() { + peer = remotePeer; + } + } + ); + return this; + } }