fix issue that skywalking extract binary data failed and refectory code
This commit is contained in:
parent
fd02bbe754
commit
0544edabf7
|
|
@ -6,6 +6,9 @@ import io.opentracing.propagation.Format;
|
|||
import io.opentracing.propagation.TextMap;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* All source code in SkyWalkingTracer acts like an NoopTracer.
|
||||
|
|
@ -15,10 +18,11 @@ import java.nio.ByteBuffer;
|
|||
* Created by wusheng on 2016/12/20.
|
||||
*/
|
||||
public class SkyWalkingTracer implements Tracer {
|
||||
private static String TRACE_HEAD_NAME = "SW-TRACING-NAME";
|
||||
private static String TRACE_HEAD_NAME = "sw3";
|
||||
|
||||
public static Tracer INSTANCE = new SkyWalkingTracer();
|
||||
|
||||
|
||||
@Override
|
||||
public SpanBuilder buildSpan(String operationName) {
|
||||
return new SkyWalkingSpanBuilder(operationName);
|
||||
|
|
@ -27,15 +31,13 @@ public class SkyWalkingTracer implements Tracer {
|
|||
@Override
|
||||
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());
|
||||
((TextMap) carrier).put(TRACE_HEAD_NAME, formatInjectCrossProcessPropagationContextData());
|
||||
} 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);
|
||||
byte[] value = formatInjectCrossProcessPropagationContextData().getBytes(ByteBufferContext.CHARSET);
|
||||
((ByteBuffer) carrier).put(key);
|
||||
((ByteBuffer) carrier).putInt(value.length);
|
||||
((ByteBuffer) carrier).put(value);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unsupported format: " + format);
|
||||
}
|
||||
|
|
@ -44,13 +46,13 @@ public class SkyWalkingTracer implements Tracer {
|
|||
@Override
|
||||
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);
|
||||
TextMap textMapCarrier = (TextMap) carrier;
|
||||
formatExtractCrossProcessPropagationContextData(fetchContextData(textMapCarrier));
|
||||
return new TextMapContext(textMapCarrier);
|
||||
} else if (Format.Builtin.BINARY.equals(format)) {
|
||||
ByteBuffer byteBufferCarrier = (ByteBuffer)carrier;
|
||||
extractCrossProcessPropagationContextData(byteBufferCarrier);
|
||||
return new ByteBufferContext((ByteBuffer)carrier);
|
||||
ByteBuffer byteBufferCarrier = (ByteBuffer) carrier;
|
||||
formatExtractCrossProcessPropagationContextData(fetchContextData(byteBufferCarrier));
|
||||
return new ByteBufferContext((ByteBuffer) carrier);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unsupported format: " + format);
|
||||
}
|
||||
|
|
@ -58,28 +60,43 @@ public class SkyWalkingTracer implements Tracer {
|
|||
|
||||
/**
|
||||
* set context data in toolkit-opentracing-activation
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private String formatCrossProcessPropagationContextData() {
|
||||
private String formatInjectCrossProcessPropagationContextData() {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* read context data in toolkit-opentracing-activation
|
||||
*
|
||||
* @param textMapCarrier
|
||||
*/
|
||||
private void extractCrossProcessPropagationContextData(TextMap textMapCarrier) {
|
||||
|
||||
private void formatExtractCrossProcessPropagationContextData(String contextData) {
|
||||
}
|
||||
|
||||
/**
|
||||
* read context data in toolkit-opentracing-activation
|
||||
*
|
||||
* @param byteBufferCarrier
|
||||
*/
|
||||
private void extractCrossProcessPropagationContextData(ByteBuffer byteBufferCarrier) {
|
||||
private String fetchContextData(TextMap textMap) {
|
||||
Iterator<Map.Entry<String, String>> iterator = textMap.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<String, String> entry = iterator.next();
|
||||
if (TRACE_HEAD_NAME.equals(entry.getKey())) {
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private String fetchContextData(ByteBuffer byteBuffer) {
|
||||
String contextDataStr = new String(byteBuffer.array(), Charset.forName("UTF-8"));
|
||||
int index = contextDataStr.indexOf(TRACE_HEAD_NAME);
|
||||
if (index == -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
byteBuffer.position(index + TRACE_HEAD_NAME.getBytes().length);
|
||||
byte[] contextDataBytes = new byte[byteBuffer.getInt()];
|
||||
byteBuffer.get(contextDataBytes);
|
||||
return new String(contextDataBytes, Charset.forName("UTF-8"));
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,10 @@ package com.a.eye.skywalking.toolkit.activation.opentracing.span;
|
|||
|
||||
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.enhance.ClassInstanceMethodsEnhancePluginDefine;
|
||||
import java.util.Map;
|
||||
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.takesArguments;
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@ import com.a.eye.skywalking.api.plugin.interceptor.enhance.InstanceMethodsAround
|
|||
import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult;
|
||||
import com.a.eye.skywalking.toolkit.opentracing.SkyWalkingSpan;
|
||||
|
||||
import io.opentracing.Span;
|
||||
import io.opentracing.tag.Tags;
|
||||
|
||||
/**
|
||||
* Intercept these following methods:
|
||||
* {@link SkyWalkingSpan#setTag(String, boolean)}
|
||||
|
|
@ -15,10 +18,15 @@ import com.a.eye.skywalking.toolkit.opentracing.SkyWalkingSpan;
|
|||
*/
|
||||
public class SpanSetTagInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
|
||||
/**
|
||||
* key of {@link com.a.eye.skywalking.trace.tag.Tags#PEER_HOST}
|
||||
*/
|
||||
private static final String KEY_OF_PEER_HOST_TAG = "peer.host";
|
||||
|
||||
@Override
|
||||
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
|
||||
MethodInterceptResult result) {
|
||||
String key = (String)interceptorContext.allArguments()[0];
|
||||
String key = fetchTagKeyFromArguments(interceptorContext.allArguments());
|
||||
Object value = interceptorContext.allArguments()[1];
|
||||
if (value instanceof String)
|
||||
ContextManager.activeSpan().setTag(key, (String)value);
|
||||
|
|
@ -30,6 +38,32 @@ public class SpanSetTagInterceptor implements InstanceMethodsAroundInterceptor {
|
|||
ContextManager.activeSpan().setTag(key, value.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch tag key of {@link Span#setTag}.
|
||||
*
|
||||
* @return tag key
|
||||
*/
|
||||
private String fetchTagKeyFromArguments(Object[] arguments) {
|
||||
String key = (String)arguments[0];
|
||||
|
||||
if (isPeerHostPrefix(key)) {
|
||||
key = KEY_OF_PEER_HOST_TAG;
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Skywalking put the tag value of {@link Tags#PEER_HOSTNAME}, {@link Tags#PEER_HOST_IPV4} and
|
||||
* {@link Tags#PEER_HOST_IPV6} into {@link com.a.eye.skywalking.trace.tag.Tags#PEER_HOST} which
|
||||
* facilitate analysis.
|
||||
*
|
||||
* @param key tag key
|
||||
*/
|
||||
private boolean isPeerHostPrefix(String key) {
|
||||
return Tags.PEER_HOST_IPV4.equals(key) || Tags.PEER_HOST_IPV6.equals(key) || Tags.PEER_HOSTNAME.equals(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
|
||||
Object ret) {
|
||||
|
|
|
|||
|
|
@ -6,11 +6,7 @@ import com.a.eye.skywalking.api.plugin.interceptor.enhance.ClassInstanceMethodsE
|
|||
import net.bytebuddy.description.method.MethodDescription;
|
||||
import net.bytebuddy.matcher.ElementMatcher;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author wusheng
|
||||
|
|
@ -32,34 +28,23 @@ public class SkyWalkingTracerActivation extends ClassInstanceMethodsEnhancePlugi
|
|||
new InstanceMethodsInterceptPoint() {
|
||||
@Override
|
||||
public ElementMatcher<MethodDescription> getMethodsMatcher() {
|
||||
return named("formatCrossProcessPropagationContextData");
|
||||
return named("formatInjectCrossProcessPropagationContextData");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethodsInterceptor() {
|
||||
return "com.a.eye.skywalking.toolkit.activation.opentracing.tracer.interceptor.TracerFormatCrossProcessContextInterceptor";
|
||||
return "com.a.eye.skywalking.toolkit.activation.opentracing.tracer.interceptor.TracerInjectFormatCrossProcessContextInterceptor";
|
||||
}
|
||||
},
|
||||
new InstanceMethodsInterceptPoint() {
|
||||
@Override
|
||||
public ElementMatcher<MethodDescription> getMethodsMatcher() {
|
||||
return named("extractCrossProcessPropagationContextData").and(takesArgumentWithType(0, "io.opentracing.propagation.TextMap"));
|
||||
return named("formatExtractCrossProcessPropagationContextData");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethodsInterceptor() {
|
||||
return "com.a.eye.skywalking.toolkit.activation.opentracing.tracer.interceptor.TracerExtractCrossProcessTextMapContextInterceptor";
|
||||
}
|
||||
},
|
||||
new InstanceMethodsInterceptPoint() {
|
||||
@Override
|
||||
public ElementMatcher<MethodDescription> getMethodsMatcher() {
|
||||
return named("extractCrossProcessPropagationContextData").and(takesArgument(0, ByteBuffer.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethodsInterceptor() {
|
||||
return "com.a.eye.skywalking.toolkit.activation.opentracing.tracer.interceptor.TracerExtractCrossProcessByteBufferContextInterceptor";
|
||||
return "com.a.eye.skywalking.toolkit.activation.opentracing.tracer.interceptor.TracerExtractCrossProcessContextInterceptor";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -6,16 +6,12 @@ import com.a.eye.skywalking.api.plugin.interceptor.EnhancedClassInstanceContext;
|
|||
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;
|
||||
|
||||
/**
|
||||
* Intercept {@link SkyWalkingTracer#extractCrossProcessPropagationContextData(TextMap)}
|
||||
* Intercept {@link SkyWalkingTracer#formatExtractCrossProcessPropagationContextData(String)}
|
||||
*/
|
||||
public class TracerExtractCrossProcessByteBufferContextInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
public class TracerExtractCrossProcessContextInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
@Override
|
||||
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
|
||||
MethodInterceptResult result) {
|
||||
|
|
@ -25,8 +21,7 @@ public class TracerExtractCrossProcessByteBufferContextInterceptor implements In
|
|||
@Override
|
||||
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"));
|
||||
String contextDataStr = (String)interceptorContext.allArguments()[0];
|
||||
|
||||
ContextCarrier carrier = new ContextCarrier();
|
||||
carrier.deserialize(contextDataStr);
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
package com.a.eye.skywalking.toolkit.activation.opentracing.tracer.interceptor;
|
||||
|
||||
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.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;
|
||||
|
||||
/**
|
||||
* Intercept {@link SkyWalkingTracer#extractCrossProcessPropagationContextData(TextMap)}
|
||||
*/
|
||||
public class TracerExtractCrossProcessTextMapContextInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
|
||||
public static final String SKY_WALKING_TRACING_NAME = "SW-TRACING-NAME";
|
||||
|
||||
@Override
|
||||
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];
|
||||
Iterator<Map.Entry<String, String>> iterator = textMap.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<String, String> entry = iterator.next();
|
||||
if (SKY_WALKING_TRACING_NAME.equals(entry.getKey())) {
|
||||
ContextCarrier carrier = new ContextCarrier();
|
||||
carrier.deserialize(entry.getValue());
|
||||
|
||||
ContextManager.extract(carrier);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context,
|
||||
InstanceMethodInvokeContext interceptorContext) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -9,9 +9,9 @@ import com.a.eye.skywalking.api.plugin.interceptor.enhance.MethodInterceptResult
|
|||
import com.a.eye.skywalking.toolkit.opentracing.SkyWalkingTracer;
|
||||
|
||||
/**
|
||||
* Intercept {@link SkyWalkingTracer#formatCrossProcessPropagationContextData()}
|
||||
* Intercept {@link SkyWalkingTracer#formatInjectCrossProcessPropagationContextData()}
|
||||
*/
|
||||
public class TracerFormatCrossProcessContextInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
public class TracerInjectFormatCrossProcessContextInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
@Override
|
||||
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
|
||||
MethodInterceptResult result) {
|
||||
Loading…
Reference in New Issue