From a23538df38af11cd20c606d4095a7a7585b15616 Mon Sep 17 00:00:00 2001 From: wusheng Date: Thu, 23 Feb 2017 14:13:32 +0800 Subject: [PATCH] Add a new field, applicationCode, in TraceSegment. --- .../a/eye/skywalking/trace/TraceSegment.java | 23 +++++++++++++++---- .../src/main/proto/trace.proto | 7 +++--- .../a/eye/skywalking/trace/SpanTestCase.java | 2 +- .../trace/TraceSegmentTestCase.java | 11 +++++---- .../skywalking/api/context/TracerContext.java | 6 ++++- 5 files changed, 35 insertions(+), 14 deletions(-) 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 f41c7c5f7..fde9e445f 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 @@ -55,14 +55,23 @@ public class TraceSegment implements ISerializable { */ private List spans; + /** + * The applicationCode represents a name of current application/JVM and indicates which is business + * role in the cluster. + * + * e.g. account_app, billing_app + */ + private String applicationCode; + /** * 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) { + public TraceSegment(String segmentId, String applicationCode) { this.traceSegmentId = segmentId; + this.applicationCode = applicationCode; this.startTime = System.currentTimeMillis(); this.spans = new LinkedList(); } @@ -72,7 +81,7 @@ public class TraceSegment implements ISerializable { * * @param message from another {@link TraceSegment#serialize()} */ - public TraceSegment(SegmentMessage message){ + public TraceSegment(SegmentMessage message) { deserialize(message); } @@ -138,6 +147,10 @@ public class TraceSegment implements ISerializable { return Collections.unmodifiableList(spans); } + public String getApplicationCode() { + return applicationCode; + } + @Override public String toString() { return "TraceSegment{" + @@ -154,6 +167,7 @@ public class TraceSegment implements ISerializable { segmentBuilder.setTraceSegmentId(traceSegmentId); segmentBuilder.setStartTime(startTime); segmentBuilder.setEndTime(endTime); + segmentBuilder.setApplicationCode(applicationCode); segmentBuilder.setPrimaryRef(primaryRef.serialize()); for (TraceSegmentRef ref : refs) { segmentBuilder.addRefs(ref.serialize()); @@ -169,9 +183,10 @@ public class TraceSegment implements ISerializable { traceSegmentId = message.getTraceSegmentId(); startTime = message.getStartTime(); endTime = message.getEndTime(); + applicationCode = message.getApplicationCode(); (primaryRef = new TraceSegmentRef()).deserialize(message.getPrimaryRef()); List refsList = message.getRefsList(); - if(refsList != null){ + if (refsList != null) { this.refs = new LinkedList(); for (SegmentRefMessage refMessage : refsList) { TraceSegmentRef ref = new TraceSegmentRef(); @@ -181,7 +196,7 @@ public class TraceSegment implements ISerializable { } List spansList = message.getSpansList(); - if(spansList != null){ + if (spansList != null) { this.spans = new LinkedList(); for (SpanMessage spanMessage : spansList) { spans.add(new Span(spanMessage)); diff --git a/skywalking-commons/skywalking-trace/src/main/proto/trace.proto b/skywalking-commons/skywalking-trace/src/main/proto/trace.proto index 8a7642b21..c9a175a00 100644 --- a/skywalking-commons/skywalking-trace/src/main/proto/trace.proto +++ b/skywalking-commons/skywalking-trace/src/main/proto/trace.proto @@ -7,9 +7,10 @@ message SegmentMessage { string traceSegmentId = 1; int64 startTime = 2; int64 endTime = 3; - SegmentRefMessage primaryRef = 4; - repeated SegmentRefMessage refs = 5; - repeated SpanMessage spans = 6; + string applicationCode = 4; + SegmentRefMessage primaryRef = 5; + repeated SegmentRefMessage refs = 6; + repeated SpanMessage spans = 7; } 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 72ce1369d..f1b05855b 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"); + TraceSegment owner = new TraceSegment("trace_1", "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 8cb632d8c..cd8596221 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,15 +10,16 @@ import org.junit.Test; public class TraceSegmentTestCase { @Test public void testConstructor() { - TraceSegment segment = new TraceSegment("trace_1"); + TraceSegment segment = new TraceSegment("trace_1", "billing_app"); Assert.assertEquals("trace_1", segment.getTraceSegmentId()); Assert.assertTrue(segment.getStartTime() > 0); + Assert.assertEquals("billing_app", segment.getApplicationCode()); } @Test public void testRef() { - TraceSegment segment = new TraceSegment("trace_3"); + TraceSegment segment = new TraceSegment("trace_3", "billing_app"); TraceSegmentRef ref1 = new TraceSegmentRef(); ref1.setTraceSegmentId("parent_trace_0"); @@ -45,7 +46,7 @@ public class TraceSegmentTestCase { @Test public void testArchiveSpan() { - TraceSegment segment = new TraceSegment("trace_1"); + TraceSegment segment = new TraceSegment("trace_1", "billing_app"); Span span1 = new Span(1, "/serviceA"); segment.archive(span1); @@ -58,7 +59,7 @@ public class TraceSegmentTestCase { @Test public void testFinish() { - TraceSegment segment = new TraceSegment("trace_1"); + TraceSegment segment = new TraceSegment("trace_1", "billing_app"); Assert.assertTrue(segment.getEndTime() == 0); segment.finish(); @@ -67,7 +68,7 @@ public class TraceSegmentTestCase { @Test public void testSerialize() { - TraceSegment segment = new TraceSegment("trace_3"); + TraceSegment segment = new TraceSegment("trace_3", "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/context/TracerContext.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java index c7d77eedb..1b3edbe92 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 @@ -1,5 +1,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; @@ -28,8 +29,11 @@ public final class TracerContext { private int spanIdGenerator; + /** + * Create a {@link TraceSegment} and init {@link #spanIdGenerator} as 0; + */ TracerContext() { - this.segment = new TraceSegment(TraceIdGenerator.generate()); + this.segment = new TraceSegment(TraceIdGenerator.generate(), Config.SkyWalking.APPLICATION_CODE); this.spanIdGenerator = 0; } -- GitLab