Implement necessary interfaces and methods of OT.

This commit is contained in:
wusheng 2016-12-21 10:59:18 +08:00
parent e5c562c4bb
commit 96519491db
4 changed files with 152 additions and 6 deletions

View File

@ -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<Map.Entry<String, String>> baggageItems() {
return new HashMap<String, String>().entrySet();
}
}

View File

@ -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.
*
* <p>
* 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 <C> void inject(SpanContext spanContext, Format<C> format, C c) {
public <C> void inject(SpanContext spanContext, Format<C> 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 <C> SpanContext extract(Format<C> format, C c) {
return null;
public <C> SpanContext extract(Format<C> 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) {
}
}

View File

@ -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<Map.Entry<String, String>> baggageItems() {
return new HashMap<String, String>().entrySet();
}
}

View File

@ -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<Map.Entry<String, String>> 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();
}
}