Commit 5c369aef authored by 吴晟's avatar 吴晟
Browse files

Alter TraceSegement, Add a list of DistributedTraceId as field `relatedGlobalTraces`.

parent 57814379
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
package com.a.eye.skywalking.api.util;
package com.a.eye.skywalking.trace;

import com.a.eye.skywalking.api.conf.Constants;
import com.a.eye.skywalking.api.util.MachineInfo;
import com.a.eye.skywalking.api.util.StringUtil;
import java.util.UUID;

public final class TraceIdGenerator {
public final class GlobalIdGenerator {
    private static final ThreadLocal<Integer> ThreadTraceIdSequence = new ThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
@@ -18,25 +19,25 @@ public final class TraceIdGenerator {
        PROCESS_UUID = uuid.substring(uuid.length() - 7).hashCode();
    }

    private TraceIdGenerator() {
    private GlobalIdGenerator() {
    }

    /**
     * TraceId由以下规则组成<br/>
     * version号 + 1位时间戳(毫秒数) + 1位进程随机号(UUID后7位) + 1位进程数号 + 1位线程号 + 1位线程内序号
     * ID类型 + 1位时间戳(毫秒数) + 1位进程随机号(UUID后7位) + 1位进程数号 + 1位线程号 + 1位线程内序号
     * <p>
     * 注意:这里的位,是指“.”作为分隔符所占的位数,非字符串长度的位数。
     * TraceId为6个片段组成的数组
     *
     * @return
     */
    public static String generate() {
    public static String generate(String type) {
        Integer seq = ThreadTraceIdSequence.get();
        seq++;
        ThreadTraceIdSequence.set(seq);

        return StringUtil.join('.',
            Constants.SDK_VERSION + "", System.currentTimeMillis() + "", PROCESS_UUID + "",
            type + "", System.currentTimeMillis() + "", PROCESS_UUID + "",
            MachineInfo.getProcessNo() + "", Thread.currentThread().getId() + "", seq + "");
    }
}
+26 −0
Original line number Diff line number Diff line
package com.a.eye.skywalking.trace.TraceId;

/**
 * The <code>DistributedTraceId</code> presents a distributed call chain.
 *
 * This call chain has an unique (service) entrance,
 *
 * such as: Service : http://www.skywalking.com/cust/query, all the services, called behind this service, rest services,
 * db executions, are using the same <code>DistributedTraceId</code> even in different JVM.
 *
 * The <code>DistributedTraceId</code> contains only one string, and can NOT be reset, creating a new instance is the
 * only option.
 *
 * @author wusheng
 */
public abstract class DistributedTraceId {
    private String id;

    public DistributedTraceId(String id) {
        this.id = id;
    }

    public String get() {
        return id;
    }
}
+16 −0
Original line number Diff line number Diff line
package com.a.eye.skywalking.trace.TraceId;

import com.a.eye.skywalking.trace.GlobalIdGenerator;

/**
 * The <code>NewDistributedTraceId</code> is a {@link DistributedTraceId} with a new generated id.
 *
 * @author wusheng
 */
public class NewDistributedTraceId extends DistributedTraceId {
    private static final String ID_TYPE = "Trace";

    public NewDistributedTraceId() {
        super(GlobalIdGenerator.generate(ID_TYPE));
    }
}
+12 −0
Original line number Diff line number Diff line
package com.a.eye.skywalking.trace.TraceId;

/**
 * The <code>PropagatedTraceId</code> represents a {@link DistributedTraceId}, which is propagated from the peer.
 *
 * @author wusheng
 */
public class PropagatedTraceId extends DistributedTraceId {
    public PropagatedTraceId(String id) {
        super(id);
    }
}
+54 −11
Original line number Diff line number Diff line
package com.a.eye.skywalking.trace;

import com.a.eye.skywalking.messages.ISerializable;
import com.a.eye.skywalking.trace.TraceId.DistributedTraceId;
import com.a.eye.skywalking.trace.TraceId.NewDistributedTraceId;
import com.a.eye.skywalking.trace.TraceId.PropagatedTraceId;
import com.a.eye.skywalking.trace.proto.SegmentMessage;
import com.a.eye.skywalking.trace.proto.SegmentRefMessage;
import com.a.eye.skywalking.trace.proto.SpanMessage;
import com.google.protobuf.ProtocolStringList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@@ -18,6 +22,8 @@ import java.util.List;
 * Created by wusheng on 2017/2/17.
 */
public class TraceSegment implements ISerializable<SegmentMessage> {
    private static final String ID_TYPE = "Segment";

    /**
     * The id of this trace segment.
     * Every segment has its unique-global-id.
@@ -63,17 +69,32 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
     */
    private String applicationCode;

    /**
     * The <code>relatedGlobalTraces</code> represent a set of all related trace. Most time it contains only one
     * element, because only one parent {@link TraceSegment} exists, but, in batch scenario, the num becomes greater
     * than 1, also meaning multi-parents {@link TraceSegment}.
     *
     * The difference between <code>relatedGlobalTraces</code> and {@link #primaryRef}/{@link #refs} is: {@link
     * #primaryRef}/{@link #refs} targets this {@link TraceSegment}'s direct parent,
     *
     * and
     *
     * <code>relatedGlobalTraces</code> targets this {@link TraceSegment}'s related call chain, a call chain contains
     * multi {@link TraceSegment}s, only using {@link #primaryRef}/{@link #refs} is enough for analysis and ui.
     */
    private LinkedList<DistributedTraceId> relatedGlobalTraces;

    /**
     * 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, String applicationCode) {
        this.traceSegmentId = segmentId;
    public TraceSegment(String applicationCode) {
        this.traceSegmentId = GlobalIdGenerator.generate(ID_TYPE);
        this.applicationCode = applicationCode;
        this.startTime = System.currentTimeMillis();
        this.spans = new LinkedList<Span>();
        this.relatedGlobalTraces = new LinkedList<DistributedTraceId>();
        this.relatedGlobalTraces.add(new NewDistributedTraceId());
    }

    /**
@@ -121,6 +142,16 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
        ref(refSegment, true);
    }

    public void buildRelation(List<DistributedTraceId> distributedTraceIds){
        if(distributedTraceIds == null || distributedTraceIds.size() == 0){
            return;
        }
        if(relatedGlobalTraces.getFirst() instanceof NewDistributedTraceId){
            relatedGlobalTraces.removeFirst();
        }
        relatedGlobalTraces.addAll(distributedTraceIds);
    }

    /**
     * After {@link Span} is finished, as be controller by "skywalking-api" module,
     * notify the {@link TraceSegment} to archive it.
@@ -164,6 +195,10 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
        return Collections.unmodifiableList(refs);
    }

    public List<DistributedTraceId> getRelatedGlobalTraces() {
        return Collections.unmodifiableList(relatedGlobalTraces);
    }

    public List<Span> getSpans() {
        return Collections.unmodifiableList(spans);
    }
@@ -197,6 +232,9 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
                segmentBuilder.addRefs(ref.serialize());
            }
        }
        for (DistributedTraceId id : relatedGlobalTraces) {
            segmentBuilder.addRelatedTraceIds(id.get());
        }
        for (Span span : spans) {
            segmentBuilder.addSpans(span.serialize());
        }
@@ -222,6 +260,11 @@ public class TraceSegment implements ISerializable<SegmentMessage> {
                refs.add(ref);
            }
        }
        ProtocolStringList relatedTraceIdsList = message.getRelatedTraceIdsList();
        this.relatedGlobalTraces = new LinkedList<DistributedTraceId>();
        for (String id : relatedTraceIdsList) {
            relatedGlobalTraces.add(new PropagatedTraceId(id));
        }

        List<SpanMessage> spansList = message.getSpansList();
        if (spansList != null) {
Loading