TomcatSpanGenerator.java 2.8 KB
Newer Older
P
pengys5 已提交
1
package org.skywalking.apm.sniffer.mock.trace.builders.span;
2

3
import org.skywalking.apm.agent.core.context.ContextManager;
P
pengys5 已提交
4 5
import org.skywalking.apm.trace.Span;
import org.skywalking.apm.trace.tag.Tags;
6 7 8

/**
 * The <code>TomcatSpanGenerator</code> generate all possible spans, by tracing Tomcat.
P
pengys5 已提交
9
 * <p>
10 11
 * Created by wusheng on 2017/2/20.
 */
12 13
public class TomcatSpanGenerator {
    public static class ON200 extends SpanGeneration {
14 15
        public static final ON200 INSTANCE = new ON200();

P
pengys5 已提交
16 17
        @Override
        protected void before() {
18
            Span webSpan = ContextManager.createSpan("/web/serviceA");
19
            Tags.COMPONENT.set(webSpan, "Tomcat");
20 21
            Tags.URL.set(webSpan, "http://10.21.9.35/web/serviceA");
            Tags.SPAN_KIND.set(webSpan, Tags.SPAN_KIND_SERVER);
wu-sheng's avatar
wu-sheng 已提交
22 23
            webSpan.setPeer_host("10.21.9.35");
            webSpan.setPort(80);
24
            Tags.SPAN_LAYER.asHttp(webSpan);
25 26
        }

P
pengys5 已提交
27 28
        @Override
        protected void after() {
29
            Span webSpan = ContextManager.activeSpan();
30
            Tags.STATUS_CODE.set(webSpan, 200);
31
            ContextManager.stopSpan();
32
        }
33 34
    }

35
    public static class ON404 extends SpanGeneration {
36 37
        public static final ON404 INSTANCE = new ON404();

P
pengys5 已提交
38 39
        @Override
        protected void before() {
40
            Span webSpan = ContextManager.createSpan("/web/service/unknown");
41
            Tags.COMPONENT.set(webSpan, "Tomcat");
42 43
            Tags.URL.set(webSpan, "http://10.21.9.35/web/unknown");
            Tags.SPAN_KIND.set(webSpan, Tags.SPAN_KIND_SERVER);
wu-sheng's avatar
wu-sheng 已提交
44 45
            webSpan.setPeer_host("10.21.9.35");
            webSpan.setPort(80);
46
            Tags.SPAN_LAYER.asHttp(webSpan);
47 48
        }

P
pengys5 已提交
49 50
        @Override
        protected void after() {
51
            Span webSpan = ContextManager.activeSpan();
52
            Tags.STATUS_CODE.set(webSpan, 404);
53
            Tags.ERROR.set(webSpan, true);
54
            ContextManager.stopSpan();
55
        }
56
    }
57 58

    public static class ON500 extends SpanGeneration {
59 60
        public static final ON500 INSTANCE = new ON500();

P
pengys5 已提交
61 62
        @Override
        protected void before() {
63
            Span webSpan = ContextManager.createSpan("/web/error/service");
64
            Tags.COMPONENT.set(webSpan, "Tomcat");
65 66
            Tags.URL.set(webSpan, "http://10.21.9.35/web/error/service");
            Tags.SPAN_KIND.set(webSpan, Tags.SPAN_KIND_SERVER);
wu-sheng's avatar
wu-sheng 已提交
67 68
            webSpan.setPeer_host("10.21.9.35");
            webSpan.setPort(80);
69
            Tags.SPAN_LAYER.asHttp(webSpan);
70
        }
71

P
pengys5 已提交
72 73
        @Override
        protected void after() {
74
            Span webSpan = ContextManager.activeSpan();
75
            Tags.STATUS_CODE.set(webSpan, 500);
76
            Tags.ERROR.set(webSpan, true);
77
            webSpan.log(new NumberFormatException("Can't convert 'abc' to int."));
78
            ContextManager.stopSpan();
79
        }
80 81
    }
}