Search in sources :

Example 31 with SofaTracerSpan

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);
    }
}
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 32 with SofaTracerSpan

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);
        }
    }
}
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 33 with SofaTracerSpan

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

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

Example 35 with SofaTracerSpan

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

Aggregations

SofaTracerSpan (com.alipay.common.tracer.core.span.SofaTracerSpan)35 SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)25 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)12 Test (org.junit.Test)12 SofaTracerSpanContext (com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)6 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)6 HashMap (java.util.HashMap)6 Metadata (io.grpc.Metadata)4 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)3 Context (io.grpc.Context)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 FlexibleTracer (com.alipay.sofa.tracer.plugin.flexible.FlexibleTracer)2 InetSocketAddress (java.net.InetSocketAddress)2 Before (org.junit.Before)2 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1