diff --git a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/Span.java b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/Span.java index e256f73f3a24b88cd82de8c6be9963a49a2cf779..fd971c9cfe7a7ab8571e5a0c1588f123b9bff0c6 100644 --- a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/Span.java +++ b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/Span.java @@ -1,11 +1,23 @@ package org.skywalking.apm.trace; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.TypeAdapter; import com.google.gson.annotations.Expose; +import com.google.gson.annotations.JsonAdapter; import com.google.gson.annotations.SerializedName; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.*; +import org.skywalking.apm.trace.TraceId.DistributedTraceIds; +import org.skywalking.apm.trace.tag.BooleanTagItem; +import org.skywalking.apm.trace.tag.IntTagItem; +import org.skywalking.apm.trace.tag.StringTagItem; +import org.skywalking.apm.util.StringUtil; /** * Span is a concept from OpenTracing Spec, also from Google Dapper Paper. @@ -16,27 +28,21 @@ import java.util.*; *

* Created by wusheng on 2017/2/17. */ +@JsonAdapter(Span.Serializer.class) public class Span { - @Expose - @SerializedName(value = "si") - private int spanId; + private static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); - @Expose - @SerializedName(value = "ps") + private int spanId; private int parentSpanId; /** * The start time of this Span. */ - @Expose - @SerializedName(value = "st") private long startTime; /** * The end time of this Span. */ - @Expose - @SerializedName(value = "et") private long endTime; /** @@ -44,43 +50,43 @@ public class Span { * If you want to know, how to set an operation name, * {@see https://github.com/opentracing/specification/blob/master/specification.md#start-a-new-span} */ - @Expose - @SerializedName(value = "on") private String operationName; + /** + * {@link #peer}, {@link #port} and {@link #peers} were part of tags, + * independence them from tags for better performance and gc. + */ + private String peer; + + private int port; + + private String peers; + /** * Tag is a concept from OpenTracing spec. *

* {@see https://github.com/opentracing/specification/blob/master/specification.md#set-a-span-tag} */ - @Expose - @SerializedName(value = "ts") - private final Map tagsWithStr; + private List tagsWithStr; - @Expose - @SerializedName(value = "tb") - private final Map tagsWithBool; + private List tagsWithBool; - @Expose - @SerializedName(value = "ti") - private final Map tagsWithInt; + private List tagsWithInt; /** * Log is a concept from OpenTracing spec. *

* {@see https://github.com/opentracing/specification/blob/master/specification.md#log-structured-data} */ - @Expose - @SerializedName(value = "lo") - private final List logs; + private List logs; /** * Create a new span, by given span id, parent span id and operationName. * This span must belong a {@link TraceSegment}, also is a part of Distributed Trace. * - * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} - * @param parentSpanId given by the creator, and must be an existed span id in the {@link TraceSegment}. Value -1 - * means no parent span if this {@link TraceSegment}. + * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} + * @param parentSpanId given by the creator, and must be an existed span id in the {@link TraceSegment}. Value -1 + * means no parent span if this {@link TraceSegment}. * @param operationName {@link #operationName} */ protected Span(int spanId, int parentSpanId, String operationName) { @@ -91,11 +97,11 @@ public class Span { * Create a new span, by given span id, parent span id, operationName and startTime. * This span must belong a {@link TraceSegment}, also is a part of Distributed Trace. * - * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} - * @param parentSpanId given by the creator, and must be an existed span id in the {@link TraceSegment}. Value -1 - * means no parent span if this {@link TraceSegment}. + * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} + * @param parentSpanId given by the creator, and must be an existed span id in the {@link TraceSegment}. Value -1 + * means no parent span if this {@link TraceSegment}. * @param operationName {@link #operationName} - * @param startTime given start timestamp. + * @param startTime given start timestamp. */ protected Span(int spanId, int parentSpanId, String operationName, long startTime) { this(); @@ -109,7 +115,7 @@ public class Span { * Create a new span, by given span id and no parent span id. * No parent span id means that, this Span is the first span of the {@link TraceSegment} * - * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} + * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} * @param operationName {@link #operationName} */ public Span(int spanId, String operationName) { @@ -120,9 +126,9 @@ public class Span { * Create a new span, by given span id and give startTime but no parent span id, * No parent span id means that, this Span is the first span of the {@link TraceSegment} * - * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} + * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} * @param operationName {@link #operationName} - * @param startTime given start time of span + * @param startTime given start time of span */ public Span(int spanId, String operationName, long startTime) { this(spanId, -1, operationName, startTime); @@ -131,8 +137,8 @@ public class Span { /** * Create a new span, by given span id and given parent {@link Span}. * - * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} - * @param parentSpan {@link Span} + * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} + * @param parentSpan {@link Span} * @param operationName {@link #operationName} */ public Span(int spanId, Span parentSpan, String operationName) { @@ -143,10 +149,10 @@ public class Span { * Create a new span, by given span id, parent span, operationName and startTime. * This span must belong a {@link TraceSegment}, also is a part of Distributed Trace. * - * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} - * @param parentSpan {@link Span} + * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} + * @param parentSpan {@link Span} * @param operationName {@link #operationName} - * @param startTime given start timestamp + * @param startTime given start timestamp */ public Span(int spanId, Span parentSpan, String operationName, long startTime) { this(spanId, parentSpan.spanId, operationName, startTime); @@ -156,10 +162,6 @@ public class Span { * Create a new/empty span. */ public Span() { - tagsWithStr = new HashMap(5); - tagsWithBool = new HashMap(1); - tagsWithInt = new HashMap(2); - logs = new LinkedList(); } /** @@ -177,7 +179,7 @@ public class Span { * When it is finished, it will be archived by the given {@link TraceSegment}, which owners it. * At the same out, set the {@link #endTime} as the given endTime * - * @param owner of the Span. + * @param owner of the Span. * @param endTime of the Span. */ public void finish(TraceSegment owner, long endTime) { @@ -201,60 +203,34 @@ public class Span { * @return this Span instance, for chaining */ public Span setTag(String key, String value) { - tagsWithStr.put(key, value); + if (tagsWithStr == null) { + tagsWithStr = new LinkedList(); + } + tagsWithStr.add(new StringTagItem(key, value)); return this; } public Span setTag(String key, boolean value) { - tagsWithBool.put(key, value); + if (tagsWithBool == null) { + tagsWithBool = new LinkedList(); + } + tagsWithBool.add(new BooleanTagItem(key, value)); return this; } public Span setTag(String key, Integer value) { - tagsWithInt.put(key, value); + if (tagsWithInt == null) { + tagsWithInt = new LinkedList(); + } + tagsWithInt.add(new IntTagItem(key, value)); return this; } - /** - * Get all tags from this span, but readonly. - * - * @return - */ - public final Map getTags() { - Map tags = new HashMap(); - tags.putAll(tagsWithStr); - tags.putAll(tagsWithBool); - tags.putAll(tagsWithInt); - return tags; - } - - /** - * Get tag value of the given key. - * - * @param key the given tag key. - * @return tag value. - */ - public String getStrTag(String key) { - return tagsWithStr.get(key); - } - - public Boolean getBoolTag(String key) { - return tagsWithBool.get(key); - } - - public Integer getIntTag(String key) { - return tagsWithInt.get(key); - } - /** * This method is from opentracing-java. {@see https://github.com/opentracing/opentracing-java/blob/release-0.20.9/opentracing-api/src/main/java/io/opentracing/Span.java#L91} - *

- * Log key:value pairs to the Span with the current walltime timestamp. - *

- *

CAUTIONARY NOTE: not all Tracer implementations support key:value log fields end-to-end. - * Caveat emptor. - *

- *

A contrived example (using Guava, which is not required): + *

Log key:value pairs to the Span with the current walltime timestamp.

CAUTIONARY NOTE: + * not all Tracer implementations support key:value log fields end-to-end. Caveat emptor.

A contrived example + * (using Guava, which is not required): *

{@code
      * span.log(
      * ImmutableMap.Builder()
@@ -265,11 +241,14 @@ public class Span {
      * }
* * @param fields key:value log fields. Tracer implementations should support String, numeric, and boolean values; - * some may also support arbitrary Objects. + * some may also support arbitrary Objects. * @return the Span, for chaining * @see Span#log(String) */ public Span log(Map fields) { + if (logs == null) { + logs = new LinkedList(); + } logs.add(new LogData(System.currentTimeMillis(), fields)); return this; } @@ -318,11 +297,7 @@ public class Span { /** * This method is from opentracing-java. {@see https://github.com/opentracing/opentracing-java/blob/release-0.20.9/opentracing-api/src/main/java/io/opentracing/Span.java#L120} - *

- * Record an event at the current walltime timestamp. - *

- * Shorthand for - *

+ *

Record an event at the current walltime timestamp.

Shorthand for

*

{@code
      * span.log(Collections.singletonMap("event", event));
      * }
@@ -355,14 +330,34 @@ public class Span { return operationName; } - public List getLogs() { - return Collections.unmodifiableList(logs); - } - public boolean isLeaf() { return false; } + public String getPeer() { + return peer; + } + + public int getPort() { + return port; + } + + public String getPeers() { + return peers; + } + + public void setPeer(String peer) { + this.peer = peer; + } + + public void setPort(int port) { + this.port = port; + } + + public void setPeers(String peers) { + this.peers = peers; + } + @Override public String toString() { return "Span{" + @@ -372,4 +367,76 @@ public class Span { ", operationName='" + operationName + '\'' + '}'; } + + public static class Serializer extends TypeAdapter { + @Override + public void write(JsonWriter out, Span span) throws IOException { + out.beginObject(); + out.name("si").value(span.spanId); + out.name("ps").value(span.parentSpanId); + out.name("st").value(span.startTime); + out.name("et").value(span.endTime); + out.name("on").value(span.operationName); + + this.writeTags(out, span); + + if(span.logs != null) { + out.name("logs").jsonValue(gson.toJson(span.logs)); + } + + out.endObject(); + } + + public void writeTags(JsonWriter out, Span span) throws IOException { + JsonObject tagWithStr = null; + JsonObject tagWithInt = null; + JsonObject tagWithBool = null; + if (!StringUtil.isEmpty(span.peer)) { + tagWithStr = new JsonObject(); + tagWithStr.addProperty("peer.host", span.peer); + tagWithInt = new JsonObject(); + tagWithInt.addProperty("peer.port", span.port); + } else if (!StringUtil.isEmpty(span.peers)) { + tagWithStr = new JsonObject(); + tagWithStr.addProperty("peers", span.peers); + } else if (span.tagsWithStr != null) { + tagWithStr = new JsonObject(); + } + + if (span.tagsWithStr != null) { + for (StringTagItem item : span.tagsWithStr) { + tagWithStr.addProperty(item.getKey(), item.getValue()); + } + } + if (span.tagsWithInt != null) { + if (tagWithInt != null) { + tagWithInt = new JsonObject(); + } + for (IntTagItem item : span.tagsWithInt) { + tagWithInt.addProperty(item.getKey(), item.getValue()); + } + } + if (span.tagsWithBool != null) { + tagWithBool = new JsonObject(); + for (BooleanTagItem item : span.tagsWithBool) { + tagWithBool.addProperty(item.getKey(), item.getValue()); + } + } + + if (tagWithStr != null) { + out.name("ts").jsonValue(tagWithStr.toString()); + } + if (tagWithInt != null) { + out.name("ti").jsonValue(tagWithInt.toString()); + } + if (tagWithBool != null) { + out.name("tb").jsonValue(tagWithBool.toString()); + } + } + + @Override + public Span read(JsonReader in) throws IOException { + throw new IOException("Can't deserialize span at agent side for performance consideration"); + } + } } diff --git a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/AbstractTag.java b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/AbstractTag.java index 1ded62e9824f8e1c58ebe637098d7f968eb1fff3..cc37ea86be41d26f33ec0196e4132811489254af 100644 --- a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/AbstractTag.java +++ b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/AbstractTag.java @@ -30,6 +30,4 @@ public abstract class AbstractTag { public String key() { return this.key; } - - public abstract T get(Span span); } diff --git a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/BooleanTag.java b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/BooleanTag.java index a08d50e5736d341815dd183de76a66f35740e7e9..ca48962b2816465982c2209719b6271c59fed11e 100644 --- a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/BooleanTag.java +++ b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/BooleanTag.java @@ -21,20 +21,7 @@ public class BooleanTag extends AbstractTag { span.setTag(key, tagValue); } - /** - * Get a tag value, type of {@link Boolean}. After akka-message/serialize, all tags values are type of {@link - * String}, convert to {@link Boolean}, if necessary. - * - * @param span - * @return tag value - */ - @Override - public Boolean get(Span span) { - Boolean tagValue = span.getBoolTag(super.key); - if (tagValue == null) { - return defaultValue; - } else { - return tagValue; - } + public boolean defaultValue() { + return defaultValue; } } diff --git a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/BooleanTagItem.java b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/BooleanTagItem.java new file mode 100644 index 0000000000000000000000000000000000000000..664fc7909c12b47f56bea8937c4eb4e14c2195b0 --- /dev/null +++ b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/BooleanTagItem.java @@ -0,0 +1,24 @@ +package org.skywalking.apm.trace.tag; + +/** + * The tag item with String key and Boolean value. + * + * @author wusheng + */ +public class BooleanTagItem { + private String key; + private boolean value; + + public BooleanTagItem(String key, boolean value) { + this.key = key; + this.value = value; + } + + public String getKey() { + return key; + } + + public boolean getValue() { + return value; + } +} diff --git a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/IntTag.java b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/IntTag.java index e02e7e580403f70c936ded7acb4fe46a1f660ef5..92ed9eba3304264a9eac84bb223458bae0f6901a 100644 --- a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/IntTag.java +++ b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/IntTag.java @@ -17,20 +17,4 @@ public class IntTag extends AbstractTag { span.setTag(super.key, tagValue); } - /** - * Get a tag value, type of {@link Integer}. After akka-message/serialize, all tags values are type of {@link - * String}, convert to {@link Integer}, if necessary. - * - * @param span - * @return tag value - */ - @Override - public Integer get(Span span) { - Integer tagValue = span.getIntTag(super.key); - if (tagValue == null) { - return null; - } else { - return tagValue; - } - } } diff --git a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/IntTagItem.java b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/IntTagItem.java new file mode 100644 index 0000000000000000000000000000000000000000..efebc7af03049e0ba2eeda2e9e2c990084646890 --- /dev/null +++ b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/IntTagItem.java @@ -0,0 +1,24 @@ +package org.skywalking.apm.trace.tag; + +/** + * The tag item with String key and Int value. + * + * @author wusheng + */ +public class IntTagItem { + private String key; + private int value; + + public IntTagItem(String key, int value) { + this.key = key; + this.value = value; + } + + public String getKey() { + return key; + } + + public int getValue() { + return value; + } +} diff --git a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/StringTag.java b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/StringTag.java index 1fa32f7ce67b873704f4a2408d8ec5812a949c17..1ce53d5dd9dffa5acbcffae93138a54bd82ebf5f 100644 --- a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/StringTag.java +++ b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/StringTag.java @@ -17,9 +17,4 @@ public class StringTag extends AbstractTag { public void set(Span span, String tagValue) { span.setTag(key, tagValue); } - - @Override - public String get(Span span) { - return span.getStrTag(super.key); - } } diff --git a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/StringTagItem.java b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/StringTagItem.java new file mode 100644 index 0000000000000000000000000000000000000000..3f905f760a4fddc687c5ec931d5810f3a4e765a1 --- /dev/null +++ b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/StringTagItem.java @@ -0,0 +1,24 @@ +package org.skywalking.apm.trace.tag; + +/** + * The tag item with String key and String value. + * + * @author wusheng + */ +public final class StringTagItem { + private String key; + private String value; + + public StringTagItem(String key, String value) { + this.key = key; + this.value = value; + } + + public String getKey() { + return key; + } + + public String getValue() { + return value; + } +} diff --git a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/Tags.java b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/Tags.java index 6e4d0247f0d62fcfae72cf78511ff4af625c04e0..b70f492d04ff8a127aa6213fd286457b202e574d 100644 --- a/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/Tags.java +++ b/apm-commons/apm-trace/src/main/java/org/skywalking/apm/trace/tag/Tags.java @@ -46,7 +46,7 @@ public final class Tags { * nosql=something like redis/memcache */ public static final class SPAN_LAYER { - private static StringTag SPAN_LAYER_TAG = new StringTag("span.layer"); + public static StringTag SPAN_LAYER_TAG = new StringTag("span.layer"); private static final String DB_LAYER = "db"; private static final String RPC_FRAMEWORK_LAYER = "rpc"; @@ -63,22 +63,6 @@ public final class Tags { public static void asHttp(Span span) { SPAN_LAYER_TAG.set(span, HTTP_LAYER); } - - public static String get(Span span) { - return SPAN_LAYER_TAG.get(span); - } - - public static boolean isDB(Span span) { - return DB_LAYER.equals(get(span)); - } - - public static boolean isRPCFramework(Span span) { - return RPC_FRAMEWORK_LAYER.equals(get(span)); - } - - public static boolean isHttp(Span span) { - return HTTP_LAYER.equals(get(span)); - } } /** @@ -92,21 +76,6 @@ public final class Tags { */ public static final BooleanTag ERROR = new BooleanTag("error", false); - /** - * 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"); - - /** - * PEER_PORT records remote port of the peer - */ - public static final IntTag PEER_PORT = new IntTag("peer.port"); - - /** - * PEERS records multiple host address and port of remote - */ - public static final StringTag PEERS = new StringTag("peers"); - /** * DB_TYPE records database type, such as sql, redis, cassandra and so on. */ diff --git a/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/SpanTestCase.java b/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/SpanTestCase.java index 300135f473566a4aab95a7400b83b6bdd7bd5e2e..612d67653b17cdec1668cbe549986962b5ec4431 100644 --- a/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/SpanTestCase.java +++ b/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/SpanTestCase.java @@ -1,7 +1,10 @@ package org.skywalking.apm.trace; +import java.lang.reflect.Field; import org.junit.Assert; import org.junit.Test; +import org.skywalking.apm.trace.tag.BooleanTagReader; +import org.skywalking.apm.trace.tag.StringTagReader; import org.skywalking.apm.trace.tag.Tags; import java.util.List; @@ -42,25 +45,26 @@ public class SpanTestCase { Span span1 = new Span(0, "serviceA"); Tags.SPAN_LAYER.asHttp(span1); Tags.COMPONENT.set(span1, "Spring"); - Tags.PEER_HOST.set(span1, "127.0.0.1"); + span1.setPeer("127.0.0.1"); Tags.ERROR.set(span1, true); Tags.STATUS_CODE.set(span1, 302); Tags.URL.set(span1, "http://127.0.0.1/serviceA"); Tags.DB_STATEMENT.set(span1, "select * from users"); - Map tags = span1.getTags(); - Assert.assertEquals(7, tags.size()); - Assert.assertTrue(Tags.SPAN_LAYER.isHttp(span1)); - Assert.assertEquals("127.0.0.1", Tags.PEER_HOST.get(span1)); - Assert.assertTrue(Tags.ERROR.get(span1)); + Assert.assertEquals("http", StringTagReader.get(span1, Tags.SPAN_LAYER.SPAN_LAYER_TAG)); + Assert.assertEquals("127.0.0.1", span1.getPeer()); + Assert.assertTrue(BooleanTagReader.get(span1, Tags.ERROR)); } @Test - public void testLogException() { + public void testLogException() throws NoSuchFieldException, IllegalAccessException { Span span1 = new Span(0, "serviceA"); Exception exp = new Exception("exception msg"); span1.log(exp); - List logs = span1.getLogs(); + + Field logsField = Span.class.getDeclaredField("logs"); + logsField.setAccessible(true); + List logs = (List)logsField.get(span1); Assert.assertEquals("java.lang.Exception", logs.get(0).getFields().get("error.kind")); Assert.assertEquals("exception msg", logs.get(0).getFields().get("message")); diff --git a/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/TraceSegmentTestCase.java b/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/TraceSegmentTestCase.java index 326e741831002bcc82d418ad568fe6e4429371f1..bcb4de4700f17bb89f61216f4edb2021509475c4 100644 --- a/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/TraceSegmentTestCase.java +++ b/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/TraceSegmentTestCase.java @@ -115,11 +115,7 @@ public class TraceSegmentTestCase { String segmentJson = jsonString.substring(5); Assert.assertEquals(length, segmentJson.length()); - TraceSegment recoverySegment = gson.fromJson(segmentJson, TraceSegment.class); - Assert.assertEquals(segment.getSpans().size(), recoverySegment.getSpans().size()); - Assert.assertEquals(segment.getRefs().get(0).getTraceSegmentId(), recoverySegment.getRefs().get(0).getTraceSegmentId()); - Assert.assertEquals(Tags.SPAN_LAYER.get(segment.getSpans().get(1)), Tags.SPAN_LAYER.get(recoverySegment.getSpans().get(1))); - Assert.assertEquals(segment.getSpans().get(1).getLogs().get(0).getTime(), recoverySegment.getSpans().get(1).getLogs().get(0).getTime()); + System.out.println(segmentJson); } } diff --git a/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/BooleanTagReader.java b/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/BooleanTagReader.java new file mode 100644 index 0000000000000000000000000000000000000000..e8cbb5180163769f70f8e2b393792c77ca60a46c --- /dev/null +++ b/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/BooleanTagReader.java @@ -0,0 +1,31 @@ +package org.skywalking.apm.trace.tag; + +import java.lang.reflect.Field; +import java.util.List; +import org.skywalking.apm.trace.Span; + +/** + * @author wusheng + */ +public class BooleanTagReader { + public static Boolean get(Span span, BooleanTag tag) { + List tagsWithBoolList = null; + try { + Field tagsWithBool = Span.class.getDeclaredField("tagsWithBool"); + tagsWithBool.setAccessible(true); + tagsWithBoolList = (List)tagsWithBool.get(span); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + for (BooleanTagItem item : tagsWithBoolList) { + if (tag.key().equals(item.getKey())) { + return item.getValue(); + } + } + return tag.defaultValue(); + } + +} diff --git a/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/IntTagReader.java b/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/IntTagReader.java new file mode 100644 index 0000000000000000000000000000000000000000..5e25f2ab04db508aa37e51c6b95751b146e3daf4 --- /dev/null +++ b/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/IntTagReader.java @@ -0,0 +1,31 @@ +package org.skywalking.apm.trace.tag; + +import java.lang.reflect.Field; +import java.util.List; +import org.skywalking.apm.trace.Span; + +/** + * @author wusheng + */ +public class IntTagReader { + public static Integer get(Span span, IntTag tag) { + List tagsWithIntList = null; + try { + Field tagsWithInt = Span.class.getDeclaredField("tagsWithInt"); + tagsWithInt.setAccessible(true); + tagsWithIntList = (List)tagsWithInt.get(span); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + for (IntTagItem item : tagsWithIntList) { + if (tag.key().equals(item.getKey())) { + return item.getValue(); + } + } + return null; + } + +} diff --git a/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/StringTagReader.java b/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/StringTagReader.java new file mode 100644 index 0000000000000000000000000000000000000000..5aee9a93c7d99f59784d646385a518154a913adb --- /dev/null +++ b/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/StringTagReader.java @@ -0,0 +1,31 @@ +package org.skywalking.apm.trace.tag; + +import java.lang.reflect.Field; +import java.util.List; +import org.skywalking.apm.trace.Span; + +/** + * @author wusheng + */ +public class StringTagReader { + public static String get(Span span, StringTag tag) { + List tagsWithStrList = null; + try { + Field tagsWithStr = Span.class.getDeclaredField("tagsWithStr"); + tagsWithStr.setAccessible(true); + tagsWithStrList = (List)tagsWithStr.get(span); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + for (StringTagItem item : tagsWithStrList) { + if (tag.key().equals(item.getKey())) { + return item.getValue(); + } + } + return null; + } + +} diff --git a/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/TagsTest.java b/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/TagsTest.java index 9e1056d6dcf2b1e7f817f0f2c15919cc066065d3..6bd18c3fbccce3fa035796a971a19a343660afd6 100644 --- a/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/TagsTest.java +++ b/apm-commons/apm-trace/src/test/java/org/skywalking/apm/trace/tag/TagsTest.java @@ -12,32 +12,32 @@ public class TagsTest { public void testLayer() { Span span = new Span(1, "/test"); Tags.SPAN_LAYER.asDB(span); - Assert.assertEquals("db", span.getStrTag("span.layer")); + Assert.assertEquals("db", StringTagReader.get(span, Tags.SPAN_LAYER.SPAN_LAYER_TAG)); Tags.SPAN_LAYER.asRPCFramework(span); - Assert.assertEquals("rpc", span.getStrTag("span.layer")); + Assert.assertEquals("rpc", StringTagReader.get(span, Tags.SPAN_LAYER.SPAN_LAYER_TAG)); Tags.SPAN_LAYER.asHttp(span); - Assert.assertEquals("http", span.getStrTag("span.layer")); + Assert.assertEquals("http", StringTagReader.get(span, Tags.SPAN_LAYER.SPAN_LAYER_TAG)); } @Test public void testBooleanTag() { BooleanTag tag = new BooleanTag("test.key", false); Span span = new Span(1, "/test"); - Assert.assertFalse(tag.get(span)); + Assert.assertFalse(BooleanTagReader.get(span, tag)); tag.set(span, true); - Assert.assertTrue(tag.get(span)); + Assert.assertTrue(BooleanTagReader.get(span, tag)); } @Test public void testIntTag() { IntTag tag = new IntTag("test.key"); Span span = new Span(1, "/test"); - Assert.assertNull(tag.get(span)); + Assert.assertNull(IntTagReader.get(span, tag)); tag.set(span, 123); - Assert.assertEquals(123, tag.get(span).intValue()); + Assert.assertEquals(123, IntTagReader.get(span, tag).intValue()); } } diff --git a/apm-sniffer/apm-sniffer-mock/src/main/java/org/skywalking/apm/sniffer/mock/trace/tags/BooleanTagReader.java b/apm-sniffer/apm-sniffer-mock/src/main/java/org/skywalking/apm/sniffer/mock/trace/tags/BooleanTagReader.java new file mode 100644 index 0000000000000000000000000000000000000000..12a60e8cc576c7a5bb5f9af6b1649d96a3bc692e --- /dev/null +++ b/apm-sniffer/apm-sniffer-mock/src/main/java/org/skywalking/apm/sniffer/mock/trace/tags/BooleanTagReader.java @@ -0,0 +1,33 @@ +package org.skywalking.apm.sniffer.mock.trace.tags; + +import java.lang.reflect.Field; +import java.util.List; +import org.skywalking.apm.trace.Span; +import org.skywalking.apm.trace.tag.BooleanTag; +import org.skywalking.apm.trace.tag.BooleanTagItem; + +/** + * @author wusheng + */ +public class BooleanTagReader { + public static Boolean get(Span span, BooleanTag tag) { + List tagsWithBoolList = null; + try { + Field tagsWithBool = Span.class.getDeclaredField("tagsWithBool"); + tagsWithBool.setAccessible(true); + tagsWithBoolList = (List)tagsWithBool.get(span); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + for (BooleanTagItem item : tagsWithBoolList) { + if (tag.key().equals(item.getKey())) { + return item.getValue(); + } + } + return tag.defaultValue(); + } + +} diff --git a/apm-sniffer/apm-sniffer-mock/src/main/java/org/skywalking/apm/sniffer/mock/trace/tags/IntTagReader.java b/apm-sniffer/apm-sniffer-mock/src/main/java/org/skywalking/apm/sniffer/mock/trace/tags/IntTagReader.java new file mode 100644 index 0000000000000000000000000000000000000000..0eeeb60c99564bfb86c266c5a44729e070d48b43 --- /dev/null +++ b/apm-sniffer/apm-sniffer-mock/src/main/java/org/skywalking/apm/sniffer/mock/trace/tags/IntTagReader.java @@ -0,0 +1,33 @@ +package org.skywalking.apm.sniffer.mock.trace.tags; + +import java.lang.reflect.Field; +import java.util.List; +import org.skywalking.apm.trace.Span; +import org.skywalking.apm.trace.tag.IntTag; +import org.skywalking.apm.trace.tag.IntTagItem; + +/** + * @author wusheng + */ +public class IntTagReader { + public static Integer get(Span span, IntTag tag) { + List tagsWithIntList = null; + try { + Field tagsWithInt = Span.class.getDeclaredField("tagsWithInt"); + tagsWithInt.setAccessible(true); + tagsWithIntList = (List)tagsWithInt.get(span); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + for (IntTagItem item : tagsWithIntList) { + if (tag.key().equals(item.getKey())) { + return item.getValue(); + } + } + return null; + } + +} diff --git a/apm-sniffer/apm-sniffer-mock/src/main/java/org/skywalking/apm/sniffer/mock/trace/tags/StringTagReader.java b/apm-sniffer/apm-sniffer-mock/src/main/java/org/skywalking/apm/sniffer/mock/trace/tags/StringTagReader.java new file mode 100644 index 0000000000000000000000000000000000000000..66ebb6eca672d8214af88cab5e625f8c9f0f97a7 --- /dev/null +++ b/apm-sniffer/apm-sniffer-mock/src/main/java/org/skywalking/apm/sniffer/mock/trace/tags/StringTagReader.java @@ -0,0 +1,33 @@ +package org.skywalking.apm.sniffer.mock.trace.tags; + +import java.lang.reflect.Field; +import java.util.List; +import org.skywalking.apm.trace.Span; +import org.skywalking.apm.trace.tag.StringTag; +import org.skywalking.apm.trace.tag.StringTagItem; + +/** + * @author wusheng + */ +public class StringTagReader { + public static String get(Span span, StringTag tag) { + List tagsWithStrList = null; + try { + Field tagsWithStr = Span.class.getDeclaredField("tagsWithStr"); + tagsWithStr.setAccessible(true); + tagsWithStrList = (List)tagsWithStr.get(span); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + for (StringTagItem item : tagsWithStrList) { + if (tag.key().equals(item.getKey())) { + return item.getValue(); + } + } + return null; + } + +}