Alter TraceSegement, Add a list of DistributedTraceId as field `relatedGlobalTraces`.
This commit is contained in:
parent
5781437984
commit
5c369aefb4
|
|
@ -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<Integer> ThreadTraceIdSequence = new ThreadLocal<Integer>() {
|
||||
@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由以下规则组成<br/>
|
||||
* version号 + 1位时间戳(毫秒数) + 1位进程随机号(UUID后7位) + 1位进程数号 + 1位线程号 + 1位线程内序号
|
||||
* ID类型 + 1位时间戳(毫秒数) + 1位进程随机号(UUID后7位) + 1位进程数号 + 1位线程号 + 1位线程内序号
|
||||
* <p>
|
||||
* 注意:这里的位,是指“.”作为分隔符所占的位数,非字符串长度的位数。
|
||||
* 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 + "");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
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));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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<SegmentMessage> {
|
||||
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<SegmentMessage> {
|
|||
*/
|
||||
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.
|
||||
* 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<Span>();
|
||||
this.relatedGlobalTraces = new LinkedList<DistributedTraceId>();
|
||||
this.relatedGlobalTraces.add(new NewDistributedTraceId());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -95,11 +116,11 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
|
|||
* @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<SegmentMessage> {
|
|||
*
|
||||
* @param refSegment {@link TraceSegmentRef}
|
||||
*/
|
||||
public void ref(TraceSegmentRef refSegment){
|
||||
public void ref(TraceSegmentRef refSegment) {
|
||||
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,
|
||||
* notify the {@link TraceSegment} to archive it.
|
||||
|
|
@ -158,12 +189,16 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
|
|||
}
|
||||
|
||||
public List<TraceSegmentRef> getRefs() {
|
||||
if(refs == null){
|
||||
if (refs == null) {
|
||||
return null;
|
||||
}
|
||||
return Collections.unmodifiableList(refs);
|
||||
}
|
||||
|
||||
public List<DistributedTraceId> getRelatedGlobalTraces() {
|
||||
return Collections.unmodifiableList(relatedGlobalTraces);
|
||||
}
|
||||
|
||||
public List<Span> getSpans() {
|
||||
return Collections.unmodifiableList(spans);
|
||||
}
|
||||
|
|
@ -189,14 +224,17 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
|
|||
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<SegmentMessage> {
|
|||
endTime = message.getEndTime();
|
||||
applicationCode = message.getApplicationCode();
|
||||
SegmentRefMessage messagePrimaryRef = message.getPrimaryRef();
|
||||
if(messagePrimaryRef != null) {
|
||||
if (messagePrimaryRef != null) {
|
||||
(primaryRef = new TraceSegmentRef()).deserialize(messagePrimaryRef);
|
||||
}
|
||||
List<SegmentRefMessage> refsList = message.getRefsList();
|
||||
|
|
@ -222,6 +260,11 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
|
|||
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();
|
||||
if (spansList != null) {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<DistributedTraceId> 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<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;
|
|||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<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
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<DistributedTraceId> ids = new LinkedList<>();
|
||||
ids.add(new PropagatedTraceId("Trace.global.id.123"));
|
||||
carrier.setDistributedTraceIds(ids);
|
||||
|
||||
Assert.assertTrue(carrier.isValid());
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue