Search in sources :

Example 6 with SofaTracerSpanContext

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

the class RpcSofaTracer method startRpc.

@Override
public void startRpc(SofaRequest request) {
    // 客户端的启动
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan serverSpan = sofaTraceContext.pop();
    SofaTracerSpan clientSpan = (SofaTracerSpan) this.sofaTracer.buildSpan(request.getInterfaceName()).asChildOf(serverSpan).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).start();
    if (RpcInternalContext.isAttachmentEnable()) {
        RpcInternalContext context = RpcInternalContext.getContext();
        clientSpan.setTag(RpcSpanTags.LOCAL_APP, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_APP_NAME));
        clientSpan.setTag(RpcSpanTags.PROTOCOL, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_PROTOCOL_NAME));
        SofaTracerSpanContext spanContext = clientSpan.getSofaTracerSpanContext();
        if (spanContext != null) {
            context.setAttachment(RpcConstants.INTERNAL_KEY_TRACE_ID, spanContext.getTraceId());
            context.setAttachment(RpcConstants.INTERNAL_KEY_SPAN_ID, spanContext.getSpanId());
        }
    }
    clientSpan.setTag(RpcSpanTags.SERVICE, request.getTargetServiceUniqueName());
    clientSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
    clientSpan.setTag(RpcSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
    // 需要主动缓存自己的 serverSpan,原因是:asChildOf 关注的是 spanContext
    clientSpan.setParentSofaTracerSpan(serverSpan);
    // push
    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) SofaTracerSpanContext(com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)

Example 7 with SofaTracerSpanContext

use of com.alipay.common.tracer.core.context.span.SofaTracerSpanContext 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 8 with SofaTracerSpanContext

use of com.alipay.common.tracer.core.context.span.SofaTracerSpanContext 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

SofaTracerSpanContext (com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)8 SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)6 SofaTracerSpan (com.alipay.common.tracer.core.span.SofaTracerSpan)6 HashMap (java.util.HashMap)4 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)3 Map (java.util.Map)3 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)1 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)1 TreeMap (java.util.TreeMap)1