From 5c369aefb4040da6c4e0570589479304af479faf Mon Sep 17 00:00:00 2001 From: wusheng Date: Thu, 16 Mar 2017 16:30:11 +0800 Subject: [PATCH] Alter TraceSegement, Add a list of DistributedTraceId as field `relatedGlobalTraces`. --- .../skywalking/trace/GlobalIdGenerator.java | 15 ++-- .../trace/TraceId/DistributedTraceId.java | 26 ++++++ .../trace/TraceId/NewDistributedTraceId.java | 16 ++++ .../trace/TraceId/PropagatedTraceId.java | 12 +++ .../a/eye/skywalking/trace/TraceSegment.java | 65 ++++++++++++--- .../src/main/proto/trace.proto | 3 +- .../a/eye/skywalking/trace/SpanTestCase.java | 2 +- .../trace/TraceSegmentTestCase.java | 12 +-- .../eye/skywalking/api/util/MachineInfo.java | 0 .../a/eye/skywalking/api/conf/Constants.java | 4 +- .../api/context/ContextCarrier.java | 83 +++++++++++++++++-- .../skywalking/api/context/TracerContext.java | 5 +- .../api/context/ContextCarrierTestCase.java | 17 +++- .../api/context/TracerContextTestCase.java | 7 ++ .../plugin/dubbo/DubboInterceptorTest.java | 15 +++- .../dubbo/RequestParamForTestBelow283.java | 8 -- .../motan/MotanProviderInterceptorTest.java | 4 +- .../tomcat78x/TomcatInterceptorTest.java | 2 +- 18 files changed, 242 insertions(+), 54 deletions(-) rename skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/TraceIdGenerator.java => skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/GlobalIdGenerator.java (65%) create mode 100644 skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceId/DistributedTraceId.java create mode 100644 skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceId/NewDistributedTraceId.java create mode 100644 skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceId/PropagatedTraceId.java rename {skywalking-sniffer/skywalking-api => skywalking-commons/skywalking-util}/src/main/java/com/a/eye/skywalking/api/util/MachineInfo.java (100%) diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/TraceIdGenerator.java b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/GlobalIdGenerator.java similarity index 65% rename from skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/TraceIdGenerator.java rename to skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/GlobalIdGenerator.java index fc3ac560c..5d6c50ffd 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/TraceIdGenerator.java +++ b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/GlobalIdGenerator.java @@ -1,9 +1,10 @@ -package com.a.eye.skywalking.api.util; +package com.a.eye.skywalking.trace; -import com.a.eye.skywalking.api.conf.Constants; +import com.a.eye.skywalking.api.util.MachineInfo; +import com.a.eye.skywalking.api.util.StringUtil; import java.util.UUID; -public final class TraceIdGenerator { +public final class GlobalIdGenerator { private static final ThreadLocal ThreadTraceIdSequence = new ThreadLocal() { @Override protected Integer initialValue() { @@ -18,25 +19,25 @@ public final class TraceIdGenerator { PROCESS_UUID = uuid.substring(uuid.length() - 7).hashCode(); } - private TraceIdGenerator() { + private GlobalIdGenerator() { } /** * TraceId由以下规则组成
- * version号 + 1位时间戳(毫秒数) + 1位进程随机号(UUID后7位) + 1位进程数号 + 1位线程号 + 1位线程内序号 + * ID类型 + 1位时间戳(毫秒数) + 1位进程随机号(UUID后7位) + 1位进程数号 + 1位线程号 + 1位线程内序号 *

* 注意:这里的位,是指“.”作为分隔符所占的位数,非字符串长度的位数。 * TraceId为6个片段组成的数组 * * @return */ - public static String generate() { + public static String generate(String type) { Integer seq = ThreadTraceIdSequence.get(); seq++; ThreadTraceIdSequence.set(seq); return StringUtil.join('.', - Constants.SDK_VERSION + "", System.currentTimeMillis() + "", PROCESS_UUID + "", + type + "", System.currentTimeMillis() + "", PROCESS_UUID + "", MachineInfo.getProcessNo() + "", Thread.currentThread().getId() + "", seq + ""); } } diff --git a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceId/DistributedTraceId.java b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceId/DistributedTraceId.java new file mode 100644 index 000000000..d1988a9ce --- /dev/null +++ b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceId/DistributedTraceId.java @@ -0,0 +1,26 @@ +package com.a.eye.skywalking.trace.TraceId; + +/** + * The DistributedTraceId presents a distributed call chain. + * + * This call chain has an unique (service) entrance, + * + * such as: Service : http://www.skywalking.com/cust/query, all the services, called behind this service, rest services, + * db executions, are using the same DistributedTraceId even in different JVM. + * + * The DistributedTraceId contains only one string, and can NOT be reset, creating a new instance is the + * only option. + * + * @author wusheng + */ +public abstract class DistributedTraceId { + private String id; + + public DistributedTraceId(String id) { + this.id = id; + } + + public String get() { + return id; + } +} diff --git a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceId/NewDistributedTraceId.java b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceId/NewDistributedTraceId.java new file mode 100644 index 000000000..6394844dd --- /dev/null +++ b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceId/NewDistributedTraceId.java @@ -0,0 +1,16 @@ +package com.a.eye.skywalking.trace.TraceId; + +import com.a.eye.skywalking.trace.GlobalIdGenerator; + +/** + * The NewDistributedTraceId is a {@link DistributedTraceId} with a new generated id. + * + * @author wusheng + */ +public class NewDistributedTraceId extends DistributedTraceId { + private static final String ID_TYPE = "Trace"; + + public NewDistributedTraceId() { + super(GlobalIdGenerator.generate(ID_TYPE)); + } +} diff --git a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceId/PropagatedTraceId.java b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceId/PropagatedTraceId.java new file mode 100644 index 000000000..50772bbd8 --- /dev/null +++ b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceId/PropagatedTraceId.java @@ -0,0 +1,12 @@ +package com.a.eye.skywalking.trace.TraceId; + +/** + * The PropagatedTraceId represents a {@link DistributedTraceId}, which is propagated from the peer. + * + * @author wusheng + */ +public class PropagatedTraceId extends DistributedTraceId { + public PropagatedTraceId(String id) { + super(id); + } +} diff --git a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegment.java b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegment.java index 53f3151b8..02c7e2280 100644 --- a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegment.java +++ b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegment.java @@ -1,9 +1,13 @@ package com.a.eye.skywalking.trace; import com.a.eye.skywalking.messages.ISerializable; +import com.a.eye.skywalking.trace.TraceId.DistributedTraceId; +import com.a.eye.skywalking.trace.TraceId.NewDistributedTraceId; +import com.a.eye.skywalking.trace.TraceId.PropagatedTraceId; import com.a.eye.skywalking.trace.proto.SegmentMessage; import com.a.eye.skywalking.trace.proto.SegmentRefMessage; import com.a.eye.skywalking.trace.proto.SpanMessage; +import com.google.protobuf.ProtocolStringList; import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -18,6 +22,8 @@ import java.util.List; * Created by wusheng on 2017/2/17. */ public class TraceSegment implements ISerializable { + private static final String ID_TYPE = "Segment"; + /** * The id of this trace segment. * Every segment has its unique-global-id. @@ -63,17 +69,32 @@ public class TraceSegment implements ISerializable { */ private String applicationCode; + /** + * The relatedGlobalTraces represent a set of all related trace. Most time it contains only one + * element, because only one parent {@link TraceSegment} exists, but, in batch scenario, the num becomes greater + * than 1, also meaning multi-parents {@link TraceSegment}. + * + * The difference between relatedGlobalTraces and {@link #primaryRef}/{@link #refs} is: {@link + * #primaryRef}/{@link #refs} targets this {@link TraceSegment}'s direct parent, + * + * and + * + * relatedGlobalTraces targets this {@link TraceSegment}'s related call chain, a call chain contains + * multi {@link TraceSegment}s, only using {@link #primaryRef}/{@link #refs} is enough for analysis and ui. + */ + private LinkedList relatedGlobalTraces; + /** * Create a trace segment, by given segmentId. * This segmentId is generated by TraceSegmentRef, AKA, from tracer/agent module. - * - * @param segmentId {@link #traceSegmentId} */ - public TraceSegment(String segmentId, String applicationCode) { - this.traceSegmentId = segmentId; + public TraceSegment(String applicationCode) { + this.traceSegmentId = GlobalIdGenerator.generate(ID_TYPE); this.applicationCode = applicationCode; this.startTime = System.currentTimeMillis(); this.spans = new LinkedList(); + this.relatedGlobalTraces = new LinkedList(); + this.relatedGlobalTraces.add(new NewDistributedTraceId()); } /** @@ -95,11 +116,11 @@ public class TraceSegment implements ISerializable { * @param primaryOnly if true, set {@param refSegment} to {@link #primaryRef} only. */ public void ref(TraceSegmentRef refSegment, boolean primaryOnly) { - if(primaryOnly){ + if (primaryOnly) { if (primaryRef == null) { primaryRef = refSegment; } - }else { + } else { if (primaryRef == null) { primaryRef = refSegment; } else { @@ -117,10 +138,20 @@ public class TraceSegment implements ISerializable { * * @param refSegment {@link TraceSegmentRef} */ - public void ref(TraceSegmentRef refSegment){ + public void ref(TraceSegmentRef refSegment) { ref(refSegment, true); } + public void buildRelation(List distributedTraceIds){ + if(distributedTraceIds == null || distributedTraceIds.size() == 0){ + return; + } + if(relatedGlobalTraces.getFirst() instanceof NewDistributedTraceId){ + relatedGlobalTraces.removeFirst(); + } + relatedGlobalTraces.addAll(distributedTraceIds); + } + /** * After {@link Span} is finished, as be controller by "skywalking-api" module, * notify the {@link TraceSegment} to archive it. @@ -158,12 +189,16 @@ public class TraceSegment implements ISerializable { } public List getRefs() { - if(refs == null){ + if (refs == null) { return null; } return Collections.unmodifiableList(refs); } + public List getRelatedGlobalTraces() { + return Collections.unmodifiableList(relatedGlobalTraces); + } + public List getSpans() { return Collections.unmodifiableList(spans); } @@ -189,14 +224,17 @@ public class TraceSegment implements ISerializable { segmentBuilder.setStartTime(startTime); segmentBuilder.setEndTime(endTime); segmentBuilder.setApplicationCode(applicationCode); - if(primaryRef != null) { + if (primaryRef != null) { segmentBuilder.setPrimaryRef(primaryRef.serialize()); } - if(refs != null && refs.size() > 0) { + if (refs != null && refs.size() > 0) { for (TraceSegmentRef ref : refs) { segmentBuilder.addRefs(ref.serialize()); } } + for (DistributedTraceId id : relatedGlobalTraces) { + segmentBuilder.addRelatedTraceIds(id.get()); + } for (Span span : spans) { segmentBuilder.addSpans(span.serialize()); } @@ -210,7 +248,7 @@ public class TraceSegment implements ISerializable { endTime = message.getEndTime(); applicationCode = message.getApplicationCode(); SegmentRefMessage messagePrimaryRef = message.getPrimaryRef(); - if(messagePrimaryRef != null) { + if (messagePrimaryRef != null) { (primaryRef = new TraceSegmentRef()).deserialize(messagePrimaryRef); } List refsList = message.getRefsList(); @@ -222,6 +260,11 @@ public class TraceSegment implements ISerializable { refs.add(ref); } } + ProtocolStringList relatedTraceIdsList = message.getRelatedTraceIdsList(); + this.relatedGlobalTraces = new LinkedList(); + for (String id : relatedTraceIdsList) { + relatedGlobalTraces.add(new PropagatedTraceId(id)); + } List spansList = message.getSpansList(); if (spansList != null) { diff --git a/skywalking-commons/skywalking-trace/src/main/proto/trace.proto b/skywalking-commons/skywalking-trace/src/main/proto/trace.proto index a5e4d77f4..56900dfac 100644 --- a/skywalking-commons/skywalking-trace/src/main/proto/trace.proto +++ b/skywalking-commons/skywalking-trace/src/main/proto/trace.proto @@ -10,7 +10,8 @@ message SegmentMessage { string applicationCode = 4; SegmentRefMessage primaryRef = 5; repeated SegmentRefMessage refs = 6; - repeated SpanMessage spans = 7; + repeated string relatedTraceIds = 7; + repeated SpanMessage spans = 8; } message SegmentRefMessage { diff --git a/skywalking-commons/skywalking-trace/src/test/java/com/a/eye/skywalking/trace/SpanTestCase.java b/skywalking-commons/skywalking-trace/src/test/java/com/a/eye/skywalking/trace/SpanTestCase.java index f2b07877a..b58ddd0ad 100644 --- a/skywalking-commons/skywalking-trace/src/test/java/com/a/eye/skywalking/trace/SpanTestCase.java +++ b/skywalking-commons/skywalking-trace/src/test/java/com/a/eye/skywalking/trace/SpanTestCase.java @@ -25,7 +25,7 @@ public class SpanTestCase { @Test public void testFinish() { - TraceSegment owner = new TraceSegment("trace_1", "billing_app"); + TraceSegment owner = new TraceSegment("billing_app"); Span span1 = new Span(0, "serviceA"); diff --git a/skywalking-commons/skywalking-trace/src/test/java/com/a/eye/skywalking/trace/TraceSegmentTestCase.java b/skywalking-commons/skywalking-trace/src/test/java/com/a/eye/skywalking/trace/TraceSegmentTestCase.java index 37a6fac69..cd120c079 100644 --- a/skywalking-commons/skywalking-trace/src/test/java/com/a/eye/skywalking/trace/TraceSegmentTestCase.java +++ b/skywalking-commons/skywalking-trace/src/test/java/com/a/eye/skywalking/trace/TraceSegmentTestCase.java @@ -10,16 +10,16 @@ import org.junit.Test; public class TraceSegmentTestCase { @Test public void testConstructor() { - TraceSegment segment = new TraceSegment("trace_1", "billing_app"); + TraceSegment segment = new TraceSegment( "billing_app"); - Assert.assertEquals("trace_1", segment.getTraceSegmentId()); + Assert.assertTrue(segment.getTraceSegmentId().startsWith("Segment")); Assert.assertTrue(segment.getStartTime() > 0); Assert.assertEquals("billing_app", segment.getApplicationCode()); } @Test public void testRef() { - TraceSegment segment = new TraceSegment("trace_3", "billing_app"); + TraceSegment segment = new TraceSegment("billing_app"); TraceSegmentRef ref1 = new TraceSegmentRef(); ref1.setTraceSegmentId("parent_trace_0"); @@ -46,7 +46,7 @@ public class TraceSegmentTestCase { @Test public void testArchiveSpan() { - TraceSegment segment = new TraceSegment("trace_1", "billing_app"); + TraceSegment segment = new TraceSegment("billing_app"); Span span1 = new Span(1, "/serviceA"); segment.archive(span1); @@ -59,7 +59,7 @@ public class TraceSegmentTestCase { @Test public void testFinish() { - TraceSegment segment = new TraceSegment("trace_1", "billing_app"); + TraceSegment segment = new TraceSegment("billing_app"); Assert.assertTrue(segment.getEndTime() == 0); segment.finish(); @@ -68,7 +68,7 @@ public class TraceSegmentTestCase { @Test public void testSerialize() { - TraceSegment segment = new TraceSegment("trace_3", "billing_app"); + TraceSegment segment = new TraceSegment("billing_app"); TraceSegmentRef ref1 = new TraceSegmentRef(); ref1.setTraceSegmentId("parent_trace_0"); diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/MachineInfo.java b/skywalking-commons/skywalking-util/src/main/java/com/a/eye/skywalking/api/util/MachineInfo.java similarity index 100% rename from skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/MachineInfo.java rename to skywalking-commons/skywalking-util/src/main/java/com/a/eye/skywalking/api/util/MachineInfo.java diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/conf/Constants.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/conf/Constants.java index 4e5dbcb46..dbbe904d5 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/conf/Constants.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/conf/Constants.java @@ -1,11 +1,11 @@ package com.a.eye.skywalking.api.conf; -import com.a.eye.skywalking.api.util.TraceIdGenerator; +import com.a.eye.skywalking.trace.GlobalIdGenerator; public class Constants { /** * This is the version, which will be the first segment of traceid. - * Ref {@link TraceIdGenerator#generate()} + * Ref {@link GlobalIdGenerator#generate()} */ public final static String SDK_VERSION = "302017"; } diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/ContextCarrier.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/ContextCarrier.java index a7d5a96ed..ebe5147fd 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/ContextCarrier.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/ContextCarrier.java @@ -1,10 +1,14 @@ package com.a.eye.skywalking.api.context; import com.a.eye.skywalking.trace.Span; +import com.a.eye.skywalking.trace.TraceId.DistributedTraceId; +import com.a.eye.skywalking.trace.TraceId.PropagatedTraceId; import com.a.eye.skywalking.trace.TraceSegment; import com.a.eye.skywalking.api.util.StringUtil; import com.a.eye.skywalking.trace.tag.Tags; import java.io.Serializable; +import java.util.LinkedList; +import java.util.List; /** * {@link ContextCarrier} is a data carrier of {@link TracerContext}. @@ -33,6 +37,11 @@ public class ContextCarrier implements Serializable { */ private String peerHost; + /** + * {@link DistributedTraceId} + */ + private List distributedTraceIds; + /** * Serialize this {@link ContextCarrier} to a {@link String}, * with '|' split. @@ -40,7 +49,12 @@ public class ContextCarrier implements Serializable { * @return the serialization string. */ public String serialize() { - return StringUtil.join('|', this.getTraceSegmentId(), this.getSpanId() + "", this.getApplicationCode(), this.getPeerHost()); + return StringUtil.join('|', + this.getTraceSegmentId(), + this.getSpanId() + "", + this.getApplicationCode(), + this.getPeerHost(), + this.serializeDistributedTraceIds()); } /** @@ -49,15 +63,16 @@ public class ContextCarrier implements Serializable { * @param text carries {@link #traceSegmentId} and {@link #spanId}, with '|' split. */ public ContextCarrier deserialize(String text) { - if(text != null){ - String[] parts = text.split("\\|", 4); - if(parts.length == 4){ - try{ + if (text != null) { + String[] parts = text.split("\\|", 5); + if (parts.length == 5) { + try { setSpanId(Integer.parseInt(parts[1])); setTraceSegmentId(parts[0]); setApplicationCode(parts[2]); setPeerHost(parts[3]); - }catch(NumberFormatException e){ + setDistributedTraceIds(deserializeDistributedTraceIds(parts[4])); + } catch (NumberFormatException e) { } } @@ -70,8 +85,12 @@ public class ContextCarrier implements Serializable { * * @return true for unbroken {@link ContextCarrier} or no-initialized. Otherwise, false; */ - public boolean isValid(){ - return !StringUtil.isEmpty(traceSegmentId) && getSpanId() > -1 && !StringUtil.isEmpty(applicationCode) && !StringUtil.isEmpty(peerHost); + public boolean isValid() { + return !StringUtil.isEmpty(traceSegmentId) + && getSpanId() > -1 + && !StringUtil.isEmpty(applicationCode) + && !StringUtil.isEmpty(peerHost) + && distributedTraceIds != null; } public String getTraceSegmentId() { @@ -105,4 +124,52 @@ public class ContextCarrier implements Serializable { public void setPeerHost(String peerHost) { this.peerHost = peerHost; } + + public List getDistributedTraceIds() { + return distributedTraceIds; + } + + public void setDistributedTraceIds(List distributedTraceIds) { + this.distributedTraceIds = distributedTraceIds; + } + + /** + * Serialize {@link #distributedTraceIds} to a string, with ',' split. + * + * @return string, represents all {@link DistributedTraceId} + */ + private String serializeDistributedTraceIds() { + StringBuilder traceIdString = new StringBuilder(); + if (distributedTraceIds != null) { + boolean first = true; + for (DistributedTraceId distributedTraceId : distributedTraceIds) { + if (first) { + first = false; + } else { + traceIdString.append(","); + } + traceIdString.append(distributedTraceId.get()); + } + } + return traceIdString.toString(); + } + + /** + * Deserialize {@link #distributedTraceIds} from a text, whith + * + * @param text + * @return + */ + private List deserializeDistributedTraceIds(String text) { + if (StringUtil.isEmpty(text)) { + return null; + } + String[] propagationTraceIdValues = text.split(","); + List traceIds = new LinkedList<>(); + for (String propagationTraceIdValue : propagationTraceIdValues) { + traceIds.add(new PropagatedTraceId(propagationTraceIdValue)); + } + return traceIds; + } + } diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java index c723fc3c0..cc9684bc6 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java @@ -3,7 +3,6 @@ package com.a.eye.skywalking.api.context; import com.a.eye.skywalking.api.conf.Config; import com.a.eye.skywalking.trace.Span; import com.a.eye.skywalking.trace.TraceSegment; -import com.a.eye.skywalking.api.util.TraceIdGenerator; import com.a.eye.skywalking.trace.TraceSegmentRef; import com.a.eye.skywalking.trace.tag.Tags; import java.util.ArrayList; @@ -35,7 +34,7 @@ public final class TracerContext { * Create a {@link TraceSegment} and init {@link #spanIdGenerator} as 0; */ TracerContext() { - this.segment = new TraceSegment(TraceIdGenerator.generate(), Config.SkyWalking.APPLICATION_CODE); + this.segment = new TraceSegment(Config.SkyWalking.APPLICATION_CODE); this.spanIdGenerator = 0; } @@ -126,6 +125,7 @@ public final class TracerContext { carrier.setSpanId(this.activeSpan().getSpanId()); carrier.setApplicationCode(Config.SkyWalking.APPLICATION_CODE); carrier.setPeerHost(Tags.PEER_HOST.get(activeSpan())); + carrier.setDistributedTraceIds(this.segment.getRelatedGlobalTraces()); } /** @@ -137,6 +137,7 @@ public final class TracerContext { public void extract(ContextCarrier carrier) { if(carrier.isValid()) { this.segment.ref(getRef(carrier)); + this.segment.buildRelation(carrier.getDistributedTraceIds()); } } diff --git a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/context/ContextCarrierTestCase.java b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/context/ContextCarrierTestCase.java index 8b6e1fc26..901aab1fd 100644 --- a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/context/ContextCarrierTestCase.java +++ b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/context/ContextCarrierTestCase.java @@ -1,5 +1,9 @@ package com.a.eye.skywalking.api.context; +import com.a.eye.skywalking.trace.TraceId.DistributedTraceId; +import com.a.eye.skywalking.trace.TraceId.PropagatedTraceId; +import java.util.LinkedList; +import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -14,19 +18,24 @@ public class ContextCarrierTestCase { carrier.setSpanId(100); carrier.setApplicationCode("REMOTE_APP"); carrier.setPeerHost("10.2.3.16:8080"); + List ids = new LinkedList<>(); + ids.add(new PropagatedTraceId("Trace.global.id.123")); + carrier.setDistributedTraceIds(ids); - Assert.assertEquals("trace_id_A|100|REMOTE_APP|10.2.3.16:8080", carrier.serialize()); + Assert.assertEquals("trace_id_A|100|REMOTE_APP|10.2.3.16:8080|Trace.global.id.123", carrier.serialize()); } @Test public void testDeserialize(){ ContextCarrier carrier = new ContextCarrier(); - carrier.deserialize("trace_id_A|100|REMOTE_APP|10.2.3.16:8080"); + carrier.deserialize("trace_id_A|100|REMOTE_APP|10.2.3.16:8080|Trace.global.id.123,Trace.global.id.222"); Assert.assertEquals("trace_id_A", carrier.getTraceSegmentId()); Assert.assertEquals(100, carrier.getSpanId()); Assert.assertEquals("REMOTE_APP", carrier.getApplicationCode()); Assert.assertEquals("10.2.3.16:8080", carrier.getPeerHost()); + Assert.assertEquals("Trace.global.id.123", carrier.getDistributedTraceIds().get(0).get()); + Assert.assertEquals("Trace.global.id.222", carrier.getDistributedTraceIds().get(1).get()); } @Test @@ -49,6 +58,10 @@ public class ContextCarrierTestCase { carrier = new ContextCarrier(); carrier.deserialize("trace_id|100|REMOTE_APP|10.2.3.16:8080"); + Assert.assertFalse(carrier.isValid()); + + carrier = new ContextCarrier(); + carrier.deserialize("trace_id|100|REMOTE_APP|10.2.3.16:8080|Trace.global.id.123,Trace.global.id.222"); Assert.assertTrue(carrier.isValid()); } } diff --git a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/context/TracerContextTestCase.java b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/context/TracerContextTestCase.java index cd8478f51..4e0b69723 100644 --- a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/context/TracerContextTestCase.java +++ b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/context/TracerContextTestCase.java @@ -1,8 +1,12 @@ package com.a.eye.skywalking.api.context; import com.a.eye.skywalking.trace.Span; +import com.a.eye.skywalking.trace.TraceId.DistributedTraceId; +import com.a.eye.skywalking.trace.TraceId.PropagatedTraceId; import com.a.eye.skywalking.trace.TraceSegment; import com.a.eye.skywalking.trace.tag.Tags; +import java.util.LinkedList; +import java.util.List; import org.junit.After; import org.junit.Assert; import org.junit.Test; @@ -73,6 +77,9 @@ public class TracerContextTestCase { carrier.setSpanId(5); carrier.setApplicationCode("REMOTE_APP"); carrier.setPeerHost("10.2.3.16:8080"); + List ids = new LinkedList<>(); + ids.add(new PropagatedTraceId("Trace.global.id.123")); + carrier.setDistributedTraceIds(ids); Assert.assertTrue(carrier.isValid()); diff --git a/skywalking-sniffer/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/a/eye/skywalking/plugin/dubbo/DubboInterceptorTest.java b/skywalking-sniffer/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/a/eye/skywalking/plugin/dubbo/DubboInterceptorTest.java index bdd735f87..44812bd44 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/a/eye/skywalking/plugin/dubbo/DubboInterceptorTest.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/a/eye/skywalking/plugin/dubbo/DubboInterceptorTest.java @@ -1,6 +1,8 @@ package com.a.eye.skywalking.plugin.dubbo; import com.a.eye.skywalking.api.boot.ServiceManager; +import com.a.eye.skywalking.api.conf.Config; +import com.a.eye.skywalking.api.context.ContextCarrier; import com.a.eye.skywalking.api.context.TracerContext; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; @@ -21,6 +23,7 @@ import com.alibaba.dubbo.rpc.RpcContext; import org.hamcrest.CoreMatchers; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -31,6 +34,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when; @@ -76,6 +80,7 @@ public class DubboInterceptorTest { Mockito.when(RpcContext.getContext()).thenReturn(rpcContext); when(rpcContext.isConsumerSide()).thenReturn(true); when(methodInvokeContext.allArguments()).thenReturn(new Object[]{invoker, invocation}); + Config.SkyWalking.APPLICATION_CODE = "DubboTestCases-APP"; } @@ -92,7 +97,11 @@ public class DubboInterceptorTest { public void call(TraceSegment traceSegment) { assertThat(traceSegment.getSpans().size(), is(1)); assertConsumerSpan(traceSegment.getSpans().get(0)); - testParam.assertSelf("127.0.0.1"); + + assertNotNull(testParam.getTraceContext()); + ContextCarrier contextCarrier = new ContextCarrier(); + contextCarrier.deserialize(testParam.getTraceContext()); + Assert.assertTrue(contextCarrier.isValid()); } }); } @@ -146,7 +155,7 @@ public class DubboInterceptorTest { @Test public void testProviderWithAttachment() { when(rpcContext.isConsumerSide()).thenReturn(false); - when(rpcContext.getAttachment(DubboInterceptor.ATTACHMENT_NAME_OF_CONTEXT_DATA)).thenReturn("302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1"); + when(rpcContext.getAttachment(DubboInterceptor.ATTACHMENT_NAME_OF_CONTEXT_DATA)).thenReturn("302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1|Trace.globalId.123"); dubboInterceptor.beforeMethod(classInstanceContext, methodInvokeContext, methodInterceptResult); dubboInterceptor.afterMethod(classInstanceContext, methodInvokeContext, result); @@ -159,7 +168,7 @@ public class DubboInterceptorTest { when(rpcContext.isConsumerSide()).thenReturn(false); when(BugFixActive.isActive()).thenReturn(true); - testParam.setTraceContext("302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1"); + testParam.setTraceContext("302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1|Trace.globalId.123"); dubboInterceptor.beforeMethod(classInstanceContext, methodInvokeContext, methodInterceptResult); diff --git a/skywalking-sniffer/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/a/eye/skywalking/plugin/dubbo/RequestParamForTestBelow283.java b/skywalking-sniffer/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/a/eye/skywalking/plugin/dubbo/RequestParamForTestBelow283.java index ff259fb8a..a416dd850 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/a/eye/skywalking/plugin/dubbo/RequestParamForTestBelow283.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/dubbo-plugin/src/test/java/com/a/eye/skywalking/plugin/dubbo/RequestParamForTestBelow283.java @@ -11,12 +11,4 @@ import static org.junit.Assert.assertThat; */ public class RequestParamForTestBelow283 extends SWBaseBean { - /** - * This method assert that {@link SWBaseBean#getTraceContext()} if it's not null and context data - * will end with the expect span id. - */ - public void assertSelf(String expectHost) { - assertNotNull(getTraceContext()); - assertThat(getTraceContext(), endsWith(expectHost)); - } } diff --git a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/test/java/com/a/eye/skywalking/plugin/motan/MotanProviderInterceptorTest.java b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/test/java/com/a/eye/skywalking/plugin/motan/MotanProviderInterceptorTest.java index 16c473f79..360c871b2 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/test/java/com/a/eye/skywalking/plugin/motan/MotanProviderInterceptorTest.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/motan-plugin/src/test/java/com/a/eye/skywalking/plugin/motan/MotanProviderInterceptorTest.java @@ -91,7 +91,7 @@ public class MotanProviderInterceptorTest { @Test public void testInvokerWithRefSegment() { HashMap attachments = new HashMap(); - attachments.put("SWTraceContext", "302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1"); + attachments.put("SWTraceContext", "302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1|Trace.globalId.123"); when(request.getAttachments()).thenReturn(attachments); invokeInterceptor.beforeMethod(instanceContext, interceptorContext, null); @@ -172,4 +172,4 @@ public class MotanProviderInterceptorTest { public void tearDown() { TracerContext.ListenerManager.remove(contextListener); } -} \ No newline at end of file +} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/src/test/java/com/a/eye/skywalking/plugin/tomcat78x/TomcatInterceptorTest.java b/skywalking-sniffer/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/src/test/java/com/a/eye/skywalking/plugin/tomcat78x/TomcatInterceptorTest.java index e6282b4f2..0f4ce83f8 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/src/test/java/com/a/eye/skywalking/plugin/tomcat78x/TomcatInterceptorTest.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/src/test/java/com/a/eye/skywalking/plugin/tomcat78x/TomcatInterceptorTest.java @@ -81,7 +81,7 @@ public class TomcatInterceptorTest { @Test public void testWithSerializedContextData() { - when(request.getHeader(TomcatInterceptor.HEADER_NAME_OF_CONTEXT_DATA)).thenReturn("302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1"); + when(request.getHeader(TomcatInterceptor.HEADER_NAME_OF_CONTEXT_DATA)).thenReturn("302017.1487666919810.624424584.17332.1.1|1|REMOTE_APP|127.0.0.1|Trace.globalId.123"); tomcatInterceptor.beforeMethod(classInstanceContext, methodInvokeContext, methodInterceptResult); tomcatInterceptor.afterMethod(classInstanceContext, methodInvokeContext, null); -- GitLab