TracerContext.java 7.9 KB
Newer Older
1
package org.skywalking.apm.agent.core.context;
P
pengys5 已提交
2

3 4 5
import org.skywalking.apm.agent.core.conf.Config;
import org.skywalking.apm.agent.core.boot.ServiceManager;
import org.skywalking.apm.agent.core.sampling.SamplingService;
P
pengys5 已提交
6 7 8 9 10
import org.skywalking.apm.trace.Span;
import org.skywalking.apm.trace.TraceSegment;
import org.skywalking.apm.trace.TraceSegmentRef;
import org.skywalking.apm.trace.tag.Tags;

11
import java.util.LinkedList;
12 13 14
import java.util.List;

/**
15
 * {@link TracerContext} maintains the context.
16
 * You manipulate (create/finish/get) spans and (inject/extract) context.
P
pengys5 已提交
17
 * <p>
18 19
 * Created by wusheng on 2017/2/17.
 */
20
public final class TracerContext {
21 22 23 24
    private TraceSegment segment;

    /**
     * Active spans stored in a Stack, usually called 'ActiveSpanStack'.
25
     * This {@link LinkedList} is the in-memory storage-structure.
P
pengys5 已提交
26
     * <p>
27
     * I use {@link LinkedList#removeLast()}, {@link LinkedList#addLast(Object)} and {@link LinkedList#last} instead of
28 29
     * {@link #pop()}, {@link #push(Span)}, {@link #peek()}
     */
30
    private LinkedList<Span> activeSpanStack = new LinkedList<Span>();
31 32 33

    private int spanIdGenerator;

34 35 36
    /**
     * Create a {@link TraceSegment} and init {@link #spanIdGenerator} as 0;
     */
37
    TracerContext() {
38
        this.segment = new TraceSegment(Config.Agent.APPLICATION_CODE);
39
        ServiceManager.INSTANCE.findService(SamplingService.class).trySampling(this.segment);
40 41 42 43 44 45 46 47 48
        this.spanIdGenerator = 0;
    }

    /**
     * Create a new span, as an active span, by the given operationName
     *
     * @param operationName {@link Span#operationName}
     * @return the new active span.
     */
49 50
    public Span createSpan(String operationName, boolean isLeaf) {
        return this.createSpan(operationName, System.currentTimeMillis(), isLeaf);
51 52 53 54 55 56
    }

    /**
     * Create a new span, as an active span, by the given operationName and startTime;
     *
     * @param operationName {@link Span#operationName}
57 58
     * @param startTime {@link Span#startTime}
     * @param isLeaf is true, if the span is a leaf in trace tree.
59 60
     * @return
     */
61
    public Span createSpan(String operationName, long startTime, boolean isLeaf) {
62 63 64
        Span parentSpan = peek();
        Span span;
        if (parentSpan == null) {
65 66 67 68 69 70 71 72 73 74
            if (isLeaf) {
                span = new LeafSpan(spanIdGenerator++, operationName, startTime);
            } else {
                span = new Span(spanIdGenerator++, operationName, startTime);
            }
            push(span);
        } else if (parentSpan.isLeaf()) {
            span = parentSpan;
            LeafSpan leafSpan = (LeafSpan)span;
            leafSpan.push();
75
        } else {
76 77 78 79 80 81
            if (isLeaf) {
                span = new LeafSpan(spanIdGenerator++, parentSpan, operationName, startTime);
            } else {
                span = new Span(spanIdGenerator++, parentSpan, operationName, startTime);
            }
            push(span);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        }
        return span;
    }

    /**
     * @return the active span of current context.
     */
    public Span activeSpan() {
        Span span = peek();
        if (span == null) {
            throw new IllegalStateException("No active span.");
        }
        return span;
    }

