Search in sources :

Example 21 with SofaTraceContext

use of com.alipay.common.tracer.core.context.trace.SofaTraceContext in project sofa-rpc by sofastack.

the class RpcSofaTracer method clientAsyncReceivedPrepare.

@Override
public void clientAsyncReceivedPrepare() {
    // 新的线程
    RpcInternalContext rpcInternalContext = RpcInternalContext.getContext();
    SofaTracerSpan clientSpan = (SofaTracerSpan) rpcInternalContext.getAttachment(RpcConstants.INTERNAL_KEY_TRACER_SPAN);
    if (clientSpan == null) {
        return;
    }
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    sofaTraceContext.push(clientSpan);
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext)

Example 22 with SofaTraceContext

use of com.alipay.common.tracer.core.context.trace.SofaTraceContext 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);
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) HashMap(java.util.HashMap) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaTracerSpanContext(com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)

Example 23 with SofaTraceContext

use of com.alipay.common.tracer.core.context.trace.SofaTraceContext 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);
        }
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaResourceMethodInvoker(com.alipay.sofa.rpc.server.rest.SofaResourceMethodInvoker) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResourceFactory(com.alipay.sofa.rpc.server.rest.SofaResourceFactory)

Example 24 with SofaTraceContext

use of com.alipay.common.tracer.core.context.trace.SofaTraceContext 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);
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) Test(org.junit.Test)

Example 25 with SofaTraceContext

use of com.alipay.common.tracer.core.context.trace.SofaTraceContext 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());
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) Test(org.junit.Test)

Aggregations

SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)26 SofaTracerSpan (com.alipay.common.tracer.core.span.SofaTracerSpan)25 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)13 Test (org.junit.Test)7 SofaTracerSpanContext (com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)6 HashMap (java.util.HashMap)6 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)4 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)3 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)2 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)2 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2 InetSocketAddress (java.net.InetSocketAddress)2 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 RpcRuntimeContext (com.alipay.sofa.rpc.context.RpcRuntimeContext)1 ServerReceiveEvent (com.alipay.sofa.rpc.event.ServerReceiveEvent)1 SofaResourceFactory (com.alipay.sofa.rpc.server.rest.SofaResourceFactory)1 SofaResourceMethodInvoker (com.alipay.sofa.rpc.server.rest.SofaResourceMethodInvoker)1 Context (io.grpc.Context)1 ForwardingServerCall (io.grpc.ForwardingServerCall)1 ForwardingServerCallListener (io.grpc.ForwardingServerCallListener)1