提交 37cc5a33 编写于 作者: wu-sheng's avatar wu-sheng

Alter ContextCarrier, add applicationCode and peerHost

上级 5902154a
......@@ -2,6 +2,7 @@ package com.a.eye.skywalking.trace;
import com.a.eye.skywalking.messages.ISerializable;
import com.a.eye.skywalking.trace.messages.proto.SegmentRefMessage;
import com.a.eye.skywalking.trace.tag.Tags;
/**
* {@link TraceSegmentRef} is like a pointer, which ref to another {@link TraceSegment},
......@@ -20,6 +21,16 @@ public class TraceSegmentRef implements ISerializable<SegmentRefMessage> {
*/
private int spanId = -1;
/**
* {@link TraceSegment#applicationCode}
*/
private String applicationCode;
/**
* {@link Tags#PEER_HOST}
*/
private String peerHost;
/**
* Create a {@link TraceSegmentRef} instance, without any data.
*/
......@@ -42,11 +53,28 @@ public class TraceSegmentRef implements ISerializable<SegmentRefMessage> {
this.spanId = spanId;
}
@Override
public String toString() {
public String getApplicationCode() {
return applicationCode;
}
public void setApplicationCode(String applicationCode) {
this.applicationCode = applicationCode;
}
public String getPeerHost() {
return peerHost;
}
public void setPeerHost(String peerHost) {
this.peerHost = peerHost;
}
@Override public String toString() {
return "TraceSegmentRef{" +
"traceSegmentId='" + traceSegmentId + '\'' +
", spanId=" + spanId +
", applicationCode='" + applicationCode + '\'' +
", peerHost='" + peerHost + '\'' +
'}';
}
......@@ -55,6 +83,10 @@ public class TraceSegmentRef implements ISerializable<SegmentRefMessage> {
SegmentRefMessage.Builder builder = SegmentRefMessage.newBuilder();
builder.setTraceSegmentId(traceSegmentId);
builder.setSpanId(spanId);
builder.setApplicationCode(applicationCode);
if(peerHost != null) {
builder.setPeerHost(peerHost);
}
return builder.build();
}
......@@ -62,5 +94,7 @@ public class TraceSegmentRef implements ISerializable<SegmentRefMessage> {
public void deserialize(SegmentRefMessage message) {
traceSegmentId = message.getTraceSegmentId();
spanId = message.getSpanId();
applicationCode = message.getApplicationCode();
peerHost = message.getPeerHost();
}
}
......@@ -102,7 +102,7 @@ public final class Tags {
public static final BooleanTag ERROR = new BooleanTag("error");
/**
* PEER_HOST records host address of the peer, maybe IPV4, IPV6 or hostname.
* PEER_HOST records host address (ip:port, or ip1:port1,ip2:port2) of the peer, maybe IPV4, IPV6 or hostname.
*/
public static final StringTag PEER_HOST = new StringTag("peer.host");
......
......@@ -16,6 +16,8 @@ message SegmentMessage {
message SegmentRefMessage {
string traceSegmentId = 1;
int32 spanId = 2;
string applicationCode = 3;
string peerHost = 4;
}
message SpanMessage {
......
......@@ -73,16 +73,22 @@ public class TraceSegmentTestCase {
TraceSegmentRef ref1 = new TraceSegmentRef();
ref1.setTraceSegmentId("parent_trace_0");
ref1.setSpanId(1);
ref1.setApplicationCode("REMOTE_APP");
ref1.setPeerHost("10.2.3.16:8080");
segment.ref(ref1);
TraceSegmentRef ref2 = new TraceSegmentRef();
ref2.setTraceSegmentId("parent_trace_1");
ref2.setSpanId(5);
ref2.setApplicationCode("REMOTE_APP");
ref2.setPeerHost("10.2.3.16:8080");
segment.ref(ref2);
TraceSegmentRef ref3 = new TraceSegmentRef();
ref3.setTraceSegmentId("parent_trace_1");
ref3.setSpanId(5);
ref3.setApplicationCode("REMOTE_APP");
ref3.setPeerHost("10.2.3.16:8080");
segment.ref(ref3);
Span span1 = new Span(1, "/serviceA");
......
......@@ -2,8 +2,8 @@ package com.a.eye.skywalking.api.context;
import com.a.eye.skywalking.trace.Span;
import com.a.eye.skywalking.trace.TraceSegment;
import com.a.eye.skywalking.trace.TraceSegmentRef;
import com.a.eye.skywalking.api.util.StringUtil;
import com.a.eye.skywalking.trace.tag.Tags;
import java.io.Serializable;
/**
......@@ -23,6 +23,16 @@ public class ContextCarrier implements Serializable {
*/
private int spanId = -1;
/**
* {@link TraceSegment#applicationCode}
*/
private String applicationCode;
/**
* {@link Tags#PEER_HOST}
*/
private String peerHost;
/**
* Serialize this {@link ContextCarrier} to a {@link String},
* with '|' split.
......@@ -30,7 +40,7 @@ public class ContextCarrier implements Serializable {
* @return the serialization string.
*/
public String serialize() {
return StringUtil.join('|', this.getTraceSegmentId(), this.getSpanId() + "");
return StringUtil.join('|', this.getTraceSegmentId(), this.getSpanId() + "", this.getApplicationCode(), this.getPeerHost());
}
/**
......@@ -40,11 +50,13 @@ public class ContextCarrier implements Serializable {
*/
public ContextCarrier deserialize(String text) {
if(text != null){
String[] parts = text.split("\\|");
if(parts.length == 2){
String[] parts = text.split("\\|", 4);
if(parts.length == 4){
try{
setSpanId(Integer.parseInt(parts[1]));
setTraceSegmentId(parts[0]);
setApplicationCode(parts[2]);
setPeerHost(parts[3]);
}catch(NumberFormatException e){
}
......@@ -59,7 +71,7 @@ public class ContextCarrier implements Serializable {
* @return true for unbroken {@link ContextCarrier} or no-initialized. Otherwise, false;
*/
public boolean isValid(){
return !StringUtil.isEmpty(getTraceSegmentId()) && getSpanId() > -1;
return !StringUtil.isEmpty(traceSegmentId) && getSpanId() > -1 && !StringUtil.isEmpty(applicationCode) && !StringUtil.isEmpty(peerHost);
}
public String getTraceSegmentId() {
......@@ -77,4 +89,20 @@ public class ContextCarrier implements Serializable {
public void setSpanId(int spanId) {
this.spanId = spanId;
}
public String getApplicationCode() {
return applicationCode;
}
public void setApplicationCode(String applicationCode) {
this.applicationCode = applicationCode;
}
public String getPeerHost() {
return peerHost;
}
public void setPeerHost(String peerHost) {
this.peerHost = peerHost;
}
}
......@@ -5,6 +5,7 @@ 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;
import java.util.LinkedList;
import java.util.List;
......@@ -116,6 +117,8 @@ public final class TracerContext {
public void inject(ContextCarrier carrier) {
carrier.setTraceSegmentId(this.segment.getTraceSegmentId());
carrier.setSpanId(this.activeSpan().getSpanId());
carrier.setApplicationCode(Config.SkyWalking.APPLICATION_CODE);
carrier.setPeerHost(Tags.PEER_HOST.get(activeSpan()));
}
/**
......@@ -128,6 +131,8 @@ public final class TracerContext {
TraceSegmentRef ref = new TraceSegmentRef();
ref.setTraceSegmentId(carrier.getTraceSegmentId());
ref.setSpanId(carrier.getSpanId());
ref.setApplicationCode(carrier.getApplicationCode());
ref.setPeerHost(carrier.getPeerHost());
this.segment.ref(ref);
}
......
......@@ -12,17 +12,21 @@ public class ContextCarrierTestCase {
ContextCarrier carrier = new ContextCarrier();
carrier.setTraceSegmentId("trace_id_A");
carrier.setSpanId(100);
carrier.setApplicationCode("REMOTE_APP");
carrier.setPeerHost("10.2.3.16:8080");
Assert.assertEquals("trace_id_A|100", carrier.serialize());
Assert.assertEquals("trace_id_A|100|REMOTE_APP|10.2.3.16:8080", carrier.serialize());
}
@Test
public void testDeserialize(){
ContextCarrier carrier = new ContextCarrier();
carrier.deserialize("trace_id_A|100");
carrier.deserialize("trace_id_A|100|REMOTE_APP|10.2.3.16:8080");
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());
}
@Test
......@@ -44,7 +48,7 @@ public class ContextCarrierTestCase {
Assert.assertFalse(carrier.isValid());
carrier = new ContextCarrier();
carrier.deserialize("trace_id|100");
carrier.deserialize("trace_id|100|REMOTE_APP|10.2.3.16:8080");
Assert.assertTrue(carrier.isValid());
}
}
......@@ -2,6 +2,7 @@ package com.a.eye.skywalking.api.context;
import com.a.eye.skywalking.trace.Span;
import com.a.eye.skywalking.trace.TraceSegment;
import com.a.eye.skywalking.trace.tag.Tags;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
......@@ -56,11 +57,12 @@ public class TracerContextTestCase {
TracerContext context = new TracerContext();
Span serviceSpan = context.createSpan("/serviceA");
Span dbSpan = context.createSpan("db/preparedStatement/execute");
Tags.PEER_HOST.set(dbSpan, "127.0.0.1:8080");
ContextCarrier carrier = new ContextCarrier();
context.inject(carrier);
Assert.assertTrue(carrier.isValid());
Assert.assertEquals("127.0.0.1:8080", carrier.getPeerHost());
Assert.assertEquals(1, carrier.getSpanId());
}
......@@ -69,6 +71,8 @@ public class TracerContextTestCase {
ContextCarrier carrier = new ContextCarrier();
carrier.setTraceSegmentId("trace_id_1");
carrier.setSpanId(5);
carrier.setApplicationCode("REMOTE_APP");
carrier.setPeerHost("10.2.3.16:8080");
Assert.assertTrue(carrier.isValid());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册