    /**
     * Stop the span. And finish the {@link #segment} if all {@link #activeSpanStack} elements are finished.
     *
     * @param span to finish. It must the the top element of {@link #activeSpanStack}.
     */
    public void stopSpan(Span span) {
103 104 105
        stopSpan(span, System.currentTimeMillis());
    }

106
    /**
107
     * @return the current trace id.
108
     */
109
    String getGlobalTraceId() {
110
        return segment.getRelatedGlobalTraces().get(0).get();
111 112
    }

113
    public void stopSpan(Span span, Long endTime) {
114
        Span lastSpan = peek();
115 116 117 118 119 120 121
        if (lastSpan.isLeaf()) {
            LeafSpan leafSpan = (LeafSpan)lastSpan;
            leafSpan.pop();
            if (!leafSpan.isFinished()) {
                return;
            }
        }
122
        if (lastSpan == span) {
123
            pop().finish(segment, endTime);
124 125 126 127 128
        } else {
            throw new IllegalStateException("Stopping the unexpected span = " + span);
        }

        if (activeSpanStack.isEmpty()) {
129
            this.finish();
130 131 132
        }
    }

wu-sheng's avatar
wu-sheng 已提交
133 134 135
    /**
     * Finish this context, and notify all {@link TracerContextListener}s, managed by {@link ListenerManager}
     */
136
    private void finish() {
wu-sheng's avatar
wu-sheng 已提交
137
        ListenerManager.notifyFinish(segment.finish());
138 139
    }

140
    /**
141
     * Give a snapshot of this {@link TracerContext},
142 143 144 145
     * and save current state to the given {@link ContextCarrier}.
     *
     * @param carrier holds the snapshot
     */
wu-sheng's avatar
wu-sheng 已提交
146
    public void inject(ContextCarrier carrier) {
147
        carrier.setTraceSegmentId(this.segment.getTraceSegmentId());
148 149
        Span span = this.activeSpan();
        carrier.setSpanId(span.getSpanId());
150
        carrier.setApplicationCode(Config.Agent.APPLICATION_CODE);
151
        String host = Tags.PEER_HOST.get(span);
152
        if (host != null) {
153 154
            Integer port = Tags.PEER_PORT.get(span);
            carrier.setPeerHost(host + ":" + port);
155
        } else {
156 157
            carrier.setPeerHost(Tags.PEERS.get(span));
        }
158
        carrier.setDistributedTraceIds(this.segment.getRelatedGlobalTraces());
159
        carrier.setSampled(this.segment.isSampled());
160 161 162 163 164 165
    }

    /**
     * Ref this {@link ContextCarrier} to this {@link TraceSegment}
     *
     * @param carrier holds the snapshot, if get this {@link ContextCarrier} from remote, make sure {@link
166
     * ContextCarrier#deserialize(String)} called.
167
     */
168
    public void extract(ContextCarrier carrier) {
169
        if (carrier.isValid()) {
wu-sheng's avatar
wu-sheng 已提交
170
            this.segment.ref(getRef(carrier));
171
            ServiceManager.INSTANCE.findService(SamplingService.class).setSampleWhenExtract(this.segment, carrier);
172
            this.segment.relatedGlobalTraces(carrier.getDistributedTraceIds());
wu-sheng's avatar
wu-sheng 已提交
173 174 175
        }
    }

176
    private TraceSegmentRef getRef(ContextCarrier carrier) {
wu-sheng's avatar
wu-sheng 已提交
177 178 179
        TraceSegmentRef ref = new TraceSegmentRef();
        ref.setTraceSegmentId(carrier.getTraceSegmentId());
        ref.setSpanId(carrier.getSpanId());
180 181
        ref.setApplicationCode(carrier.getApplicationCode());
        ref.setPeerHost(carrier.getPeerHost());
wu-sheng's avatar
wu-sheng 已提交
182
        return ref;
183 184 185 186 187 188
    }

    /**
     * @return the top element of 'ActiveSpanStack', and remove it.
     */
    private Span pop() {
189
        return activeSpanStack.removeLast();
190 191 192 193 194 195 196 197
    }

    /**
     * Add a new Span at the top of 'ActiveSpanStack'
     *
     * @param span
     */
    private void push(Span span) {
198
        activeSpanStack.addLast(span);
199 200 201 202 203 204 205 206 207
    }

    /**
     * @return the top element of 'ActiveSpanStack' only.
     */
    private Span peek() {
        if (activeSpanStack.isEmpty()) {
            return null;
        }
208
        return activeSpanStack.getLast();
209
    }
210 211

    public static class ListenerManager {
212
        private static List<TracerContextListener> LISTENERS = new LinkedList<TracerContextListener>();
213 214

        /**
215
         * Add the given {@link TracerContextListener} to {@link #LISTENERS} list.
216 217 218 219
         *
         * @param listener the new listener.
         */
        public static synchronized void add(TracerContextListener listener) {
220
            LISTENERS.add(listener);
221 222 223 224
        }

        /**
         * Notify the {@link ListenerManager} about the given {@link TraceSegment} have finished.
225
         * And trigger {@link ListenerManager} to notify all {@link #LISTENERS} 's
226 227 228 229 230
         * {@link TracerContextListener#afterFinished(TraceSegment)}
         *
         * @param finishedSegment
         */
        static void notifyFinish(TraceSegment finishedSegment) {
231
            for (TracerContextListener listener : LISTENERS) {
232 233 234
                listener.afterFinished(finishedSegment);
            }
        }
wu-sheng's avatar
wu-sheng 已提交
235 236 237 238

        /**
         * Clear the given {@link TracerContextListener}
         */
239 240
        public static synchronized void remove(TracerContextListener listener) {
            LISTENERS.remove(listener);
wu-sheng's avatar
wu-sheng 已提交
241
        }
242
    }
243
}