TracerContext.java 7.1 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 15 16
import java.util.List;

/**
 * {@link TracerContext} maintains the context.
 * You manipulate (create/finish/get) spans and (inject/extract) context.
P
pengys5 已提交
17
 * <p>
18 19 20 21 22 23 24
 * Created by wusheng on 2017/2/17.
 */
public final class TracerContext {
    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 49
        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.
     */
    public Span createSpan(String operationName) {
50 51 52 53 54 55 56
        return this.createSpan(operationName, System.currentTimeMillis());
    }

    /**
     * Create a new span, as an active span, by the given operationName and startTime;
     *
     * @param operationName {@link Span#operationName}
P
pengys5 已提交
57
     * @param startTime     {@link Span#startTime}
58 59
     * @return
     */
60
    public Span createSpan(String operationName, long startTime) {
61 62 63
        Span parentSpan = peek();
        Span span;
        if (parentSpan == null) {
64
            span = new Span(spanIdGenerator++, operationName, startTime);
65
        } else {
66
            span = new Span(spanIdGenerator++, parentSpan, operationName, startTime);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        }
        push(span);
        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) {
89 90 91
        stopSpan(span, System.currentTimeMillis());
    }

92
    /**
93
     * @return the current trace id.
94
     */
95
    String getGlobalTraceId() {
96
        return segment.getRelatedGlobalTraces().get(0).get();
97 98
    }

99
    public void stopSpan(Span span, Long endTime) {
100 101
        Span lastSpan = peek();
        if (lastSpan == span) {
102
            pop().finish(segment, endTime);
103 104 105 106 107
        } else {
            throw new IllegalStateException("Stopping the unexpected span = " + span);
        }

        if (activeSpanStack.isEmpty()) {
108
            this.finish();
109 110 111
        }
    }

wu-sheng's avatar
wu-sheng 已提交
112 113 114
    /**
     * Finish this context, and notify all {@link TracerContextListener}s, managed by {@link ListenerManager}
     */
115
    private void finish() {
wu-sheng's avatar
wu-sheng 已提交
116
        ListenerManager.notifyFinish(segment.finish());
117 118
    }

119 120 121 122 123 124
    /**
     * Give a snapshot of this {@link TracerContext},
     * and save current state to the given {@link ContextCarrier}.
     *
     * @param carrier holds the snapshot
     */
wu-sheng's avatar
wu-sheng 已提交
125
    public void inject(ContextCarrier carrier) {
126
        carrier.setTraceSegmentId(this.segment.getTraceSegmentId());
127 128
        Span span = this.activeSpan();
        carrier.setSpanId(span.getSpanId());
129
        carrier.setApplicationCode(Config.Agent.APPLICATION_CODE);
130
        String host = Tags.PEER_HOST.get(span);
131
        if (host != null) {
132 133
            Integer port = Tags.PEER_PORT.get(span);
            carrier.setPeerHost(host + ":" + port);
134
        } else {
135 136
            carrier.setPeerHost(Tags.PEERS.get(span));
        }
137
        carrier.setDistributedTraceIds(this.segment.getRelatedGlobalTraces());
138
        carrier.setSampled(this.segment.isSampled());
139 140 141 142 143 144
    }

    /**
     * Ref this {@link ContextCarrier} to this {@link TraceSegment}
     *
     * @param carrier holds the snapshot, if get this {@link ContextCarrier} from remote, make sure {@link
P
pengys5 已提交
145
     *                ContextCarrier#deserialize(String)} called.
146
     */
wu-sheng's avatar
wu-sheng 已提交
147
    public void extract(ContextCarrier carrier) {
148
        if (carrier.isValid()) {
wu-sheng's avatar
wu-sheng 已提交
149
            this.segment.ref(getRef(carrier));
150
            ServiceManager.INSTANCE.findService(SamplingService.class).setSampleWhenExtract(this.segment, carrier);
151
            this.segment.relatedGlobalTraces(carrier.getDistributedTraceIds());
wu-sheng's avatar
wu-sheng 已提交
152 153 154
        }
    }

155
    private TraceSegmentRef getRef(ContextCarrier carrier) {
wu-sheng's avatar
wu-sheng 已提交
156 157 158
        TraceSegmentRef ref = new TraceSegmentRef();
        ref.setTraceSegmentId(carrier.getTraceSegmentId());
        ref.setSpanId(carrier.getSpanId());
159 160
        ref.setApplicationCode(carrier.getApplicationCode());
        ref.setPeerHost(carrier.getPeerHost());
wu-sheng's avatar
wu-sheng 已提交
161
        return ref;
162 163 164 165 166 167
    }

    /**
     * @return the top element of 'ActiveSpanStack', and remove it.
     */
    private Span pop() {
168
        return activeSpanStack.removeLast();
169 170 171 172 173 174 175 176
    }

    /**
     * Add a new Span at the top of 'ActiveSpanStack'
     *
     * @param span
     */
    private void push(Span span) {
177
        activeSpanStack.addLast(span);
178 179 180 181 182 183 184 185 186
    }

    /**
     * @return the top element of 'ActiveSpanStack' only.
     */
    private Span peek() {
        if (activeSpanStack.isEmpty()) {
            return null;
        }
187
        return activeSpanStack.getLast();
188
    }
189 190

    public static class ListenerManager {
191
        private static List<TracerContextListener> LISTENERS = new LinkedList<TracerContextListener>();
192 193

        /**
194
         * Add the given {@link TracerContextListener} to {@link #LISTENERS} list.
195 196 197 198
         *
         * @param listener the new listener.
         */
        public static synchronized void add(TracerContextListener listener) {
199
            LISTENERS.add(listener);
200 201 202 203
        }

        /**
         * Notify the {@link ListenerManager} about the given {@link TraceSegment} have finished.
204
         * And trigger {@link ListenerManager} to notify all {@link #LISTENERS} 's
205 206 207 208 209
         * {@link TracerContextListener#afterFinished(TraceSegment)}
         *
         * @param finishedSegment
         */
        static void notifyFinish(TraceSegment finishedSegment) {
210
            for (TracerContextListener listener : LISTENERS) {
211 212 213
                listener.afterFinished(finishedSegment);
            }
        }
wu-sheng's avatar
wu-sheng 已提交
214 215 216 217

        /**
         * Clear the given {@link TracerContextListener}
         */
218 219
        public static synchronized void remove(TracerContextListener listener) {
            LISTENERS.remove(listener);
wu-sheng's avatar
wu-sheng 已提交
220
        }
221
    }
222
}