diff --git a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegmentRef.java b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegmentRef.java index 3646e3a4b21ece2275e6b4af8bd44ec06d5a728e..098b54787dba269c19d1783c115bfdb3db9d9408 100644 --- a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegmentRef.java +++ b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegmentRef.java @@ -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 { */ 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 { 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.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 { public void deserialize(SegmentRefMessage message) { traceSegmentId = message.getTraceSegmentId(); spanId = message.getSpanId(); + applicationCode = message.getApplicationCode(); + peerHost = message.getPeerHost(); } } diff --git a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/tag/Tags.java b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/tag/Tags.java index e706e975d538c32b9621255b6fb40bb5e09b9081..67e841e45c725c1f5867e15096625124138a18eb 100644 --- a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/tag/Tags.java +++ b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/tag/Tags.java @@ -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"); diff --git a/skywalking-commons/skywalking-trace/src/main/proto/trace.proto b/skywalking-commons/skywalking-trace/src/main/proto/trace.proto index c9a175a00d5d2b9947650241eb739233c3b547cb..df53a6c2b2c8534da8d3642c0d6c31da302c2f9f 100644 --- a/skywalking-commons/skywalking-trace/src/main/proto/trace.proto +++ b/skywalking-commons/skywalking-trace/src/main/proto/trace.proto @@ -16,6 +16,8 @@ message SegmentMessage { message SegmentRefMessage { string traceSegmentId = 1; int32 spanId = 2; + string applicationCode = 3; + string peerHost = 4; } message SpanMessage { 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 cd8596221849645c3b39ba81194cb025f168edcf..46843831f8af7c16f65007a2ae0370266bac6a20 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 @@ -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"); 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 6af5cf7e34841ade78dfca2999502566ba9a6347..a7d5a96ed0f2da20e41a64c6cdb434af9b7c41a2 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 @@ -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; + } } 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 13860b6df8693c18691879333c91c62ff2d49050..31e6fe6de0fc67b0d208b46f28facded4c5389d5 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 @@ -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); } 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 805090e07ca0458c1ee8b9fe3326d6012c676d0b..8b6e1fc263d7e8300223325ab1f4e08b9fd0ef95 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 @@ -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()); } } 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 15389ff9bd7ae1094cb2ed41feb977a14cdab16a..cd8478f51149ed5e8ae9ced79c1744878ad459ff 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 @@ -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());