diff --git a/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/main/java/com/a/eye/skywalking/toolkit/opentracing/ByteBufferContext.java b/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/main/java/com/a/eye/skywalking/toolkit/opentracing/ByteBufferContext.java new file mode 100644 index 000000000..f919e3fd1 --- /dev/null +++ b/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/main/java/com/a/eye/skywalking/toolkit/opentracing/ByteBufferContext.java @@ -0,0 +1,29 @@ +package com.a.eye.skywalking.toolkit.opentracing; + +import io.opentracing.SpanContext; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; + +/** + * Created by wusheng on 2016/12/21. + */ +public class ByteBufferContext implements SpanContext { + static final Charset CHARSET = Charset.forName("UTF-8"); + + static final byte NO_ENTRY = 0; + static final byte ENTRY = 1; + + private final ByteBuffer byteBuffer; + + ByteBufferContext(ByteBuffer byteBuffer) { + this.byteBuffer = byteBuffer; + } + + @Override + public Iterable> baggageItems() { + return new HashMap().entrySet(); + } +} diff --git a/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/main/java/com/a/eye/skywalking/toolkit/opentracing/SkyWalkingTracer.java b/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/main/java/com/a/eye/skywalking/toolkit/opentracing/SkyWalkingTracer.java index 4f91b3a1d..eb3f851be 100644 --- a/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/main/java/com/a/eye/skywalking/toolkit/opentracing/SkyWalkingTracer.java +++ b/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/main/java/com/a/eye/skywalking/toolkit/opentracing/SkyWalkingTracer.java @@ -3,15 +3,20 @@ package com.a.eye.skywalking.toolkit.opentracing; import io.opentracing.SpanContext; import io.opentracing.Tracer; import io.opentracing.propagation.Format; +import io.opentracing.propagation.TextMap; + +import java.nio.ByteBuffer; /** * All source code in SkyWalkingTracer acts like an NoopTracer. * Actually, it is NOT. * The whole logic will be added after toolkit-activation. - * + *

* Created by wusheng on 2016/12/20. */ -public class SkyWalkingTracer implements Tracer{ +public class SkyWalkingTracer implements Tracer { + private static String TRACE_HEAD_NAME = "SkyWalking-TRACING-NAME"; + public static Tracer INSTANCE = new SkyWalkingTracer(); @Override @@ -20,12 +25,62 @@ public class SkyWalkingTracer implements Tracer{ } @Override - public void inject(SpanContext spanContext, Format format, C c) { - + public void inject(SpanContext spanContext, Format format, C carrier) { + if (Format.Builtin.TEXT_MAP.equals(format) || Format.Builtin.HTTP_HEADERS.equals(format)) { + ((TextMap) carrier).put(TRACE_HEAD_NAME, formatCrossProcessPropagationContextData()); + } else if (Format.Builtin.BINARY.equals(format)) { + byte[] key = TRACE_HEAD_NAME.getBytes(ByteBufferContext.CHARSET); + byte[] value = formatCrossProcessPropagationContextData().getBytes(ByteBufferContext.CHARSET); + ((ByteBuffer) carrier).put(ByteBufferContext.ENTRY); + ((ByteBuffer) carrier).putInt(key.length); + ((ByteBuffer) carrier).putInt(value.length); + ((ByteBuffer) carrier).put(key); + ((ByteBuffer) carrier).put(value); + } else { + throw new IllegalArgumentException("Unsupported format: " + format); + } } @Override - public SpanContext extract(Format format, C c) { - return null; + public SpanContext extract(Format format, C carrier) { + if (Format.Builtin.TEXT_MAP.equals(format) || Format.Builtin.HTTP_HEADERS.equals(format)) { + TextMap textMapCarrier = (TextMap) carrier; + extractCrossProcessPropagationContextData(textMapCarrier); + return new TextMapContext(textMapCarrier); + } else if (Format.Builtin.BINARY.equals(format)) { + ByteBuffer byteBufferCarrier = (ByteBuffer)carrier; + extractCrossProcessPropagationContextData(byteBufferCarrier); + return new ByteBufferContext((ByteBuffer)carrier); + } else { + throw new IllegalArgumentException("Unsupported format: " + format); + } + } + + + /** + * set context data in toolkit-opentracing-activation + * + * @return + */ + private String formatCrossProcessPropagationContextData() { + return ""; + } + + /** + * read context data in toolkit-opentracing-activation + * + * @param textMapCarrier + */ + private void extractCrossProcessPropagationContextData(TextMap textMapCarrier) { + + } + + /** + * read context data in toolkit-opentracing-activation + * + * @param byteBufferCarrier + */ + private void extractCrossProcessPropagationContextData(ByteBuffer byteBufferCarrier) { + } } diff --git a/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/main/java/com/a/eye/skywalking/toolkit/opentracing/TextMapContext.java b/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/main/java/com/a/eye/skywalking/toolkit/opentracing/TextMapContext.java new file mode 100644 index 000000000..66d1f443b --- /dev/null +++ b/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/main/java/com/a/eye/skywalking/toolkit/opentracing/TextMapContext.java @@ -0,0 +1,23 @@ +package com.a.eye.skywalking.toolkit.opentracing; + +import io.opentracing.SpanContext; +import io.opentracing.propagation.TextMap; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by wusheng on 2016/12/21. + */ +public class TextMapContext implements SpanContext { + private final TextMap textMap; + + TextMapContext(TextMap textMap) { + this.textMap = textMap; + } + + @Override + public Iterable> baggageItems() { + return new HashMap().entrySet(); + } +} diff --git a/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/test/java/com/a/eye/skywalking/toolkit/opentracing/SkyWalkingTracerTest.java b/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/test/java/com/a/eye/skywalking/toolkit/opentracing/SkyWalkingTracerTest.java new file mode 100644 index 000000000..159e905aa --- /dev/null +++ b/skywalking-application-toolkit/skywalking-toolkit-opentracing/src/test/java/com/a/eye/skywalking/toolkit/opentracing/SkyWalkingTracerTest.java @@ -0,0 +1,39 @@ +package com.a.eye.skywalking.toolkit.opentracing; + +import io.opentracing.Span; +import io.opentracing.SpanContext; +import io.opentracing.Tracer; +import io.opentracing.propagation.TextMap; +import org.junit.Test; + +import java.util.Iterator; +import java.util.Map; + +/** + * Created by wusheng on 2016/12/21. + */ +public class SkyWalkingTracerTest { + @Test + public void testBuildSpan(){ + Tracer tracer = SkyWalkingTracer.INSTANCE; + Tracer.SpanBuilder spanBuilder = tracer.buildSpan("/http/serviceName"); + + SpanContext context = new TextMapContext(new TextMap() { + + @Override + public Iterator> iterator() { + throw new UnsupportedOperationException( + "TextMapInjectAdapter should only be used with Tracer.inject()"); + } + + @Override + public void put(String key, String value) { + + } + }); + spanBuilder.asChildOf(context).withTag("example.tag", "testtag"); + Span span = spanBuilder.start(); + + span.finish(); + } +}