Commit 2940f762 authored by 吴晟's avatar 吴晟
Browse files

Fix tag bugs. Adjust TraceSegment and ContextCarrier. Add sampling mechanism. #110

parent f2434067
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -17,9 +17,9 @@ public class LogData {

    @Expose
    @SerializedName(value="fi")
    private Map<String, ?> fields;
    private Map<String, String> fields;

    LogData(long time, Map<String, ?> fields) {
    LogData(long time, Map<String, String> fields) {
        this.time = time;
        if(fields == null){
            throw new NullPointerException();
+33 −11
Original line number Diff line number Diff line
@@ -57,8 +57,16 @@ public class Span{
     * {@see https://github.com/opentracing/specification/blob/master/specification.md#set-a-span-tag}
     */
    @Expose
    @SerializedName(value="ta")
    private final Map<String, Object> tags;
    @SerializedName(value="ts")
    private final Map<String, String> tagsWithStr;

    @Expose
    @SerializedName(value="tb")
    private final Map<String, Boolean> tagsWithBool;

    @Expose
    @SerializedName(value="ti")
    private final Map<String, Integer> tagsWithInt;

    /**
     * Log is a concept from OpenTracing spec.
@@ -151,7 +159,9 @@ public class Span{
     * Create a new/empty span.
     */
    public Span() {
        tags = new HashMap<String, Object>();
        tagsWithStr = new HashMap<String, String>(5);
        tagsWithBool = new HashMap<String, Boolean>(1);
        tagsWithInt = new HashMap<String, Integer>(2);
        logs = new LinkedList<LogData>();
    }

@@ -194,17 +204,17 @@ public class Span{
     * @return this Span instance, for chaining
     */
    public final Span setTag(String key, String value) {
        tags.put(key, value);
        tagsWithStr.put(key, value);
        return this;
    }

    public final Span setTag(String key, boolean value) {
        tags.put(key, value);
        tagsWithBool.put(key, value);
        return this;
    }

    public final Span setTag(String key, Number value) {
        tags.put(key, value);
    public final Span setTag(String key, Integer value) {
        tagsWithInt.put(key, value);
        return this;
    }

@@ -214,7 +224,11 @@ public class Span{
     * @return
     */
    public final Map<String, Object> getTags() {
        return Collections.unmodifiableMap(tags);
        Map<String, Object> tags = new HashMap<String, Object>();
        tags.putAll(tagsWithStr);
        tags.putAll(tagsWithBool);
        tags.putAll(tagsWithInt);
        return tags;
    }

    /**
@@ -223,8 +237,16 @@ public class Span{
     * @param key the given tag key.
     * @return tag value.
     */
    public Object getTag(String key) {
        return tags.get(key);
    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);
    }

    /**
@@ -250,7 +272,7 @@ public class Span{
     * @return the Span, for chaining
     * @see Span#log(String)
     */
    public Span log(Map<String, ?> fields) {
    public Span log(Map<String, String> fields) {
        logs.add(new LogData(System.currentTimeMillis(), fields));
        return this;
    }
+27 −8
Original line number Diff line number Diff line
@@ -89,6 +89,16 @@ public class TraceSegment {
    @SerializedName(value = "gt")
    private DistributedTraceIds relatedGlobalTraces;

    /**
     * The <code>sampled</code> is a flag, which represent, when this {@link TraceSegment} finished, it need to be send
     * to Collector.
     *
     * Its value depends on SamplingService. True, by default.
     *
     * This value is not serialized.
     */
    private boolean sampled;

    /**
     * Create a trace segment, by given segmentId.
     * This segmentId is generated by TraceSegmentRef, AKA, from tracer/agent module.
@@ -107,6 +117,7 @@ public class TraceSegment {
        this.spans = new LinkedList<Span>();
        this.relatedGlobalTraces = new DistributedTraceIds();
        this.relatedGlobalTraces.append(new NewDistributedTraceId());
        this.sampled = true;
    }

    /**
@@ -183,6 +194,14 @@ public class TraceSegment {
        return applicationCode;
    }

    public boolean isSampled() {
        return sampled;
    }

    public void setSampled(boolean sampled) {
        this.sampled = sampled;
    }

    @Override
    public String toString() {
        return "TraceSegment{" +
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ import com.a.eye.skywalking.trace.Span;
 * All span's tags inherit from {@link AbstractTag},
 * which provide an easy way to
 *      {@link Span#setTag(String, String)} ,
 *      {@link Span#setTag(String, Number)} ,
 *      {@link Span#setTag(String, Integer)}
 *      {@link Span#setTag(String, boolean)} ,
 *
 * Created by wusheng on 2017/2/17.
+2 −4
Original line number Diff line number Diff line
@@ -30,13 +30,11 @@ public class BooleanTag extends AbstractTag<Boolean> {
     */
    @Override
    public Boolean get(Span span) {
        Object tagValue = span.getTag(super.key);
        Boolean tagValue = span.getBoolTag(super.key);
        if (tagValue == null) {
            return defaultValue;
        } else if (tagValue instanceof Boolean) {
            return (Boolean) tagValue;
        } else {
            return Boolean.valueOf(tagValue.toString());
            return tagValue;
        }
    }
}
Loading