From a4d2f237912d431b9cabf07559acf77d1f866eb4 Mon Sep 17 00:00:00 2001 From: wusheng Date: Mon, 20 Feb 2017 22:34:47 +0800 Subject: [PATCH 1/7] Add skywalking-sniffer-mock module. For testing sdk-plugin and collector(not merged yet) modules. --- skywalking-sniffer/pom.xml | 1 + skywalking-sniffer/skywalking-api/pom.xml | 20 -------- .../skywalking-sniffer-mock/pom.xml | 30 +++++++++++ .../context/MockTracerContextListener.java | 51 +++++++++++++++++++ .../sniffer/mock/context/SegmentAssert.java | 13 +++++ 5 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 skywalking-sniffer/skywalking-sniffer-mock/pom.xml create mode 100644 skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/MockTracerContextListener.java create mode 100644 skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/SegmentAssert.java diff --git a/skywalking-sniffer/pom.xml b/skywalking-sniffer/pom.xml index 2661cf4d9..a6aef14eb 100644 --- a/skywalking-sniffer/pom.xml +++ b/skywalking-sniffer/pom.xml @@ -17,6 +17,7 @@ skywalking-api skywalking-sdk-plugin skywalking-toolkit-activation + skywalking-sniffer-mock diff --git a/skywalking-sniffer/skywalking-api/pom.xml b/skywalking-sniffer/skywalking-api/pom.xml index e11ee28cc..5774f18bd 100644 --- a/skywalking-sniffer/skywalking-api/pom.xml +++ b/skywalking-sniffer/skywalking-api/pom.xml @@ -51,26 +51,6 @@ disruptor 3.3.6 - - - - junit - junit - 4.11 - test - - - org.powermock - powermock-api-mockito - 1.6.5 - test - - - org.powermock - powermock-module-junit4 - 1.6.5 - test - diff --git a/skywalking-sniffer/skywalking-sniffer-mock/pom.xml b/skywalking-sniffer/skywalking-sniffer-mock/pom.xml new file mode 100644 index 000000000..b18693f11 --- /dev/null +++ b/skywalking-sniffer/skywalking-sniffer-mock/pom.xml @@ -0,0 +1,30 @@ + + + + skywalking-sniffer + com.a.eye + 3.0-2017 + + 4.0.0 + skywalking-sniffer-mock + This is a sniffer mock module, test for test-dependency, only. + + + + com.a.eye + skywalking-api + ${project.version} + + + + + junit + junit + 4.12 + + + + + diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/MockTracerContextListener.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/MockTracerContextListener.java new file mode 100644 index 000000000..ec7e1ccde --- /dev/null +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/MockTracerContextListener.java @@ -0,0 +1,51 @@ +package com.a.eye.skywalking.sniffer.mock.context; + +import com.a.eye.skywalking.api.context.TracerContext; +import com.a.eye.skywalking.api.context.TracerContextListener; +import com.a.eye.skywalking.trace.TraceSegment; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.junit.Assert; + +/** + * This is mock tracer context listener, which should be added by calling {@link TracerContext.ListenerManager#add(TracerContextListener)}. + * This mock listener will hold all finished trace, which all are generated by {@link TracerContext#finish()}. + * + * Created by wusheng on 2017/2/20. + */ +public class MockTracerContextListener implements TracerContextListener { + private List finishedTraceSegments = Collections.synchronizedList(new ArrayList()); + + @Override public void afterFinished(TraceSegment traceSegment) { + finishedTraceSegments.add(traceSegment); + } + + /** + * Assert all finished {@link #finishedTraceSegments} match the given size. + * + * @param size the give size. + */ + public void assertSize(int size){ + Assert.assertEquals(size, finishedTraceSegments.size()); + } + + /** + * Assert the {@link TraceSegment} at the given index of {@link #finishedTraceSegments}, + * and run the given {@link SegmentAssert#call(TraceSegment)} to assert. + * + * @param index the given index. + * @param segmentAssert the given assert. + */ + public void assertTraceSegment(int index, SegmentAssert segmentAssert){ + assertSize(index + 1); + segmentAssert.call(finishedTraceSegments.get(index)); + } + + /** + * Clear all hold data. + */ + public void clear(){ + finishedTraceSegments.clear(); + } +} diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/SegmentAssert.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/SegmentAssert.java new file mode 100644 index 000000000..52a2b66d7 --- /dev/null +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/SegmentAssert.java @@ -0,0 +1,13 @@ +package com.a.eye.skywalking.sniffer.mock.context; + +import com.a.eye.skywalking.trace.TraceSegment; + +/** + * The SegmentAssert interface should be implemented by any + * class whose instances are intended to assert a trace segment data. + * + * Created by wusheng on 2017/2/20. + */ +public interface SegmentAssert{ + void call(TraceSegment finishedSegment); +} From 69877b80fdebf84a7e703d8b9b18e4a047cab1c7 Mon Sep 17 00:00:00 2001 From: wusheng Date: Mon, 20 Feb 2017 22:52:26 +0800 Subject: [PATCH 2/7] Give a sample code of the first Mock trace. --- .../mock/trace/TraceSegmentBuilder.java | 10 ++++++ .../trace/TraceSegmentBuilderFactory.java | 35 +++++++++++++++++++ .../builders/SingleTomcat200TraceBuilder.java | 27 ++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilder.java create mode 100644 skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilderFactory.java create mode 100644 skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/SingleTomcat200TraceBuilder.java diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilder.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilder.java new file mode 100644 index 000000000..57e48fd19 --- /dev/null +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilder.java @@ -0,0 +1,10 @@ +package com.a.eye.skywalking.sniffer.mock.trace; + +import com.a.eye.skywalking.trace.TraceSegment; + +/** + * Created by wusheng on 2017/2/20. + */ +public interface TraceSegmentBuilder { + TraceSegment build(); +} diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilderFactory.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilderFactory.java new file mode 100644 index 000000000..6714ced0b --- /dev/null +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilderFactory.java @@ -0,0 +1,35 @@ +package com.a.eye.skywalking.sniffer.mock.trace; + +import com.a.eye.skywalking.sniffer.mock.trace.builders.SingleTomcat200TraceBuilder; +import com.a.eye.skywalking.trace.TraceSegment; +import java.util.HashMap; +import java.util.Map; + +/** + * The TraceSegmentBuilderFactory contains all {@link TraceSegmentBuilder} implementations. + * All the implementations can build a true {@link TraceSegment} object, and contain all necessary spans, with all tags/events, all refs. + * + * Created by wusheng on 2017/2/20. + */ +public enum TraceSegmentBuilderFactory { + INSTANCE; + + private Map allBuilders; + + TraceSegmentBuilderFactory(){ + allBuilders = new HashMap(); + initialize(); + } + + public TraceSegment singleTomcat200Trace(){ + return allBuilders.get(SingleTomcat200TraceBuilder.INSTANCE).build(); + } + + private void initialize(){ + initBuilder(SingleTomcat200TraceBuilder.INSTANCE); + } + + private void initBuilder(TraceSegmentBuilder builder){ + allBuilders.put(builder.toString(), builder); + } +} diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/SingleTomcat200TraceBuilder.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/SingleTomcat200TraceBuilder.java new file mode 100644 index 000000000..d84eb8d79 --- /dev/null +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/SingleTomcat200TraceBuilder.java @@ -0,0 +1,27 @@ +package com.a.eye.skywalking.sniffer.mock.trace.builders; + +import com.a.eye.skywalking.api.context.ContextManager; +import com.a.eye.skywalking.sniffer.mock.trace.TraceSegmentBuilder; +import com.a.eye.skywalking.trace.Span; +import com.a.eye.skywalking.trace.TraceSegment; +import com.a.eye.skywalking.trace.tag.Tags; + +/** + * A Trace contains only one span, which represent a tomcat server side span. + * + * Created by wusheng on 2017/2/20. + */ +public enum SingleTomcat200TraceBuilder implements TraceSegmentBuilder { + INSTANCE; + + @Override public TraceSegment build() { + Span webSpan = ContextManager.INSTANCE.createSpan("/web/serviceA"); + { + Tags.COMPONENT.set(webSpan, "tomcat"); + Tags.URL.set(webSpan, "http://10.21.9.35/web/serviceA"); + Tags.SPAN_KIND.set(webSpan, Tags.SPAN_KIND_SERVER); + Tags.STATUS_CODE.set(webSpan, 200); + } + return null; + } +} From 96f6c5fad94e57d2dec30ae7090bf5eaae8c64f8 Mon Sep 17 00:00:00 2001 From: wusheng Date: Mon, 20 Feb 2017 23:34:34 +0800 Subject: [PATCH 3/7] Add Tomcat span, and singleTrace mock. --- .../skywalking/api/context/TracerContext.java | 2 +- .../context/MockTracerContextListener.java | 10 ++++ .../mock/trace/TraceSegmentBuilder.java | 3 +- .../trace/TraceSegmentBuilderFactory.java | 51 ++++++++++++------ .../builders/SingleTomcat200TraceBuilder.java | 27 ---------- .../builders/span/TomcatSpanGenerator.java | 53 +++++++++++++++++++ .../trace/SingleTomcat200TraceBuilder.java | 20 +++++++ .../trace/SingleTomcat404TraceBuilder.java | 20 +++++++ .../trace/SingleTomcat500TraceBuilder.java | 20 +++++++ 9 files changed, 162 insertions(+), 44 deletions(-) delete mode 100644 skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/SingleTomcat200TraceBuilder.java create mode 100644 skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/span/TomcatSpanGenerator.java create mode 100644 skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/trace/SingleTomcat200TraceBuilder.java create mode 100644 skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/trace/SingleTomcat404TraceBuilder.java create mode 100644 skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/trace/SingleTomcat500TraceBuilder.java diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java index ea8faf001..4ef3d3188 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java @@ -171,7 +171,7 @@ public final class TracerContext { /** * Clear the given {@link TracerContextListener} */ - static synchronized void remove(TracerContextListener listener){ + public static synchronized void remove(TracerContextListener listener){ listeners.remove(listener); } } diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/MockTracerContextListener.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/MockTracerContextListener.java index ec7e1ccde..a79f80e1a 100644 --- a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/MockTracerContextListener.java +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/MockTracerContextListener.java @@ -48,4 +48,14 @@ public class MockTracerContextListener implements TracerContextListener { public void clear(){ finishedTraceSegments.clear(); } + + /** + * Get {@link TraceSegment} of the given index. + * @param index + * @return + */ + public TraceSegment getFinished(int index){ + assertSize(index + 1); + return finishedTraceSegments.get(index); + } } diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilder.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilder.java index 57e48fd19..55743677a 100644 --- a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilder.java +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilder.java @@ -1,10 +1,11 @@ package com.a.eye.skywalking.sniffer.mock.trace; +import com.a.eye.skywalking.sniffer.mock.context.MockTracerContextListener; import com.a.eye.skywalking.trace.TraceSegment; /** * Created by wusheng on 2017/2/20. */ public interface TraceSegmentBuilder { - TraceSegment build(); + TraceSegment build(MockTracerContextListener listener); } diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilderFactory.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilderFactory.java index 6714ced0b..f13b49a8f 100644 --- a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilderFactory.java +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/TraceSegmentBuilderFactory.java @@ -1,9 +1,11 @@ package com.a.eye.skywalking.sniffer.mock.trace; -import com.a.eye.skywalking.sniffer.mock.trace.builders.SingleTomcat200TraceBuilder; +import com.a.eye.skywalking.api.context.TracerContext; +import com.a.eye.skywalking.sniffer.mock.context.MockTracerContextListener; +import com.a.eye.skywalking.sniffer.mock.trace.builders.trace.SingleTomcat200TraceBuilder; +import com.a.eye.skywalking.sniffer.mock.trace.builders.trace.SingleTomcat404TraceBuilder; +import com.a.eye.skywalking.sniffer.mock.trace.builders.trace.SingleTomcat500TraceBuilder; import com.a.eye.skywalking.trace.TraceSegment; -import java.util.HashMap; -import java.util.Map; /** * The TraceSegmentBuilderFactory contains all {@link TraceSegmentBuilder} implementations. @@ -14,22 +16,41 @@ import java.util.Map; public enum TraceSegmentBuilderFactory { INSTANCE; - private Map allBuilders; - - TraceSegmentBuilderFactory(){ - allBuilders = new HashMap(); - initialize(); - } - + /** + * @see {@link SingleTomcat200TraceBuilder} + * + * @return + */ public TraceSegment singleTomcat200Trace(){ - return allBuilders.get(SingleTomcat200TraceBuilder.INSTANCE).build(); + return this.build(SingleTomcat200TraceBuilder.INSTANCE); } - private void initialize(){ - initBuilder(SingleTomcat200TraceBuilder.INSTANCE); + /** + * @see {@link SingleTomcat404TraceBuilder} + * + * @return + */ + public TraceSegment singleTomcat404Trace(){ + return this.build(SingleTomcat404TraceBuilder.INSTANCE); } - private void initBuilder(TraceSegmentBuilder builder){ - allBuilders.put(builder.toString(), builder); + /** + * @see {@link SingleTomcat500TraceBuilder} + * + * @return + */ + public TraceSegment singleTomcat500Trace(){ + return this.build(SingleTomcat500TraceBuilder.INSTANCE); } + + private TraceSegment build(TraceSegmentBuilder builder){ + MockTracerContextListener listener = new MockTracerContextListener(); + try{ + TracerContext.ListenerManager.add(listener); + return builder.build(listener); + }finally{ + TracerContext.ListenerManager.remove(listener); + } + } + } diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/SingleTomcat200TraceBuilder.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/SingleTomcat200TraceBuilder.java deleted file mode 100644 index d84eb8d79..000000000 --- a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/SingleTomcat200TraceBuilder.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.a.eye.skywalking.sniffer.mock.trace.builders; - -import com.a.eye.skywalking.api.context.ContextManager; -import com.a.eye.skywalking.sniffer.mock.trace.TraceSegmentBuilder; -import com.a.eye.skywalking.trace.Span; -import com.a.eye.skywalking.trace.TraceSegment; -import com.a.eye.skywalking.trace.tag.Tags; - -/** - * A Trace contains only one span, which represent a tomcat server side span. - * - * Created by wusheng on 2017/2/20. - */ -public enum SingleTomcat200TraceBuilder implements TraceSegmentBuilder { - INSTANCE; - - @Override public TraceSegment build() { - Span webSpan = ContextManager.INSTANCE.createSpan("/web/serviceA"); - { - Tags.COMPONENT.set(webSpan, "tomcat"); - Tags.URL.set(webSpan, "http://10.21.9.35/web/serviceA"); - Tags.SPAN_KIND.set(webSpan, Tags.SPAN_KIND_SERVER); - Tags.STATUS_CODE.set(webSpan, 200); - } - return null; - } -} diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/span/TomcatSpanGenerator.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/span/TomcatSpanGenerator.java new file mode 100644 index 000000000..c9096cf26 --- /dev/null +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/span/TomcatSpanGenerator.java @@ -0,0 +1,53 @@ +package com.a.eye.skywalking.sniffer.mock.trace.builders.span; + +import com.a.eye.skywalking.api.context.ContextManager; +import com.a.eye.skywalking.trace.Span; +import com.a.eye.skywalking.trace.tag.Tags; + +/** + * The TomcatSpanGenerator generate all possible spans, by tracing Tomcat. + * + * Created by wusheng on 2017/2/20. + */ +public enum TomcatSpanGenerator { + INSTANCE; + + /** + * When tomcat response 200. + */ + public void on200(){ + Span webSpan = ContextManager.INSTANCE.createSpan("/web/serviceA"); + Tags.COMPONENT.set(webSpan, "tomcat"); + Tags.URL.set(webSpan, "http://10.21.9.35/web/serviceA"); + Tags.SPAN_KIND.set(webSpan, Tags.SPAN_KIND_SERVER); + Tags.STATUS_CODE.set(webSpan, 200); + ContextManager.INSTANCE.stopSpan(webSpan); + } + + /** + * When tomcat response 404. + */ + public void on404(){ + Span webSpan = ContextManager.INSTANCE.createSpan("/web/service/unknown"); + Tags.COMPONENT.set(webSpan, "tomcat"); + Tags.URL.set(webSpan, "http://10.21.9.35/web/unknown"); + Tags.SPAN_KIND.set(webSpan, Tags.SPAN_KIND_SERVER); + Tags.STATUS_CODE.set(webSpan, 404); + Tags.ERROR.set(webSpan,true); + ContextManager.INSTANCE.stopSpan(webSpan); + } + + /** + * When tomcat response 500. + */ + public void on500(){ + Span webSpan = ContextManager.INSTANCE.createSpan("/web/error/service"); + Tags.COMPONENT.set(webSpan, "tomcat"); + Tags.URL.set(webSpan, "http://10.21.9.35/web/error/service"); + Tags.SPAN_KIND.set(webSpan, Tags.SPAN_KIND_SERVER); + Tags.STATUS_CODE.set(webSpan, 500); + Tags.ERROR.set(webSpan,true); + webSpan.log(new NumberFormatException("Can't convert 'abc' to int.")); + ContextManager.INSTANCE.stopSpan(webSpan); + } +} diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/trace/SingleTomcat200TraceBuilder.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/trace/SingleTomcat200TraceBuilder.java new file mode 100644 index 000000000..5812b24de --- /dev/null +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/trace/SingleTomcat200TraceBuilder.java @@ -0,0 +1,20 @@ +package com.a.eye.skywalking.sniffer.mock.trace.builders.trace; + +import com.a.eye.skywalking.sniffer.mock.context.MockTracerContextListener; +import com.a.eye.skywalking.sniffer.mock.trace.TraceSegmentBuilder; +import com.a.eye.skywalking.sniffer.mock.trace.builders.span.TomcatSpanGenerator; +import com.a.eye.skywalking.trace.TraceSegment; + +/** + * A Trace contains only one span, which represent a tomcat server side span. + * + * Created by wusheng on 2017/2/20. + */ +public enum SingleTomcat200TraceBuilder implements TraceSegmentBuilder { + INSTANCE; + + @Override public TraceSegment build(MockTracerContextListener listener) { + TomcatSpanGenerator.INSTANCE.on200(); + return listener.getFinished(0); + } +} diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/trace/SingleTomcat404TraceBuilder.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/trace/SingleTomcat404TraceBuilder.java new file mode 100644 index 000000000..7ec32e46e --- /dev/null +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/trace/SingleTomcat404TraceBuilder.java @@ -0,0 +1,20 @@ +package com.a.eye.skywalking.sniffer.mock.trace.builders.trace; + +import com.a.eye.skywalking.sniffer.mock.context.MockTracerContextListener; +import com.a.eye.skywalking.sniffer.mock.trace.TraceSegmentBuilder; +import com.a.eye.skywalking.sniffer.mock.trace.builders.span.TomcatSpanGenerator; +import com.a.eye.skywalking.trace.TraceSegment; + +/** + * A Trace contains only one span, which represent a tomcat server side span. + * + * Created by wusheng on 2017/2/20. + */ +public enum SingleTomcat404TraceBuilder implements TraceSegmentBuilder { + INSTANCE; + + @Override public TraceSegment build(MockTracerContextListener listener) { + TomcatSpanGenerator.INSTANCE.on404(); + return listener.getFinished(0); + } +} diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/trace/SingleTomcat500TraceBuilder.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/trace/SingleTomcat500TraceBuilder.java new file mode 100644 index 000000000..150c4ab6e --- /dev/null +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/trace/builders/trace/SingleTomcat500TraceBuilder.java @@ -0,0 +1,20 @@ +package com.a.eye.skywalking.sniffer.mock.trace.builders.trace; + +import com.a.eye.skywalking.sniffer.mock.context.MockTracerContextListener; +import com.a.eye.skywalking.sniffer.mock.trace.TraceSegmentBuilder; +import com.a.eye.skywalking.sniffer.mock.trace.builders.span.TomcatSpanGenerator; +import com.a.eye.skywalking.trace.TraceSegment; + +/** + * A Trace contains only one span, which represent a tomcat server side span. + * + * Created by wusheng on 2017/2/20. + */ +public enum SingleTomcat500TraceBuilder implements TraceSegmentBuilder { + INSTANCE; + + @Override public TraceSegment build(MockTracerContextListener listener) { + TomcatSpanGenerator.INSTANCE.on500(); + return listener.getFinished(0); + } +} From b69cd851499ba0ecb19fad694753446d2bcf1909 Mon Sep 17 00:00:00 2001 From: wusheng Date: Tue, 21 Feb 2017 09:38:02 +0800 Subject: [PATCH 4/7] fix compile issues --- .../a/eye/skywalking/trace/SpanTestCase.java | 34 ++----------------- .../skywalking/api/logging/EasyLogger.java | 4 +-- .../api/logging/SyncFileWriter.java | 4 +-- .../skywalking/api/logging/WriterFactory.java | 2 +- .../plugin/interceptor/EnhanceException.java | 2 +- .../assist/NoCocurrencyAceessObject.java | 2 +- .../enhance/ClassEnhancePluginDefine.java | 7 +++- ...assInstanceMethodsEnhancePluginDefine.java | 1 - ...ClassStaticMethodsEnhancePluginDefine.java | 2 +- .../api/queue/TraceSegmentProcessQueue.java | 6 ++-- .../skywalking/api/util/TraceIdGenerator.java | 2 +- .../api/plugin/TestAroundInterceptor.java | 3 +- .../api/plugin/TestInterceptorDefine.java | 2 -- .../plugin/TestStaticAroundInterceptor.java | 2 +- .../MockTracerContextListenerTestCase.java | 19 +++++++++++ 15 files changed, 41 insertions(+), 51 deletions(-) create mode 100644 skywalking-sniffer/skywalking-sniffer-mock/src/test/java/com/a/eye/skywalking/sniffer/mock/MockTracerContextListenerTestCase.java diff --git a/skywalking-commons/skywalking-trace/src/test/java/com/a/eye/skywalking/trace/SpanTestCase.java b/skywalking-commons/skywalking-trace/src/test/java/com/a/eye/skywalking/trace/SpanTestCase.java index 27209fc92..8108a2aad 100644 --- a/skywalking-commons/skywalking-trace/src/test/java/com/a/eye/skywalking/trace/SpanTestCase.java +++ b/skywalking-commons/skywalking-trace/src/test/java/com/a/eye/skywalking/trace/SpanTestCase.java @@ -39,7 +39,7 @@ public class SpanTestCase { Span span1 = new Span(0, "serviceA"); Tags.SPAN_LAYER.asHttp(span1); Tags.COMPONENT.set(span1, "Spring"); - Tags.PEER_HOST.set(span1, ipToInt("127.0.0.1")); + Tags.PEER_HOST.set(span1, "127.0.0.1"); Tags.ERROR.set(span1, true); Tags.STATUS_CODE.set(span1, 302); Tags.URL.set(span1, "http://127.0.0.1/serviceA"); @@ -49,40 +49,10 @@ public class SpanTestCase { Map tags = span1.getTags(); Assert.assertEquals(8, tags.size()); Assert.assertTrue(Tags.SPAN_LAYER.isHttp(span1)); - Assert.assertEquals("127.0.0.1", intToIp(Tags.PEER_HOST.get(span1))); + Assert.assertEquals("127.0.0.1", Tags.PEER_HOST.get(span1)); Assert.assertTrue(Tags.ERROR.get(span1)); } - private int ipToInt(String ipAddress) { - int result = 0; - String[] ipAddressInArray = ipAddress.split("\\."); - - for (int i = 3; i >= 0; i--) { - - int ip = Integer.parseInt(ipAddressInArray[3 - i]); - - //left shifting 24,16,8,0 and bitwise OR - //1. 192 << 24 - //1. 168 << 16 - //1. 1 << 8 - //1. 2 << 0 - result |= ip << (i * 8); - } - return result; - } - - private static String intToIp(int longIp) { - StringBuffer sb = new StringBuffer(""); - sb.append(String.valueOf((longIp >>> 24))); - sb.append("."); - sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16)); - sb.append("."); - sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8)); - sb.append("."); - sb.append(String.valueOf((longIp & 0x000000FF))); - return sb.toString(); - } - @Test public void testLogException(){ Span span1 = new Span(0, "serviceA"); diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/EasyLogger.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/EasyLogger.java index d5e9dfa12..286366f4c 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/EasyLogger.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/EasyLogger.java @@ -2,13 +2,13 @@ package com.a.eye.skywalking.api.logging; import com.a.eye.skywalking.api.logging.api.ILog; -import com.a.eye.skywalking.util.LoggingUtil; +import com.a.eye.skywalking.api.util.LoggingUtil; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; -import static com.a.eye.skywalking.logging.LogLevel.*; +import static com.a.eye.skywalking.api.logging.LogLevel.*; /** * Created by xin on 16-6-23. diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/SyncFileWriter.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/SyncFileWriter.java index 21cfd4872..579e098ba 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/SyncFileWriter.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/SyncFileWriter.java @@ -1,9 +1,9 @@ package com.a.eye.skywalking.api.logging; -import com.a.eye.skywalking.conf.Config; -import com.a.eye.skywalking.util.LoggingUtil; +import com.a.eye.skywalking.api.conf.Config; +import com.a.eye.skywalking.api.util.LoggingUtil; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/WriterFactory.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/WriterFactory.java index 1e290aa26..3571aaa06 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/WriterFactory.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/logging/WriterFactory.java @@ -1,6 +1,6 @@ package com.a.eye.skywalking.api.logging; -import com.a.eye.skywalking.conf.Config; +import com.a.eye.skywalking.api.conf.Config; public class WriterFactory { private WriterFactory(){ diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/EnhanceException.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/EnhanceException.java index adf1fae5c..dec61a839 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/EnhanceException.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/EnhanceException.java @@ -1,6 +1,6 @@ package com.a.eye.skywalking.api.plugin.interceptor; -import com.a.eye.skywalking.plugin.PluginException; +import com.a.eye.skywalking.api.plugin.PluginException; public class EnhanceException extends PluginException { private static final long serialVersionUID = -2234782755784217255L; diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/assist/NoCocurrencyAceessObject.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/assist/NoCocurrencyAceessObject.java index eaa0ffadb..5229c1a49 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/assist/NoCocurrencyAceessObject.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/assist/NoCocurrencyAceessObject.java @@ -1,7 +1,7 @@ package com.a.eye.skywalking.api.plugin.interceptor.assist; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.InterceptorException; +import com.a.eye.skywalking.api.plugin.interceptor.InterceptorException; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; /** diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/enhance/ClassEnhancePluginDefine.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/enhance/ClassEnhancePluginDefine.java index dbb2d3c3e..cd0f92858 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/enhance/ClassEnhancePluginDefine.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/enhance/ClassEnhancePluginDefine.java @@ -3,7 +3,12 @@ package com.a.eye.skywalking.api.plugin.interceptor.enhance; import com.a.eye.skywalking.api.logging.api.ILog; import com.a.eye.skywalking.api.logging.api.LogManager; import com.a.eye.skywalking.api.plugin.AbstractClassEnhancePluginDefine; -import com.a.eye.skywalking.plugin.PluginException; +import com.a.eye.skywalking.api.plugin.PluginException; +import com.a.eye.skywalking.api.plugin.interceptor.ConstructorInterceptPoint; +import com.a.eye.skywalking.api.plugin.interceptor.EnhanceException; +import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; +import com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint; +import com.a.eye.skywalking.api.plugin.interceptor.StaticMethodsInterceptPoint; import com.a.eye.skywalking.api.util.StringUtil; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.dynamic.DynamicType; diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/enhance/ClassInstanceMethodsEnhancePluginDefine.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/enhance/ClassInstanceMethodsEnhancePluginDefine.java index 97571d4dc..9a3888775 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/enhance/ClassInstanceMethodsEnhancePluginDefine.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/enhance/ClassInstanceMethodsEnhancePluginDefine.java @@ -1,7 +1,6 @@ package com.a.eye.skywalking.api.plugin.interceptor.enhance; import com.a.eye.skywalking.api.plugin.interceptor.StaticMethodsInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.StaticMethodsInterceptPoint; /** * Plugins, which only need enhance class static methods. diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/enhance/ClassStaticMethodsEnhancePluginDefine.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/enhance/ClassStaticMethodsEnhancePluginDefine.java index 37b38c077..e97f1447b 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/enhance/ClassStaticMethodsEnhancePluginDefine.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/plugin/interceptor/enhance/ClassStaticMethodsEnhancePluginDefine.java @@ -1,7 +1,7 @@ package com.a.eye.skywalking.api.plugin.interceptor.enhance; +import com.a.eye.skywalking.api.plugin.interceptor.ConstructorInterceptPoint; import com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.ConstructorInterceptPoint; /** * Plugins, which only need enhance class static methods. diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/queue/TraceSegmentProcessQueue.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/queue/TraceSegmentProcessQueue.java index 10b2420f8..7a3ac2d1b 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/queue/TraceSegmentProcessQueue.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/queue/TraceSegmentProcessQueue.java @@ -1,8 +1,8 @@ package com.a.eye.skywalking.api.queue; -import com.a.eye.skywalking.conf.Config; +import com.a.eye.skywalking.api.conf.Config; import com.a.eye.skywalking.api.context.TracerContext; -import com.a.eye.skywalking.context.TracerContextListener; +import com.a.eye.skywalking.api.context.TracerContextListener; import com.a.eye.skywalking.health.report.HealthCollector; import com.a.eye.skywalking.health.report.HeathReading; import com.a.eye.skywalking.trace.TraceSegment; @@ -36,7 +36,7 @@ public enum TraceSegmentProcessQueue implements TracerContextListener { RingBuffer buffer; TraceSegmentProcessQueue() { - disruptor = new Disruptor<>(TraceSegmentHolder.Factory.INSTANCE, Config.Disruptor.BUFFER_SIZE, DaemonThreadFactory.INSTANCE); + disruptor = new Disruptor(TraceSegmentHolder.Factory.INSTANCE, Config.Disruptor.BUFFER_SIZE, DaemonThreadFactory.INSTANCE); buffer = disruptor.getRingBuffer(); } diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/TraceIdGenerator.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/TraceIdGenerator.java index e68fe7c64..fc3ac560c 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/TraceIdGenerator.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/util/TraceIdGenerator.java @@ -1,6 +1,6 @@ package com.a.eye.skywalking.api.util; -import com.a.eye.skywalking.conf.Constants; +import com.a.eye.skywalking.api.conf.Constants; import java.util.UUID; public final class TraceIdGenerator { diff --git a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/TestAroundInterceptor.java b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/TestAroundInterceptor.java index a640985e9..2c37506c2 100644 --- a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/TestAroundInterceptor.java +++ b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/TestAroundInterceptor.java @@ -1,8 +1,7 @@ package com.a.eye.skywalking.api.plugin; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; diff --git a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/TestInterceptorDefine.java b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/TestInterceptorDefine.java index 6e4c492a2..cc5024b6d 100644 --- a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/TestInterceptorDefine.java +++ b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/TestInterceptorDefine.java @@ -4,8 +4,6 @@ import com.a.eye.skywalking.api.plugin.interceptor.ConstructorInterceptPoint; import com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint; import com.a.eye.skywalking.api.plugin.interceptor.StaticMethodsInterceptPoint; import com.a.eye.skywalking.api.plugin.interceptor.enhance.ClassEnhancePluginDefine; -import com.a.eye.skywalking.plugin.interceptor.ConstructorInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.StaticMethodsInterceptPoint; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; diff --git a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/TestStaticAroundInterceptor.java b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/TestStaticAroundInterceptor.java index cfe346d88..07dd955ac 100644 --- a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/TestStaticAroundInterceptor.java +++ b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/plugin/TestStaticAroundInterceptor.java @@ -1,7 +1,7 @@ package com.a.eye.skywalking.api.plugin; import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; -import com.a.eye.skywalking.plugin.interceptor.enhance.MethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.StaticMethodInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.StaticMethodsAroundInterceptor; diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/test/java/com/a/eye/skywalking/sniffer/mock/MockTracerContextListenerTestCase.java b/skywalking-sniffer/skywalking-sniffer-mock/src/test/java/com/a/eye/skywalking/sniffer/mock/MockTracerContextListenerTestCase.java new file mode 100644 index 000000000..96e05bb42 --- /dev/null +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/test/java/com/a/eye/skywalking/sniffer/mock/MockTracerContextListenerTestCase.java @@ -0,0 +1,19 @@ +package com.a.eye.skywalking.sniffer.mock; + +import com.a.eye.skywalking.sniffer.mock.context.MockTracerContextListener; +import com.a.eye.skywalking.sniffer.mock.trace.TraceSegmentBuilderFactory; +import org.junit.Assert; +import org.junit.Test; + +/** + * Created by wusheng on 2017/2/21. + */ +public class MockTracerContextListenerTestCase { + @Test + public void testAfterFinished(){ + MockTracerContextListener listener = new MockTracerContextListener(); + listener.afterFinished(TraceSegmentBuilderFactory.INSTANCE.singleTomcat200Trace()); + + Assert.assertNotNull(listener.getFinished(0)); + } +} From 47871d1571542e55735ab08a7fa839ef085aa716 Mon Sep 17 00:00:00 2001 From: wusheng Date: Tue, 21 Feb 2017 09:47:29 +0800 Subject: [PATCH 5/7] Add mock-module test cases. --- .../context/MockTracerContextListener.java | 11 ++++++- .../MockTracerContextListenerTestCase.java | 33 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/MockTracerContextListener.java b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/MockTracerContextListener.java index a79f80e1a..d9de084d2 100644 --- a/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/MockTracerContextListener.java +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/main/java/com/a/eye/skywalking/sniffer/mock/context/MockTracerContextListener.java @@ -30,6 +30,15 @@ public class MockTracerContextListener implements TracerContextListener { Assert.assertEquals(size, finishedTraceSegments.size()); } + /** + * Assert the given index is a valid index of {@link #finishedTraceSegments} + * + * @param index the given index. + */ + public void assertValidIndex(int index){ + Assert.assertTrue(index < finishedTraceSegments.size()); + } + /** * Assert the {@link TraceSegment} at the given index of {@link #finishedTraceSegments}, * and run the given {@link SegmentAssert#call(TraceSegment)} to assert. @@ -38,7 +47,7 @@ public class MockTracerContextListener implements TracerContextListener { * @param segmentAssert the given assert. */ public void assertTraceSegment(int index, SegmentAssert segmentAssert){ - assertSize(index + 1); + assertValidIndex(index); segmentAssert.call(finishedTraceSegments.get(index)); } diff --git a/skywalking-sniffer/skywalking-sniffer-mock/src/test/java/com/a/eye/skywalking/sniffer/mock/MockTracerContextListenerTestCase.java b/skywalking-sniffer/skywalking-sniffer-mock/src/test/java/com/a/eye/skywalking/sniffer/mock/MockTracerContextListenerTestCase.java index 96e05bb42..ce43770c8 100644 --- a/skywalking-sniffer/skywalking-sniffer-mock/src/test/java/com/a/eye/skywalking/sniffer/mock/MockTracerContextListenerTestCase.java +++ b/skywalking-sniffer/skywalking-sniffer-mock/src/test/java/com/a/eye/skywalking/sniffer/mock/MockTracerContextListenerTestCase.java @@ -1,7 +1,9 @@ package com.a.eye.skywalking.sniffer.mock; import com.a.eye.skywalking.sniffer.mock.context.MockTracerContextListener; +import com.a.eye.skywalking.sniffer.mock.context.SegmentAssert; import com.a.eye.skywalking.sniffer.mock.trace.TraceSegmentBuilderFactory; +import com.a.eye.skywalking.trace.TraceSegment; import org.junit.Assert; import org.junit.Test; @@ -16,4 +18,35 @@ public class MockTracerContextListenerTestCase { Assert.assertNotNull(listener.getFinished(0)); } + + @Test(expected = AssertionError.class) + public void testAssertSize(){ + MockTracerContextListener listener = new MockTracerContextListener(); + listener.afterFinished(TraceSegmentBuilderFactory.INSTANCE.singleTomcat404Trace()); + + listener.assertSize(0); + } + + @Test + public void testAssertTraceSegment(){ + MockTracerContextListener listener = new MockTracerContextListener(); + listener.afterFinished(TraceSegmentBuilderFactory.INSTANCE.singleTomcat404Trace()); + listener.afterFinished(TraceSegmentBuilderFactory.INSTANCE.singleTomcat500Trace()); + + listener.assertTraceSegment(0, new SegmentAssert() { + @Override public void call(TraceSegment finishedSegment) { + Assert.assertNotNull(finishedSegment); + } + }); + } + + @Test(expected = AssertionError.class) + public void testClear(){ + MockTracerContextListener listener = new MockTracerContextListener(); + listener.afterFinished(TraceSegmentBuilderFactory.INSTANCE.singleTomcat404Trace()); + listener.afterFinished(TraceSegmentBuilderFactory.INSTANCE.singleTomcat500Trace()); + + listener.clear(); + listener.assertValidIndex(0); + } } From bfa53127fbed56a2add7cc22218c66885e136a97 Mon Sep 17 00:00:00 2001 From: wusheng Date: Tue, 21 Feb 2017 16:00:28 +0800 Subject: [PATCH 6/7] Fix opentracing-toolkit activation compile issue, and useless codes. --- .../java/com/a/eye/skywalking/trace/Span.java | 70 ++++++++++++++++- skywalking-sniffer/skywalking-api/pom.xml | 5 -- .../api/context/ContextManager.java | 8 ++ .../skywalking/api/context/TracerContext.java | 21 ++++- .../span/SkyWalkingSpanActivation.java | 7 +- .../interceptor/SpanFinishInterceptor.java | 26 +++---- .../SpanNewInstanceInterceptor.java | 20 +++-- .../SpanSetOperationNameInterceptor.java | 11 +-- .../interceptor/SpanSetTagInterceptor.java | 39 +++++----- .../OpenTracingLocalBuriedPointType.java | 20 ----- .../SkyWalkingSpanBuilderActivation.java | 77 ------------------- .../SpanBuilderNewInstanceInterceptor.java | 15 ---- .../SpanBuilderStartInterceptor.java | 62 --------------- .../SpanBuilderWithStartTimeInterceptor.java | 29 ------- .../SpanBuilderWithTagInterceptor.java | 39 ---------- .../tracer/SkyWalkingTracerActivation.java | 6 +- ...ssProcessByteBufferContextInterceptor.java | 17 ++-- ...CrossProcessTextMapContextInterceptor.java | 39 +++++----- ...rFormatCrossProcessContextInterceptor.java | 26 ++----- .../src/main/resources/skywalking-plugin.def | 3 +- 20 files changed, 189 insertions(+), 351 deletions(-) delete mode 100644 skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/OpenTracingLocalBuriedPointType.java delete mode 100644 skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/SkyWalkingSpanBuilderActivation.java delete mode 100644 skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderNewInstanceInterceptor.java delete mode 100644 skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderStartInterceptor.java delete mode 100644 skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderWithStartTimeInterceptor.java delete mode 100644 skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderWithTagInterceptor.java diff --git a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/Span.java b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/Span.java index 75e7f3ab6..764fe05d6 100644 --- a/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/Span.java +++ b/skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/Span.java @@ -54,7 +54,7 @@ public class Span { private final List logs; /** - * Create a new span, by given span id and parent span id. + * Create a new span, by given span id, parent span id and operationName. * This span must belong a {@link TraceSegment}, also is a part of Distributed Trace. * * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} @@ -63,9 +63,23 @@ public class Span { * @param operationName {@link #operationName} */ private Span(int spanId, int parentSpanId, String operationName) { + this(spanId, parentSpanId, operationName, System.currentTimeMillis()); + } + + /** + *Create a new span, by given span id, parent span id, operationName and startTime. + * This span must belong a {@link TraceSegment}, also is a part of Distributed Trace. + * + * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} + * @param parentSpanId given by the creator, and must be an existed span id in the {@link TraceSegment}. Value -1 + * means no parent span if this {@link TraceSegment}. + * @param operationName {@link #operationName} + * @param startTime given start timestamp. + */ + private Span(int spanId, int parentSpanId, String operationName, long startTime){ this.spanId = spanId; this.parentSpanId = parentSpanId; - this.startTime = System.currentTimeMillis(); + this.startTime = startTime; this.operationName = operationName; this.tags = new HashMap(); this.logs = new ArrayList(); @@ -82,6 +96,19 @@ public class Span { this(spanId, -1, operationName); } + /** + * + * Create a new span, by given span id and give startTime but no parent span id, + * No parent span id means that, this Span is the first span of the {@link TraceSegment} + * + * @param spanId given by the creator, and must be unique id in the {@link TraceSegment} + * @param operationName {@link #operationName} + * @param startTime given start time of span + */ + public Span(int spanId, String operationName, long startTime) { + this(spanId, -1, operationName, startTime); + } + /** * Create a new span, by given span id and given parent {@link Span}. * @@ -90,7 +117,18 @@ public class Span { * @param operationName {@link #operationName} */ public Span(int spanId, Span parentSpan, String operationName) { - this(spanId, parentSpan.spanId, operationName); + this(spanId, parentSpan.spanId, operationName, System.currentTimeMillis()); + } + + /** + * + * @param spanId + * @param parentSpan + * @param operationName + * @param startTime + */ + public Span(int spanId, Span parentSpan, String operationName, long startTime) { + this(spanId, parentSpan.spanId, operationName, startTime); } /** @@ -100,12 +138,36 @@ public class Span { * @param owner of the Span. */ public void finish(TraceSegment owner) { - this.endTime = System.currentTimeMillis(); + this.finish(owner, System.currentTimeMillis()); + } + + /** + * Finish the active Span. + * When it is finished, it will be archived by the given {@link TraceSegment}, which owners it. + * At the same out, set the {@link #endTime} as the given endTime + * + * @param owner of the Span. + * @param endTime of the Span. + */ + public void finish(TraceSegment owner, long endTime){ + this.endTime = endTime; owner.archive(this); } + /** + * Sets the string name for the logical operation this span represents. + * + * @return this Span instance, for chaining + */ + public Span setOperationName(String operationName){ + this.operationName = operationName; + return this; + } + /** * Set a key:value tag on the Span. + * + * @return this Span instance, for chaining */ public final Span setTag(String key, String value) { tags.put(key, value); diff --git a/skywalking-sniffer/skywalking-api/pom.xml b/skywalking-sniffer/skywalking-api/pom.xml index 5774f18bd..5d898ca26 100644 --- a/skywalking-sniffer/skywalking-api/pom.xml +++ b/skywalking-sniffer/skywalking-api/pom.xml @@ -46,11 +46,6 @@ skywalking-logging-api ${project.version} - - com.lmax - disruptor - 3.3.6 - diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/ContextManager.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/ContextManager.java index 2ff330509..9fe501c2a 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/ContextManager.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/ContextManager.java @@ -57,6 +57,10 @@ public enum ContextManager implements TracerContextListener { return get().createSpan(operationName); } + public Span createSpan(String operationName, long startTime) { + return get().createSpan(operationName); + } + public Span activeSpan() { return get().activeSpan(); } @@ -65,6 +69,10 @@ public enum ContextManager implements TracerContextListener { get().stopSpan(span); } + public void stopSpan(Long endTime) { + get().stopSpan(activeSpan(), endTime); + } + public void stopSpan() { stopSpan(activeSpan()); } diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java index 4ef3d3188..c7d77eedb 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/context/TracerContext.java @@ -40,12 +40,23 @@ public final class TracerContext { * @return the new active span. */ public Span createSpan(String operationName) { + 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} + * @param startTime {@link Span#startTime} + * @return + */ + public Span createSpan(String operationName, long startTime){ Span parentSpan = peek(); Span span; if (parentSpan == null) { - span = new Span(spanIdGenerator++, operationName); + span = new Span(spanIdGenerator++, operationName, startTime); } else { - span = new Span(spanIdGenerator++, parentSpan, operationName); + span = new Span(spanIdGenerator++, parentSpan, operationName, startTime); } push(span); return span; @@ -68,9 +79,13 @@ public final class TracerContext { * @param span to finish. It must the the top element of {@link #activeSpanStack}. */ public void stopSpan(Span span) { + stopSpan(span, System.currentTimeMillis()); + } + + public void stopSpan(Span span, Long endTime){ Span lastSpan = peek(); if (lastSpan == span) { - segment.archive(pop()); + pop().finish(segment, endTime); } else { throw new IllegalStateException("Stopping the unexpected span = " + span); } diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/SkyWalkingSpanActivation.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/SkyWalkingSpanActivation.java index 8fd58f621..9f9884b2c 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/SkyWalkingSpanActivation.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/SkyWalkingSpanActivation.java @@ -1,16 +1,13 @@ package com.a.eye.skywalking.toolkit.activation.opentracing.span; -import com.a.eye.skywalking.plugin.interceptor.ConstructorInterceptPoint; +import com.a.eye.skywalking.api.plugin.interceptor.ConstructorInterceptPoint; import com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; - import java.util.Map; - import static net.bytebuddy.matcher.ElementMatchers.named; -import static net.bytebuddy.matcher.ElementMatchers.noneOf; import static net.bytebuddy.matcher.ElementMatchers.takesArguments; /** diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanFinishInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanFinishInterceptor.java index 3af85ee90..e008ffcf7 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanFinishInterceptor.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanFinishInterceptor.java @@ -1,36 +1,32 @@ package com.a.eye.skywalking.toolkit.activation.opentracing.span.interceptor; -import com.a.eye.skywalking.api.Tracing; -import com.a.eye.skywalking.invoke.monitor.LocalMethodInvokeMonitor; -import com.a.eye.skywalking.model.Span; +import com.a.eye.skywalking.api.context.ContextManager; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; - -import java.util.Map; +import com.a.eye.skywalking.toolkit.opentracing.SkyWalkingSpan; /** - * Created by xin on 2017/1/16. + * Intercept these following methods: + * {@link SkyWalkingSpan#finish()} + * {@link SkyWalkingSpan#finish(long)} */ public class SpanFinishInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) { - // do nothing } @Override public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { - Span currentSpan = Tracing.getCurrentSpan(); + Object[] allArguments = interceptorContext.allArguments(); - Map tags = (Map) context.get("tags"); - if (tags != null) { - for (Map.Entry entry : tags.entrySet()) { - Tracing.tag(currentSpan, entry.getKey(), entry.getValue()); - } + if(allArguments.length == 1) { + ContextManager.INSTANCE.stopSpan(((Long)allArguments[0])); + }else{ + ContextManager.INSTANCE.stopSpan(); } - new LocalMethodInvokeMonitor().afterInvoke(); return ret; } diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanNewInstanceInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanNewInstanceInterceptor.java index d0821a41e..73967ca7a 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanNewInstanceInterceptor.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanNewInstanceInterceptor.java @@ -1,18 +1,28 @@ package com.a.eye.skywalking.toolkit.activation.opentracing.span.interceptor; +import com.a.eye.skywalking.api.context.ContextManager; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ConstructorInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceConstructorInterceptor; +import com.a.eye.skywalking.toolkit.opentracing.SkyWalkingSpan; +import com.a.eye.skywalking.trace.Span; +import java.util.Map; /** - * @author zhangxin + * Intercept {@link SkyWalkingSpan} constructor. */ public class SpanNewInstanceInterceptor implements InstanceConstructorInterceptor { - private static final String OPERATION_NAME = "operationName"; - @Override public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext) { - context.set(OPERATION_NAME, interceptorContext.allArguments()[0]); + Object[] allArguments = interceptorContext.allArguments(); + String operationName = ((String)allArguments[0]); + long startTime = ((Long)allArguments[1]); + Map tags = ((Map)allArguments[2]); + Span span = ContextManager.INSTANCE.createSpan(operationName, startTime); + + for (Map.Entry entry : tags.entrySet()) { + span.setTag(entry.getKey(), entry.getValue()); + } } } diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanSetOperationNameInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanSetOperationNameInterceptor.java index 5eb368ff0..7f46ac393 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanSetOperationNameInterceptor.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanSetOperationNameInterceptor.java @@ -1,20 +1,21 @@ package com.a.eye.skywalking.toolkit.activation.opentracing.span.interceptor; +import com.a.eye.skywalking.api.context.ContextManager; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; +import com.a.eye.skywalking.toolkit.opentracing.SkyWalkingSpan; /** - * Created by xin on 2017/1/16. + * Intercept {@link SkyWalkingSpan#setOperationName(String)} */ public class SpanSetOperationNameInterceptor implements InstanceMethodsAroundInterceptor { - private static final String OPERATION_NAME = "operationName"; - @Override public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) { - context.set(OPERATION_NAME, interceptorContext.allArguments()[0]); + String operationName = (String)interceptorContext.allArguments()[0]; + ContextManager.INSTANCE.activeSpan().setOperationName(operationName); } @Override diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanSetTagInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanSetTagInterceptor.java index f8ee21cc7..35d5ae3ec 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanSetTagInterceptor.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/span/interceptor/SpanSetTagInterceptor.java @@ -1,39 +1,42 @@ package com.a.eye.skywalking.toolkit.activation.opentracing.span.interceptor; +import com.a.eye.skywalking.api.context.ContextManager; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; - -import java.util.HashMap; -import java.util.Map; +import com.a.eye.skywalking.toolkit.opentracing.SkyWalkingSpan; /** - * Created by xin on 2017/1/16. + * Intercept these following methods: + * {@link SkyWalkingSpan#setTag(String, boolean)} + * {@link SkyWalkingSpan#setTag(String, Number)} + * {@link SkyWalkingSpan#setTag(String, String)} */ public class SpanSetTagInterceptor implements InstanceMethodsAroundInterceptor { - private static final String TAGS = "tags"; - @Override - public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) { - Map contextTags = (Map) context.get(TAGS); - if (!context.isContain(TAGS)){ - contextTags = new HashMap(); - context.set(TAGS, contextTags); - } - - contextTags.put((String) interceptorContext.allArguments()[0], String.valueOf(interceptorContext - .allArguments()[1])); + public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, + MethodInterceptResult result) { + String key = (String)interceptorContext.allArguments()[0]; + Object value = interceptorContext.allArguments()[1]; + if (value instanceof String) + ContextManager.INSTANCE.activeSpan().setTag(key, (String)value); + else if (value instanceof Boolean) + ContextManager.INSTANCE.activeSpan().setTag(key, (Boolean)value); + else if (value instanceof Number) + ContextManager.INSTANCE.activeSpan().setTag(key, (Number)value); } @Override - public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { + public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, + Object ret) { return ret; } @Override - public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext) { + public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, + InstanceMethodInvokeContext interceptorContext) { } } diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/OpenTracingLocalBuriedPointType.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/OpenTracingLocalBuriedPointType.java deleted file mode 100644 index 340a46865..000000000 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/OpenTracingLocalBuriedPointType.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.a.eye.skywalking.toolkit.activation.opentracing.spanbuilder; - -import com.a.eye.skywalking.api.IBuriedPointType; - -/** - * @author zhangxin - */ -public enum OpenTracingLocalBuriedPointType implements IBuriedPointType { - INSTANCE; - - @Override - public String getTypeName() { - return "OT"; - } - - @Override - public CallType getCallType() { - return CallType.LOCAL; - } -} diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/SkyWalkingSpanBuilderActivation.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/SkyWalkingSpanBuilderActivation.java deleted file mode 100644 index 7d79c3d1c..000000000 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/SkyWalkingSpanBuilderActivation.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.a.eye.skywalking.toolkit.activation.opentracing.spanbuilder; - -import com.a.eye.skywalking.plugin.interceptor.ConstructorInterceptPoint; -import com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; - -import net.bytebuddy.description.method.MethodDescription; -import net.bytebuddy.matcher.ElementMatcher; - -import static net.bytebuddy.matcher.ElementMatchers.named; -import static net.bytebuddy.matcher.ElementMatchers.takesArguments; - -/** - * Created by xin on 2017/1/16. - */ -public class SkyWalkingSpanBuilderActivation extends ClassInstanceMethodsEnhancePluginDefine { - @Override - protected String enhanceClassName() { - return "com.a.eye.skywalking.toolkit.opentracing.SkyWalkingSpanBuilder"; - } - - @Override - protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { - return new ConstructorInterceptPoint[]{ - new ConstructorInterceptPoint() { - @Override - public ElementMatcher getConstructorMatcher() { - return takesArguments(String.class); - } - - @Override - public String getConstructorInterceptor() { - return "com.a.eye.skywalking.toolkit.activation.opentracing.spanbuilder.interceptor.SpanBuilderNewInstanceInterceptor"; - } - } - }; - } - - @Override - protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { - return new InstanceMethodsInterceptPoint[]{ - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("withTag"); - } - - @Override - public String getMethodsInterceptor() { - return "com.a.eye.skywalking.toolkit.activation.opentracing.spanbuilder.interceptor.SpanBuilderWithTagInterceptor"; - } - }, - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("withStartTimestamp"); - } - - @Override - public String getMethodsInterceptor() { - return "com.a.eye.skywalking.toolkit.activation.opentracing.spanbuilder.interceptor.SpanBuilderWithStartTimeInterceptor"; - } - }, - new InstanceMethodsInterceptPoint() { - @Override - public ElementMatcher getMethodsMatcher() { - return named("start"); - } - - @Override - public String getMethodsInterceptor() { - return "com.a.eye.skywalking.toolkit.activation.opentracing.spanbuilder.interceptor.SpanBuilderStartInterceptor"; - } - } - }; - } -} diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderNewInstanceInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderNewInstanceInterceptor.java deleted file mode 100644 index 6b0b56dd5..000000000 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderNewInstanceInterceptor.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.a.eye.skywalking.toolkit.activation.opentracing.spanbuilder.interceptor; - -import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.ConstructorInvokeContext; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceConstructorInterceptor; - -public class SpanBuilderNewInstanceInterceptor implements InstanceConstructorInterceptor { - - public static final String OPERATION_NAME = "operationName"; - - @Override - public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext) { - context.set(OPERATION_NAME, interceptorContext.allArguments()[0]); - } -} diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderStartInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderStartInterceptor.java deleted file mode 100644 index 70846c7e0..000000000 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderStartInterceptor.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.a.eye.skywalking.toolkit.activation.opentracing.spanbuilder.interceptor; - -import com.a.eye.skywalking.invoke.monitor.LocalMethodInvokeMonitor; -import com.a.eye.skywalking.model.Identification; -import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; -import com.a.eye.skywalking.toolkit.activation.opentracing.spanbuilder.OpenTracingLocalBuriedPointType; - -import java.util.Map; - -/** - * @author zhangxin - */ -public class SpanBuilderStartInterceptor implements InstanceMethodsAroundInterceptor { - - public static final String START_TIME = "startTimestamp"; - public static final String OPERATION_NAME = "operationName"; - public static final String TAGS = "tags"; - - @Override - public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) { - - Long startTime = fetchStartTime(context); - String operationName = (String) context.get(OPERATION_NAME); - Identification.IdentificationBuilder builder = Identification.newBuilder().viewPoint(operationName) - .spanType(OpenTracingLocalBuriedPointType.INSTANCE); - - if (startTime != null){ - builder.startTime(startTime); - } - - Map tags = (Map) context.get(TAGS); - if (tags != null) { - for (Map.Entry tag : tags.entrySet()) { - builder.tag(tag.getKey(), tag.getValue()); - } - } - - new LocalMethodInvokeMonitor().beforeInvoke(builder.build()); - } - - private Long fetchStartTime(EnhancedClassInstanceContext context) { - Object startTime = context.get(START_TIME); - if (startTime != null){ - return (Long) startTime; - } - - return null; - } - - @Override - public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { - return ret; - } - - @Override - public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext) { - - } -} diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderWithStartTimeInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderWithStartTimeInterceptor.java deleted file mode 100644 index 1071743bc..000000000 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderWithStartTimeInterceptor.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.a.eye.skywalking.toolkit.activation.opentracing.spanbuilder.interceptor; - -import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; - -/** - * Created by xin on 2017/1/16. - */ -public class SpanBuilderWithStartTimeInterceptor implements InstanceMethodsAroundInterceptor { - - public static final String START_TIME = "startTimestamp"; - - @Override - public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) { - context.set(START_TIME, interceptorContext.allArguments()[0]); - } - - @Override - public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { - return ret; - } - - @Override - public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext) { - - } -} diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderWithTagInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderWithTagInterceptor.java deleted file mode 100644 index 6fddea0c7..000000000 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/spanbuilder/interceptor/SpanBuilderWithTagInterceptor.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.a.eye.skywalking.toolkit.activation.opentracing.spanbuilder.interceptor; - -import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; -import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; - -import java.util.HashMap; -import java.util.Map; - -/** - * Created by xin on 2017/1/16. - */ -public class SpanBuilderWithTagInterceptor implements InstanceMethodsAroundInterceptor { - - public static final String TAGS = "tags"; - - @Override - public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) { - Map contextTags = (Map) context.get(TAGS); - if (!context.isContain(TAGS)){ - contextTags = new HashMap(); - context.set(TAGS, contextTags); - } - - contextTags.put((String) interceptorContext.allArguments()[0], String.valueOf(interceptorContext - .allArguments()[1])); - } - - @Override - public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { - return ret; - } - - @Override - public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext) { - // Do nothing - } -} diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/SkyWalkingTracerActivation.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/SkyWalkingTracerActivation.java index 767e1d9da..ed496a039 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/SkyWalkingTracerActivation.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/SkyWalkingTracerActivation.java @@ -1,14 +1,14 @@ package com.a.eye.skywalking.toolkit.activation.opentracing.tracer; -import com.a.eye.skywalking.plugin.interceptor.ConstructorInterceptPoint; +import com.a.eye.skywalking.api.plugin.interceptor.ConstructorInterceptPoint; import com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; import java.nio.ByteBuffer; -import static com.a.eye.skywalking.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; +import static com.a.eye.skywalking.api.plugin.bytebuddy.ArgumentTypeNameMatch.takesArgumentWithType; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArgument; diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/interceptor/TracerExtractCrossProcessByteBufferContextInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/interceptor/TracerExtractCrossProcessByteBufferContextInterceptor.java index 8650449e2..f8c978c38 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/interceptor/TracerExtractCrossProcessByteBufferContextInterceptor.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/interceptor/TracerExtractCrossProcessByteBufferContextInterceptor.java @@ -1,18 +1,19 @@ package com.a.eye.skywalking.toolkit.activation.opentracing.tracer.interceptor; -import com.a.eye.skywalking.api.Tracing; -import com.a.eye.skywalking.model.RefContext; +import com.a.eye.skywalking.api.context.ContextCarrier; +import com.a.eye.skywalking.api.context.ContextManager; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; +import com.a.eye.skywalking.toolkit.opentracing.SkyWalkingTracer; +import io.opentracing.propagation.TextMap; import java.nio.ByteBuffer; import java.nio.charset.Charset; /** - * - * @author zhangxin + * Intercept {@link SkyWalkingTracer#extractCrossProcessPropagationContextData(TextMap)} */ public class TracerExtractCrossProcessByteBufferContextInterceptor implements InstanceMethodsAroundInterceptor { @Override @@ -24,7 +25,11 @@ public class TracerExtractCrossProcessByteBufferContextInterceptor implements In public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { ByteBuffer byteBuffer = (ByteBuffer) interceptorContext.allArguments()[0]; String contextDataStr = new String(byteBuffer.array(), Charset.forName("UTF-8")); - Tracing.initRefContext(new RefContext(contextDataStr)); + + ContextCarrier carrier = new ContextCarrier(); + carrier.deserialize(contextDataStr); + + ContextManager.INSTANCE.extract(carrier); return ret; } diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/interceptor/TracerExtractCrossProcessTextMapContextInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/interceptor/TracerExtractCrossProcessTextMapContextInterceptor.java index 729c555f5..ca1e71c98 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/interceptor/TracerExtractCrossProcessTextMapContextInterceptor.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/interceptor/TracerExtractCrossProcessTextMapContextInterceptor.java @@ -1,50 +1,49 @@ package com.a.eye.skywalking.toolkit.activation.opentracing.tracer.interceptor; -import com.a.eye.skywalking.api.Tracing; -import com.a.eye.skywalking.model.RefContext; +import com.a.eye.skywalking.api.context.ContextCarrier; +import com.a.eye.skywalking.api.context.ContextManager; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; - +import com.a.eye.skywalking.toolkit.opentracing.SkyWalkingTracer; +import io.opentracing.propagation.TextMap; import java.util.Iterator; import java.util.Map; -import io.opentracing.propagation.TextMap; - /** - * - * @author zhangxin + * Intercept {@link SkyWalkingTracer#extractCrossProcessPropagationContextData(TextMap)} */ public class TracerExtractCrossProcessTextMapContextInterceptor implements InstanceMethodsAroundInterceptor { - public static final String SKY_WALKING_TRACING_NAME = "SkyWalking-TRACING-NAME"; @Override - public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) { + public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, + MethodInterceptResult result) { // Do nothing } @Override - public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { - TextMap textMap = (TextMap) interceptorContext.allArguments()[0]; + public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, + Object ret) { + TextMap textMap = (TextMap)interceptorContext.allArguments()[0]; Iterator> iterator = textMap.iterator(); - while (iterator.hasNext()){ + while (iterator.hasNext()) { Map.Entry entry = iterator.next(); - if (SKY_WALKING_TRACING_NAME.equals(entry.getKey())){ - try { - Tracing.initRefContext(new RefContext(entry.getValue())); - }catch (Throwable e){ - // do something - } + if (SKY_WALKING_TRACING_NAME.equals(entry.getKey())) { + ContextCarrier carrier = new ContextCarrier(); + carrier.deserialize(entry.getValue()); + + ContextManager.INSTANCE.extract(carrier); } } return ret; } @Override - public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext) { + public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, + InstanceMethodInvokeContext interceptorContext) { } } diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/interceptor/TracerFormatCrossProcessContextInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/interceptor/TracerFormatCrossProcessContextInterceptor.java index bf6a4880f..ed87a94d0 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/interceptor/TracerFormatCrossProcessContextInterceptor.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/opentracing/tracer/interceptor/TracerFormatCrossProcessContextInterceptor.java @@ -1,15 +1,15 @@ package com.a.eye.skywalking.toolkit.activation.opentracing.tracer.interceptor; -import com.a.eye.skywalking.api.Tracing; -import com.a.eye.skywalking.model.ContextData; -import com.a.eye.skywalking.model.Span; +import com.a.eye.skywalking.api.context.ContextCarrier; +import com.a.eye.skywalking.api.context.ContextManager; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; +import com.a.eye.skywalking.toolkit.opentracing.SkyWalkingTracer; /** - * @author zhangxin + * Intercept {@link SkyWalkingTracer#formatCrossProcessPropagationContextData()} */ public class TracerFormatCrossProcessContextInterceptor implements InstanceMethodsAroundInterceptor { @Override @@ -19,23 +19,13 @@ public class TracerFormatCrossProcessContextInterceptor implements InstanceMetho @Override public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { - Span span = Tracing.getCurrentSpan(); - if (span != null) { - return new ContextData(span.getTraceId(), generateSubParentLevelId(span), span.getRouteKey()).toString(); - } - return ret; + ContextCarrier carrier = new ContextCarrier(); + ContextManager.INSTANCE.inject(carrier); + return carrier.serialize(); } @Override public void handleMethodException(Throwable t, EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext) { } - - private String generateSubParentLevelId(Span spanData) { - if (spanData.getParentLevel() == null || spanData.getParentLevel().length() == 0) { - return spanData.getLevelId() + ""; - } - - return spanData.getParentLevel() + "." + spanData.getLevelId(); - } } diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/resources/skywalking-plugin.def b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/resources/skywalking-plugin.def index bad4288c7..a528db98f 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/resources/skywalking-plugin.def +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-opentracing-activation/src/main/resources/skywalking-plugin.def @@ -1,3 +1,2 @@ com.a.eye.skywalking.toolkit.activation.opentracing.span.SkyWalkingSpanActivation -com.a.eye.skywalking.toolkit.activation.opentracing.spanbuilder.SkyWalkingSpanBuilderActivation -com.a.eye.skywalking.toolkit.activation.opentracing.tracer.SkyWalkingTracerActivation \ No newline at end of file +com.a.eye.skywalking.toolkit.activation.opentracing.tracer.SkyWalkingTracerActivation From f61008badc93e99447951c8b854104585a4f0130 Mon Sep 17 00:00:00 2001 From: wusheng Date: Tue, 21 Feb 2017 16:16:34 +0800 Subject: [PATCH 7/7] Fix log-toolkits(log4j, log4j2, logback) compile issue. --- .../log/log4j/v1/x/PrintTraceIdInterceptor.java | 9 ++++++--- .../log4j/v2/x/Log4j2OutputAppenderActivation.java | 4 ++-- .../log/log4j/v2/x/PrintTraceIdInterceptor.java | 11 +++++++++-- .../v1/x/LogbackPatternConverterActivation.java | 6 +++--- .../log/logback/v1/x/PrintTraceIdInterceptor.java | 9 ++++++--- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-1.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/log4j/v1/x/PrintTraceIdInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-1.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/log4j/v1/x/PrintTraceIdInterceptor.java index b70b9173d..9c9091445 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-1.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/log4j/v1/x/PrintTraceIdInterceptor.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-1.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/log4j/v1/x/PrintTraceIdInterceptor.java @@ -1,8 +1,9 @@ package com.a.eye.skywalking.toolkit.activation.log.log4j.v1.x; -import com.a.eye.skywalking.api.Tracing; +import com.a.eye.skywalking.api.context.ContextCarrier; +import com.a.eye.skywalking.api.context.ContextManager; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; @@ -25,7 +26,9 @@ public class PrintTraceIdInterceptor implements InstanceMethodsAroundInterceptor */ @Override public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { - return "TID:" + Tracing.getTraceId(); + ContextCarrier carrier = new ContextCarrier(); + ContextManager.INSTANCE.inject(carrier); + return "TID:" + carrier.getTraceSegmentId(); } @Override diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/log4j/v2/x/Log4j2OutputAppenderActivation.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/log4j/v2/x/Log4j2OutputAppenderActivation.java index fd6f4ee7b..be70f30ff 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/log4j/v2/x/Log4j2OutputAppenderActivation.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/log4j/v2/x/Log4j2OutputAppenderActivation.java @@ -1,7 +1,7 @@ package com.a.eye.skywalking.toolkit.activation.log.log4j.v2.x; -import com.a.eye.skywalking.plugin.interceptor.StaticMethodsInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.enhance.ClassStaticMethodsEnhancePluginDefine; +import com.a.eye.skywalking.api.plugin.interceptor.StaticMethodsInterceptPoint; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ClassStaticMethodsEnhancePluginDefine; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/log4j/v2/x/PrintTraceIdInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/log4j/v2/x/PrintTraceIdInterceptor.java index 46840d3bf..d8faef9e7 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/log4j/v2/x/PrintTraceIdInterceptor.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-log4j-2.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/log4j/v2/x/PrintTraceIdInterceptor.java @@ -1,6 +1,11 @@ package com.a.eye.skywalking.toolkit.activation.log.log4j.v2.x; -import com.a.eye.skywalking.api.Tracing; +import com.a.eye.skywalking.api.context.ContextCarrier; +import com.a.eye.skywalking.api.context.ContextManager; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.StaticMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.StaticMethodsAroundInterceptor; /** * Created by wusheng on 2016/12/7. @@ -14,7 +19,9 @@ public class PrintTraceIdInterceptor implements StaticMethodsAroundInterceptor { */ @Override public void beforeMethod(StaticMethodInvokeContext interceptorContext, MethodInterceptResult result) { - ((StringBuilder) interceptorContext.allArguments()[0]).append("TID:" + Tracing.getTraceId()); + ContextCarrier carrier = new ContextCarrier(); + ContextManager.INSTANCE.inject(carrier); + ((StringBuilder) interceptorContext.allArguments()[0]).append("TID:" + carrier.getTraceSegmentId()); //make sure origin method do not invoke. result.defineReturnValue(null); diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-logback-1.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/logback/v1/x/LogbackPatternConverterActivation.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-logback-1.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/logback/v1/x/LogbackPatternConverterActivation.java index 6379d9685..02ff96d6d 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-logback-1.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/logback/v1/x/LogbackPatternConverterActivation.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-logback-1.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/logback/v1/x/LogbackPatternConverterActivation.java @@ -1,9 +1,9 @@ package com.a.eye.skywalking.toolkit.activation.log.logback.v1.x; -import com.a.eye.skywalking.plugin.interceptor.ConstructorInterceptPoint; +import com.a.eye.skywalking.api.plugin.interceptor.ConstructorInterceptPoint; import com.a.eye.skywalking.api.plugin.interceptor.InstanceMethodsInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.StaticMethodsInterceptPoint; -import com.a.eye.skywalking.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; +import com.a.eye.skywalking.api.plugin.interceptor.StaticMethodsInterceptPoint; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; diff --git a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-logback-1.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/logback/v1/x/PrintTraceIdInterceptor.java b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-logback-1.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/logback/v1/x/PrintTraceIdInterceptor.java index 0677d4efa..acc17953c 100644 --- a/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-logback-1.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/logback/v1/x/PrintTraceIdInterceptor.java +++ b/skywalking-sniffer/skywalking-toolkit-activation/skywalking-toolkit-logback-1.x-activation/src/main/java/com/a/eye/skywalking/toolkit/activation/log/logback/v1/x/PrintTraceIdInterceptor.java @@ -1,8 +1,9 @@ package com.a.eye.skywalking.toolkit.activation.log.logback.v1.x; -import com.a.eye.skywalking.api.Tracing; +import com.a.eye.skywalking.api.context.ContextCarrier; +import com.a.eye.skywalking.api.context.ContextManager; import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; +import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodInvokeContext; import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult; @@ -25,7 +26,9 @@ public class PrintTraceIdInterceptor implements InstanceMethodsAroundInterceptor */ @Override public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { - return "TID:" + Tracing.getTraceId(); + ContextCarrier carrier = new ContextCarrier(); + ContextManager.INSTANCE.inject(carrier); + return "TID:" + carrier.getTraceSegmentId(); } @Override