提交 5c369aef 编写于 作者: wu-sheng's avatar wu-sheng

Alter TraceSegement, Add a list of DistributedTraceId as field `relatedGlobalTraces`.

上级 57814379
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; import java.util.UUID;
public final class TraceIdGenerator { public final class GlobalIdGenerator {
private static final ThreadLocal<Integer> ThreadTraceIdSequence = new ThreadLocal<Integer>() { private static final ThreadLocal<Integer> ThreadTraceIdSequence = new ThreadLocal<Integer>() {
@Override @Override
protected Integer initialValue() { protected Integer initialValue() {
...@@ -18,25 +19,25 @@ public final class TraceIdGenerator { ...@@ -18,25 +19,25 @@ public final class TraceIdGenerator {
PROCESS_UUID = uuid.substring(uuid.length() - 7).hashCode(); PROCESS_UUID = uuid.substring(uuid.length() - 7).hashCode();
} }
private TraceIdGenerator() { private GlobalIdGenerator() {
} }
/** /**
* TraceId由以下规则组成<br/> * TraceId由以下规则组成<br/>
* version号 + 1位时间戳(毫秒数) + 1位进程随机号(UUID后7位) + 1位进程数号 + 1位线程号 + 1位线程内序号 * ID类型 + 1位时间戳(毫秒数) + 1位进程随机号(UUID后7位) + 1位进程数号 + 1位线程号 + 1位线程内序号
* <p> * <p>
* 注意:这里的位,是指“.”作为分隔符所占的位数,非字符串长度的位数。 * 注意:这里的位,是指“.”作为分隔符所占的位数,非字符串长度的位数。
* TraceId为6个片段组成的数组 * TraceId为6个片段组成的数组
* *
* @return * @return
*/ */
public static String generate() { public static String generate(String type) {
Integer seq = ThreadTraceIdSequence.get(); Integer seq = ThreadTraceIdSequence.get();
seq++; seq++;
ThreadTraceIdSequence.set(seq); ThreadTraceIdSequence.set(seq);
return StringUtil.join('.', return StringUtil.join('.',
Constants.SDK_VERSION + "", System.currentTimeMillis() + "", PROCESS_UUID + "", type + "", System.currentTimeMillis() + "", PROCESS_UUID + "",
MachineInfo.getProcessNo() + "", Thread.currentThread().getId() + "", seq + ""); MachineInfo.getProcessNo() + "", Thread.currentThread().getId() + "", seq + "");
} }
} }
package com.a.eye.skywalking.trace.TraceId;
/**
* The <code>DistributedTraceId</code> 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 <code>DistributedTraceId</code> even in different JVM.
*
* The <code>DistributedTraceId</code> 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;
}
}
package com.a.eye.skywalking.trace.TraceId;
import com.a.eye.skywalking.trace.GlobalIdGenerator;
/**
* The <code>NewDistributedTraceId</code> 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));
}
}
package com.a.eye.skywalking.trace.TraceId;
/**
* The <code>PropagatedTraceId</code> represents a {@link DistributedTraceId}, which is propagated from the peer.
*
* @author wusheng
*/
public class PropagatedTraceId extends DistributedTraceId {
public PropagatedTraceId(String id) {
super(id);
}
}
package com.a.eye.skywalking.trace; package com.a.eye.skywalking.trace;
import com.a.eye.skywalking.messages.ISerializable; 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.SegmentMessage;
import com.a.eye.skywalking.trace.proto.SegmentRefMessage; import com.a.eye.skywalking.trace.proto.SegmentRefMessage;
import com.a.eye.skywalking.trace.proto.SpanMessage; import com.a.eye.skywalking.trace.proto.SpanMessage;
import com.google.protobuf.ProtocolStringList;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
...@@ -18,6 +22,8 @@ import java.util.List; ...@@ -18,6 +22,8 @@ import java.util.List;
* Created by wusheng on 2017/2/17. * Created by wusheng on 2017/2/17.
*/ */
public class TraceSegment implements ISerializable<SegmentMessage> { public class TraceSegment implements ISerializable<SegmentMessage> {
private static final String ID_TYPE = "Segment";
/** /**
* The id of this trace segment. * The id of this trace segment.
* Every segment has its unique-global-id. * Every segment has its unique-global-id.
...@@ -63,17 +69,32 @@ public class TraceSegment implements ISerializable<SegmentMessage> { ...@@ -63,17 +69,32 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
*/ */
private String applicationCode; private String applicationCode;
/**
* The <code>relatedGlobalTraces</code> 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 <code>relatedGlobalTraces</code> and {@link #primaryRef}/{@link #refs} is: {@link
* #primaryRef}/{@link #refs} targets this {@link TraceSegment}'s direct parent,
*
* and
*
* <code>relatedGlobalTraces</code> 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<DistributedTraceId> relatedGlobalTraces;
/** /**
* Create a trace segment, by given segmentId. * Create a trace segment, by given segmentId.
* This segmentId is generated by TraceSegmentRef, AKA, from tracer/agent module. * This segmentId is generated by TraceSegmentRef, AKA, from tracer/agent module.
*
* @param segmentId {@link #traceSegmentId}
*/ */
public TraceSegment(String segmentId, String applicationCode) { public TraceSegment(String applicationCode) {
this.traceSegmentId = segmentId; this.traceSegmentId = GlobalIdGenerator.generate(ID_TYPE);
this.applicationCode = applicationCode; this.applicationCode = applicationCode;
this.startTime = System.currentTimeMillis(); this.startTime = System.currentTimeMillis();
this.spans = new LinkedList<Span>(); this.spans = new LinkedList<Span>();
this.relatedGlobalTraces = new LinkedList<DistributedTraceId>();
this.relatedGlobalTraces.add(new NewDistributedTraceId());
} }
/** /**
...@@ -95,11 +116,11 @@ public class TraceSegment implements ISerializable<SegmentMessage> { ...@@ -95,11 +116,11 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
* @param primaryOnly if true, set {@param refSegment} to {@link #primaryRef} only. * @param primaryOnly if true, set {@param refSegment} to {@link #primaryRef} only.
*/ */
public void ref(TraceSegmentRef refSegment, boolean primaryOnly) { public void ref(TraceSegmentRef refSegment, boolean primaryOnly) {
if(primaryOnly){ if (primaryOnly) {
if (primaryRef == null) { if (primaryRef == null) {
primaryRef = refSegment; primaryRef = refSegment;
} }
}else { } else {
if (primaryRef == null) { if (primaryRef == null) {
primaryRef = refSegment; primaryRef = refSegment;
} else { } else {
...@@ -117,10 +138,20 @@ public class TraceSegment implements ISerializable<SegmentMessage> { ...@@ -117,10 +138,20 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
* *
* @param refSegment {@link TraceSegmentRef} * @param refSegment {@link TraceSegmentRef}
*/ */
public void ref(TraceSegmentRef refSegment){ public void ref(TraceSegmentRef refSegment) {
ref(refSegment, true); ref(refSegment, true);
} }
public void buildRelation(List<DistributedTraceId> 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, * After {@link Span} is finished, as be controller by "skywalking-api" module,
* notify the {@link TraceSegment} to archive it. * notify the {@link TraceSegment} to archive it.
...@@ -158,12 +189,16 @@ public class TraceSegment implements ISerializable<SegmentMessage> { ...@@ -158,12 +189,16 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
} }
public List<TraceSegmentRef> getRefs() { public List<TraceSegmentRef> getRefs() {
if(refs == null){ if (refs == null) {
return null; return null;
} }
return Collections.unmodifiableList(refs); return Collections.unmodifiableList(refs);
} }
public List<DistributedTraceId> getRelatedGlobalTraces() {
return Collections.unmodifiableList(relatedGlobalTraces);
}
public List<Span> getSpans() { public List<Span> getSpans() {
return Collections.unmodifiableList(spans); return Collections.unmodifiableList(spans);
} }
...@@ -189,14 +224,17 @@ public class TraceSegment implements ISerializable<SegmentMessage> { ...@@ -189,14 +224,17 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
segmentBuilder.setStartTime(startTime); segmentBuilder.setStartTime(startTime);
segmentBuilder.setEndTime(endTime); segmentBuilder.setEndTime(endTime);
segmentBuilder.setApplicationCode(applicationCode); segmentBuilder.setApplicationCode(applicationCode);
if(primaryRef != null) { if (primaryRef != null) {
segmentBuilder.setPrimaryRef(primaryRef.serialize()); segmentBuilder.setPrimaryRef(primaryRef.serialize());
} }
if(refs != null && refs.size() > 0) { if (refs != null && refs.size() > 0) {
for (TraceSegmentRef ref : refs) { for (TraceSegmentRef ref : refs) {
segmentBuilder.addRefs(ref.serialize()); segmentBuilder.addRefs(ref.serialize());
} }
} }
for (DistributedTraceId id : relatedGlobalTraces) {
segmentBuilder.addRelatedTraceIds(id.get());
}
for (Span span : spans) { for (Span span : spans) {
segmentBuilder.addSpans(span.serialize()); segmentBuilder.addSpans(span.serialize());
} }
...@@ -210,7 +248,7 @@ public class TraceSegment implements ISerializable<SegmentMessage> { ...@@ -210,7 +248,7 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
endTime = message.getEndTime(); endTime = message.getEndTime();
applicationCode = message.getApplicationCode(); applicationCode = message.getApplicationCode();
SegmentRefMessage messagePrimaryRef = message.getPrimaryRef(); SegmentRefMessage messagePrimaryRef = message.getPrimaryRef();
if(messagePrimaryRef != null) { if (messagePrimaryRef != null) {
(primaryRef = new TraceSegmentRef()).deserialize(messagePrimaryRef); (primaryRef = new TraceSegmentRef()).deserialize(messagePrimaryRef);
} }
List<SegmentRefMessage> refsList = message.getRefsList(); List<SegmentRefMessage> refsList = message.getRefsList();
...@@ -222,6 +260,11 @@ public class TraceSegment implements ISerializable<SegmentMessage> { ...@@ -222,6 +260,11 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
refs.add(ref); refs.add(ref);
} }
} }
ProtocolStringList relatedTraceIdsList = message.getRelatedTraceIdsList();
this.relatedGlobalTraces = new LinkedList<DistributedTraceId>();
for (String id : relatedTraceIdsList) {
relatedGlobalTraces.add(new PropagatedTraceId(id));
}
List<SpanMessage> spansList = message.getSpansList(); List<SpanMessage> spansList = message.getSpansList();
if (spansList != null) { if (spansList != null) {
......
...@@ -10,7 +10,8 @@ message SegmentMessage { ...@@ -10,7 +10,8 @@ message SegmentMessage {
string applicationCode = 4; string applicationCode = 4;
SegmentRefMessage primaryRef = 5; SegmentRefMessage primaryRef = 5;
repeated SegmentRefMessage refs = 6; repeated SegmentRefMessage refs = 6;
repeated SpanMessage spans = 7; repeated string relatedTraceIds = 7;
repeated SpanMessage spans = 8;
} }
message SegmentRefMessage { message SegmentRefMessage {
......
...@@ -25,7 +25,7 @@ public class SpanTestCase { ...@@ -25,7 +25,7 @@ public class SpanTestCase {
@Test @Test
public void testFinish() { public void testFinish() {
TraceSegment owner = new TraceSegment("trace_1", "billing_app"); TraceSegment owner = new TraceSegment("billing_app");
Span span1 = new Span(0, "serviceA"); Span span1 = new Span(0, "serviceA");
......
...@@ -10,16 +10,16 @@ import org.junit.Test; ...@@ -10,16 +10,16 @@ import org.junit.Test;
public class TraceSegmentTestCase { public class TraceSegmentTestCase {
@Test @Test
public void testConstructor() { 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.assertTrue(segment.getStartTime() > 0);
Assert.assertEquals("billing_app", segment.getApplicationCode()); Assert.assertEquals("billing_app", segment.getApplicationCode());
} }
@Test @Test
public void testRef() { public void testRef() {
TraceSegment segment = new TraceSegment("trace_3", "billing_app"); TraceSegment segment = new TraceSegment("billing_app");
TraceSegmentRef ref1 = new TraceSegmentRef(); TraceSegmentRef ref1 = new TraceSegmentRef();
ref1.setTraceSegmentId("parent_trace_0"); ref1.setTraceSegmentId("parent_trace_0");
...@@ -46,7 +46,7 @@ public class TraceSegmentTestCase { ...@@ -46,7 +46,7 @@ public class TraceSegmentTestCase {
@Test @Test
public void testArchiveSpan() { public void testArchiveSpan() {
TraceSegment segment = new TraceSegment("trace_1", "billing_app"); TraceSegment segment = new TraceSegment("billing_app");
Span span1 = new Span(1, "/serviceA"); Span span1 = new Span(1, "/serviceA");
segment.archive(span1); segment.archive(span1);
...@@ -59,7 +59,7 @@ public class TraceSegmentTestCase { ...@@ -59,7 +59,7 @@ public class TraceSegmentTestCase {
@Test @Test
public void testFinish() { public void testFinish() {
TraceSegment segment = new TraceSegment("trace_1", "billing_app"); TraceSegment segment = new TraceSegment("billing_app");
Assert.assertTrue(segment.getEndTime() == 0); Assert.assertTrue(segment.getEndTime() == 0);
segment.finish(); segment.finish();
...@@ -68,7 +68,7 @@ public class TraceSegmentTestCase { ...@@ -68,7 +68,7 @@ public class TraceSegmentTestCase {
@Test @Test
public void testSerialize() { public void testSerialize() {
TraceSegment segment = new TraceSegment("trace_3", "billing_app"); TraceSegment segment = new TraceSegment("billing_app");
TraceSegmentRef ref1 = new TraceSegmentRef(); TraceSegmentRef ref1 = new TraceSegmentRef();
ref1.setTraceSegmentId("parent_trace_0"); ref1.setTraceSegmentId("parent_trace_0");
......
package com.a.eye.skywalking.api.conf; 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 { public class Constants {
/** /**
* This is the version, which will be the first segment of traceid. * 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"; public final static String SDK_VERSION = "302017";
} }
package com.a.eye.skywalking.api.context; package com.a.eye.skywalking.api.context;
import com.a.eye.skywalking.trace.Span; 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.TraceSegment;
import com.a.eye.skywalking.api.util.StringUtil; import com.a.eye.skywalking.api.util.StringUtil;
import com.a.eye.skywalking.trace.tag.Tags; import com.a.eye.skywalking.trace.tag.Tags;
import java.io.Serializable; import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
/** /**
* {@link ContextCarrier} is a data carrier of {@link TracerContext}. * {@link ContextCarrier} is a data carrier of {@link TracerContext}.
...@@ -33,6 +37,11 @@ public class ContextCarrier implements Serializable { ...@@ -33,6 +37,11 @@ public class ContextCarrier implements Serializable {
*/ */
private String peerHost; private String peerHost;
/**
* {@link DistributedTraceId}
*/
private List<DistributedTraceId> distributedTraceIds;
/** /**
* Serialize this {@link ContextCarrier} to a {@link String}, * Serialize this {@link ContextCarrier} to a {@link String},
* with '|' split. * with '|' split.
...@@ -40,7 +49,12 @@ public class ContextCarrier implements Serializable { ...@@ -40,7 +49,12 @@ public class ContextCarrier implements Serializable {
* @return the serialization string. * @return the serialization string.
*/ */
public String serialize() { 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 { ...@@ -49,15 +63,16 @@ public class ContextCarrier implements Serializable {
* @param text carries {@link #traceSegmentId} and {@link #spanId}, with '|' split. * @param text carries {@link #traceSegmentId} and {@link #spanId}, with '|' split.
*/ */
public ContextCarrier deserialize(String text) { public ContextCarrier deserialize(String text) {
if(text != null){ if (text != null) {
String[] parts = text.split("\\|", 4); String[] parts = text.split("\\|", 5);
if(parts.length == 4){ if (parts.length == 5) {
try{ try {
setSpanId(Integer.parseInt(parts[1])); setSpanId(Integer.parseInt(parts[1]));
setTraceSegmentId(parts[0]); setTraceSegmentId(parts[0]);
setApplicationCode(parts[2]); setApplicationCode(parts[2]);
setPeerHost(parts[3]); setPeerHost(parts[3]);
}catch(NumberFormatException e){ setDistributedTraceIds(deserializeDistributedTraceIds(parts[4]));
} catch (NumberFormatException e) {
} }
} }
...@@ -70,8 +85,12 @@ public class ContextCarrier implements Serializable { ...@@ -70,8 +85,12 @@ public class ContextCarrier implements Serializable {
* *
* @return true for unbroken {@link ContextCarrier} or no-initialized. Otherwise, false; * @return true for unbroken {@link ContextCarrier} or no-initialized. Otherwise, false;
*/ */
public boolean isValid(){ public boolean isValid() {
return !StringUtil.isEmpty(traceSegmentId) && getSpanId() > -1 && !StringUtil.isEmpty(applicationCode) && !StringUtil.isEmpty(peerHost); return !StringUtil.isEmpty(traceSegmentId)
&& getSpanId() > -1
&& !StringUtil.isEmpty(applicationCode)
&& !StringUtil.isEmpty(peerHost)
&& distributedTraceIds != null;
} }
public String getTraceSegmentId() { public String getTraceSegmentId() {
...@@ -105,4 +124,52 @@ public class ContextCarrier implements Serializable { ...@@ -105,4 +124,52 @@ public class ContextCarrier implements Serializable {
public void setPeerHost(String peerHost) { public void setPeerHost(String peerHost) {
this.peerHost = peerHost; this.peerHost = peerHost;
} }
public List<DistributedTraceId> getDistributedTraceIds() {
return distributedTraceIds;
}
public void setDistributedTraceIds(List<DistributedTraceId> 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<DistributedTraceId> deserializeDistributedTraceIds(String text) {
if (StringUtil.isEmpty(text)) {
return null;
}
String[] propagationTraceIdValues = text.split(",");
List<DistributedTraceId> traceIds = new LinkedList<>();
for (String propagationTraceIdValue : propagationTraceIdValues) {
traceIds.add(new PropagatedTraceId(propagationTraceIdValue));
}
return traceIds;
}
} }
...@@ -3,7 +3,6 @@ package com.a.eye.skywalking.api.context; ...@@ -3,7 +3,6 @@ package com.a.eye.skywalking.api.context;
import com.a.eye.skywalking.api.conf.Config; import com.a.eye.skywalking.api.conf.Config;
import com.a.eye.skywalking.trace.Span; import com.a.eye.skywalking.trace.Span;
import com.a.eye.skywalking.trace.TraceSegment; 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.TraceSegmentRef;
import com.a.eye.skywalking.trace.tag.Tags; import com.a.eye.skywalking.trace.tag.Tags;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -35,7 +34,7 @@ public final class TracerContext { ...@@ -35,7 +34,7 @@ public final class TracerContext {
* Create a {@link TraceSegment} and init {@link #spanIdGenerator} as 0; * Create a {@link TraceSegment} and init {@link #spanIdGenerator} as 0;
*/ */
TracerContext() { TracerContext() {
this.segment = new TraceSegment(TraceIdGenerator.generate(), Config.SkyWalking.APPLICATION_CODE); this.segment = new TraceSegment(Config.SkyWalking.APPLICATION_CODE);
this.spanIdGenerator = 0; this.spanIdGenerator = 0;
} }
...@@ -126,6 +125,7 @@ public final class TracerContext { ...@@ -126,6 +125,7 @@ public final class TracerContext {
carrier.setSpanId(this.activeSpan().getSpanId()); carrier.setSpanId(this.activeSpan().getSpanId());
carrier.setApplicationCode(Config.SkyWalking.APPLICATION_CODE); carrier.setApplicationCode(Config.SkyWalking.APPLICATION_CODE);
carrier.setPeerHost(Tags.PEER_HOST.get(activeSpan())); carrier.setPeerHost(Tags.PEER_HOST.get(activeSpan()));
carrier.setDistributedTraceIds(this.segment.getRelatedGlobalTraces());
} }
/** /**
...@@ -137,6 +137,7 @@ public final class TracerContext { ...@@ -137,6 +137,7 @@ public final class TracerContext {
public void extract(ContextCarrier carrier) { public void extract(ContextCarrier carrier) {
if(carrier.isValid()) { if(carrier.isValid()) {
this.segment.ref(getRef(carrier)); this.segment.ref(getRef(carrier));
this.segment.buildRelation(carrier.getDistributedTraceIds());
} }
} }
......
package com.a.eye.skywalking.api.context; 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.Assert;
import org.junit.Test; import org.junit.Test;
...@@ -14,19 +18,24 @@ public class ContextCarrierTestCase { ...@@ -14,19 +18,24 @@ public class ContextCarrierTestCase {
carrier.setSpanId(100); carrier.setSpanId(100);
carrier.setApplicationCode("REMOTE_APP"); carrier.setApplicationCode("REMOTE_APP");
carrier.setPeerHost("10.2.3.16:8080"); carrier.setPeerHost("10.2.3.16:8080");
List<DistributedTraceId> 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 @Test
public void testDeserialize(){ public void testDeserialize(){
ContextCarrier carrier = new ContextCarrier(); 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("trace_id_A", carrier.getTraceSegmentId());
Assert.assertEquals(100, carrier.getSpanId()); Assert.assertEquals(100, carrier.getSpanId());
Assert.assertEquals("REMOTE_APP", carrier.getApplicationCode()); Assert.assertEquals("REMOTE_APP", carrier.getApplicationCode());
Assert.assertEquals("10.2.3.16:8080", carrier.getPeerHost()); 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 @Test
...@@ -49,6 +58,10 @@ public class ContextCarrierTestCase { ...@@ -49,6 +58,10 @@ public class ContextCarrierTestCase {
carrier = new ContextCarrier(); carrier = new ContextCarrier();
carrier.deserialize("trace_id|100|REMOTE_APP|10.2.3.16:8080"); 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()); Assert.assertTrue(carrier.isValid());
} }
} }
package com.a.eye.skywalking.api.context; package com.a.eye.skywalking.api.context;
import com.a.eye.skywalking.trace.Span; 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.TraceSegment;
import com.a.eye.skywalking.trace.tag.Tags; import com.a.eye.skywalking.trace.tag.Tags;
import java.util.LinkedList;
import java.util.List;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
...@@ -73,6 +77,9 @@ public class TracerContextTestCase { ...@@ -73,6 +77,9 @@ public class TracerContextTestCase {
carrier.setSpanId(5); carrier.setSpanId(5);
carrier.setApplicationCode("REMOTE_APP"); carrier.setApplicationCode("REMOTE_APP");
carrier.setPeerHost("10.2.3.16:8080"); carrier.setPeerHost("10.2.3.16:8080");
List<DistributedTraceId> ids = new LinkedList<>();
ids.add(new PropagatedTraceId("Trace.global.id.123"));
carrier.setDistributedTraceIds(ids);
Assert.assertTrue(carrier.isValid()); Assert.assertTrue(carrier.isValid());
......
package com.a.eye.skywalking.plugin.dubbo; package com.a.eye.skywalking.plugin.dubbo;
import com.a.eye.skywalking.api.boot.ServiceManager; 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.context.TracerContext;
import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext;
import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext;
...@@ -21,6 +23,7 @@ import com.alibaba.dubbo.rpc.RpcContext; ...@@ -21,6 +23,7 @@ import com.alibaba.dubbo.rpc.RpcContext;
import org.hamcrest.CoreMatchers; import org.hamcrest.CoreMatchers;
import org.junit.After; import org.junit.After;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -31,6 +34,7 @@ import org.powermock.modules.junit4.PowerMockRunner; ...@@ -31,6 +34,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when; import static org.powermock.api.mockito.PowerMockito.when;
...@@ -76,6 +80,7 @@ public class DubboInterceptorTest { ...@@ -76,6 +80,7 @@ public class DubboInterceptorTest {
Mockito.when(RpcContext.getContext()).thenReturn(rpcContext); Mockito.when(RpcContext.getContext()).thenReturn(rpcContext);
when(rpcContext.isConsumerSide()).thenReturn(true); when(rpcContext.isConsumerSide()).thenReturn(true);
when(methodInvokeContext.allArguments()).thenReturn(new Object[]{invoker, invocation}); when(methodInvokeContext.allArguments()).thenReturn(new Object[]{invoker, invocation});
Config.SkyWalking.APPLICATION_CODE = "DubboTestCases-APP";
} }
...@@ -92,7 +97,11 @@ public class DubboInterceptorTest { ...@@ -92,7 +97,11 @@ public class DubboInterceptorTest {
public void call(TraceSegment traceSegment) { public void call(TraceSegment traceSegment) {
assertThat(traceSegment.getSpans().size(), is(1)); assertThat(traceSegment.getSpans().size(), is(1));
assertConsumerSpan(traceSegment.getSpans().get(0)); 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 { ...@@ -146,7 +155,7 @@ public class DubboInterceptorTest {
@Test @Test
public void testProviderWithAttachment() { public void testProviderWithAttachment() {
when(rpcContext.isConsumerSide()).thenReturn(false); 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.beforeMethod(classInstanceContext, methodInvokeContext, methodInterceptResult);
dubboInterceptor.afterMethod(classInstanceContext, methodInvokeContext, result); dubboInterceptor.afterMethod(classInstanceContext, methodInvokeContext, result);
...@@ -159,7 +168,7 @@ public class DubboInterceptorTest { ...@@ -159,7 +168,7 @@ public class DubboInterceptorTest {
when(rpcContext.isConsumerSide()).thenReturn(false); when(rpcContext.isConsumerSide()).thenReturn(false);
when(BugFixActive.isActive()).thenReturn(true); 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); dubboInterceptor.beforeMethod(classInstanceContext, methodInvokeContext, methodInterceptResult);
......
...@@ -11,12 +11,4 @@ import static org.junit.Assert.assertThat; ...@@ -11,12 +11,4 @@ import static org.junit.Assert.assertThat;
*/ */
public class RequestParamForTestBelow283 extends SWBaseBean { 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));
}
} }
...@@ -91,7 +91,7 @@ public class MotanProviderInterceptorTest { ...@@ -91,7 +91,7 @@ public class MotanProviderInterceptorTest {
@Test @Test
public void testInvokerWithRefSegment() { public void testInvokerWithRefSegment() {
HashMap attachments = new HashMap(); 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); when(request.getAttachments()).thenReturn(attachments);
invokeInterceptor.beforeMethod(instanceContext, interceptorContext, null); invokeInterceptor.beforeMethod(instanceContext, interceptorContext, null);
......
...@@ -81,7 +81,7 @@ public class TomcatInterceptorTest { ...@@ -81,7 +81,7 @@ public class TomcatInterceptorTest {
@Test @Test
public void testWithSerializedContextData() { 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.beforeMethod(classInstanceContext, methodInvokeContext, methodInterceptResult);
tomcatInterceptor.afterMethod(classInstanceContext, methodInvokeContext, null); tomcatInterceptor.afterMethod(classInstanceContext, methodInvokeContext, null);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册