use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class RpcSofaTracer method clientBeforeSend.
@Override
public void clientBeforeSend(SofaRequest request) {
// 客户端的启动
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
// 获取并不弹出
SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
if (clientSpan == null) {
SelfLog.warn("ClientSpan is null.Before call interface=" + request.getInterfaceName() + ",method=" + request.getMethodName());
return;
}
SofaTracerSpanContext sofaTracerSpanContext = clientSpan.getSofaTracerSpanContext();
// 获取 RPC 上下文
RpcInternalContext rpcInternalContext = RpcInternalContext.getContext();
ProviderInfo providerInfo;
if ((providerInfo = rpcInternalContext.getProviderInfo()) != null && getRpcVersionFromProvider(providerInfo) >= 50100) {
// 版本>5.1.0
// 新调用新:缓存在 Request 中
String serializedSpanContext = sofaTracerSpanContext.serializeSpanContext();
request.addRequestProp(RemotingConstants.NEW_RPC_TRACE_NAME, serializedSpanContext);
} else {
// 新调用老
Map<String, String> oldTracerContext = new HashMap<String, String>();
oldTracerContext.put(TracerCompatibleConstants.TRACE_ID_KEY, sofaTracerSpanContext.getTraceId());
oldTracerContext.put(TracerCompatibleConstants.RPC_ID_KEY, sofaTracerSpanContext.getSpanId());
// 将采样标记解析并传递
oldTracerContext.put(TracerCompatibleConstants.SAMPLING_MARK, String.valueOf(sofaTracerSpanContext.isSampled()));
// 业务
oldTracerContext.put(TracerCompatibleConstants.PEN_ATTRS_KEY, sofaTracerSpanContext.getBizSerializedBaggage());
// 系统
oldTracerContext.put(TracerCompatibleConstants.PEN_SYS_ATTRS_KEY, sofaTracerSpanContext.getSysSerializedBaggage());
request.addRequestProp(RemotingConstants.RPC_TRACE_NAME, oldTracerContext);
}
}
use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class RestTracerAdapter method serverFilter.
/**
* 适配服务端filter
*
* @param requestContext ContainerRequestContext
*/
public static void serverFilter(ContainerRequestContext requestContext) {
try {
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
if (serverSpan != null) {
RpcInternalContext context = RpcInternalContext.getContext();
context.setAttachment(RpcConstants.INTERNAL_KEY_SERVER_RECEIVE_TIME, RpcRuntimeContext.now());
SofaResourceMethodInvoker resourceMethodInvoker = (SofaResourceMethodInvoker) ((PostMatchContainerRequestContext) requestContext).getResourceMethod();
SofaResourceFactory factory = resourceMethodInvoker.getResource();
String serviceName = factory.getServiceName();
String appName = factory.getAppName();
if (serviceName == null) {
serviceName = resourceMethodInvoker.getResourceClass().getName();
}
serverSpan.setTag(RpcSpanTags.SERVICE, serviceName);
if (resourceMethodInvoker.getMethod() != null) {
serverSpan.setTag(RpcSpanTags.METHOD, resourceMethodInvoker.getMethod().getName());
// serverSend需要
context.setAttachment(METHOD_TYPE_STRING, resourceMethodInvoker.getMethod());
}
// 客户端地址
serverSpan.setTag(RpcSpanTags.REMOTE_IP, context.getRemoteHostName());
String remoteAppName = requestContext.getHeaderString(RemotingConstants.HEAD_APP_NAME);
if (StringUtils.isNotBlank(remoteAppName)) {
serverSpan.setTag(RpcSpanTags.REMOTE_APP, remoteAppName);
}
serverSpan.setTag(RpcSpanTags.PROTOCOL, RpcConstants.PROTOCOL_TYPE_REST);
serverSpan.setTag(RpcSpanTags.INVOKE_TYPE, RpcConstants.INVOKER_TYPE_SYNC);
if (appName == null) {
appName = (String) RpcRuntimeContext.get(RpcRuntimeContext.KEY_APPNAME);
}
serverSpan.setTag(RpcSpanTags.LOCAL_APP, appName);
}
} catch (Throwable t) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("the process of rest tracer server filter occur error ", t);
}
}
}
use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.
the class RpcSofaTracerTest method testStartRpc.
/**
* Method: startRpc(SofaRequest request)
*/
@Test
public void testStartRpc() throws Exception {
this.rpcSofaTracer.startRpc(sofaRequest);
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan sofaTracerSpan = sofaTraceContext.pop();
assertNotNull(sofaTracerSpan);
System.err.println("\n" + sofaTracerSpan);
}
use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-boot by sofastack.
the class ZipkinSofaTracerSpanRemoteReporterTest method testDoServerReport.
@Test
public void testDoServerReport() throws Exception {
// sr TL
SofaTracerSpan sofaTracerServerSpan = this.remoteTracer.serverReceive();
sofaTracerServerSpan.setOperationName("mockOperationName");
// ss TL
this.remoteTracer.serverSend("0");
// 异步汇报,所以 sleep 1s
Thread.sleep(1000);
// assert
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
assertTrue(sofaTraceContext.isEmpty());
}
use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-boot by sofastack.
the class ZipkinSofaTracerSpanRemoteReporterTest method mockRemoteCall.
public void mockRemoteCall() throws Exception {
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan originSofaTracerSpan = sofaTraceContext.getCurrentSpan();
SofaTracerSpanContext spanContext = originSofaTracerSpan.getSofaTracerSpanContext();
sofaTraceContext.clear();
// sr
SofaTracerSpan srSpan = this.remoteTracer.serverReceive(spanContext);
srSpan.setOperationName("ServerReceive1");
assertEquals(srSpan, sofaTraceContext.getCurrentSpan());
// ss
this.remoteTracer.serverSend("0");
assertTrue(sofaTraceContext.getThreadLocalSpanSize() == 0);
// mock restore
sofaTraceContext.push(originSofaTracerSpan);
}
Aggregations