Search in sources :

Example 1 with SofaTracerSpanContext

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

the class RpcSofaTracer method serverReceived.

@Override
public void serverReceived(SofaRequest request) {
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    Map<String, String> tags = new HashMap<String, String>();
    // server tags 必须设置
    tags.put(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER);
    String spanStrs = (String) request.getRequestProp(RemotingConstants.NEW_RPC_TRACE_NAME);
    SofaTracerSpanContext spanContext = null;
    if (StringUtils.isBlank(spanStrs)) {
        // 老
        Object oldInstanceMap = request.getRequestProp(RemotingConstants.RPC_TRACE_NAME);
        spanContext = this.saveSpanContextAndTags(tags, oldInstanceMap);
    } else {
        // 新
        spanContext = SofaTracerSpanContext.deserializeFromString(spanStrs);
    }
    SofaTracerSpan serverSpan;
    // 使用客户端的进行初始化,如果上游没有,需要新建
    if (spanContext == null) {
        serverSpan = (SofaTracerSpan) this.sofaTracer.buildSpan(request.getInterfaceName()).asChildOf(spanContext).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER).start();
    } else {
        // 有的话,需要new,采样会正确
        serverSpan = new SofaTracerSpan(this.sofaTracer, System.currentTimeMillis(), request.getInterfaceName(), spanContext, tags);
    }
    // 重新获取
    spanContext = serverSpan.getSofaTracerSpanContext();
    // Record server receive event
    serverSpan.log(LogData.SERVER_RECV_EVENT_VALUE);
    // 放到线程上下文
    sofaTraceContext.push(serverSpan);
    // rpc 上下文
    if (RpcInternalContext.isAttachmentEnable()) {
        RpcInternalContext context = RpcInternalContext.getContext();
        context.setAttachment(RpcConstants.INTERNAL_KEY_TRACE_ID, spanContext.getTraceId());
        context.setAttachment(RpcConstants.INTERNAL_KEY_SPAN_ID, spanContext.getSpanId());
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) 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 2 with SofaTracerSpanContext

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

the class RpcSofaTracer method saveSpanContextAndTags.

private SofaTracerSpanContext saveSpanContextAndTags(Map<String, String> tags, Object oldInstanceMap) {
    if (oldInstanceMap instanceof Map) {
        try {
            Map<String, String> contextMap = (Map<String, String>) oldInstanceMap;
            String traceId = this.getEmptyStringIfNull(contextMap, TracerCompatibleConstants.TRACE_ID_KEY);
            String rpcId = this.getEmptyStringIfNull(contextMap, TracerCompatibleConstants.RPC_ID_KEY);
            String bizBaggage = this.getEmptyStringIfNull(contextMap, TracerCompatibleConstants.PEN_ATTRS_KEY);
            String sysBaggage = this.getEmptyStringIfNull(contextMap, TracerCompatibleConstants.PEN_SYS_ATTRS_KEY);
            String callerApp = this.getEmptyStringIfNull(contextMap, TracerCompatibleConstants.CALLER_APP_KEY);
            String callerZone = this.getEmptyStringIfNull(contextMap, TracerCompatibleConstants.CALLER_ZONE_KEY);
            String callerIdc = this.getEmptyStringIfNull(contextMap, TracerCompatibleConstants.CALLER_IDC_KEY);
            String callerIp = this.getEmptyStringIfNull(contextMap, TracerCompatibleConstants.CALLER_IP_KEY);
            SofaTracerSpanContext spanContext = new SofaTracerSpanContext(traceId, rpcId);
            spanContext.deserializeBizBaggage(bizBaggage);
            spanContext.deserializeSysBaggage(sysBaggage);
            // 解析采样标记
            spanContext.setSampled(parseSampled(contextMap, spanContext));
            // tags
            tags.put(RpcSpanTags.REMOTE_APP, callerApp);
            tags.put(RpcSpanTags.REMOTE_ZONE, callerZone);
            tags.put(RpcSpanTags.REMOTE_IDC, callerIdc);
            tags.put(RpcSpanTags.REMOTE_IP, callerIp);
            return spanContext;
        } catch (Throwable throwable) {
            return null;
        }
    } else {
        return null;
    }
}
Also used : SofaTracerSpanContext(com.alipay.common.tracer.core.context.span.SofaTracerSpanContext) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 3 with SofaTracerSpanContext

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

the class AbstractRpcDigestSpanJsonEncoder method appendSlot.

public void appendSlot(JsonStringBuilder jsb, SofaTracerSpan span) {
    SofaTracerSpanContext spanContext = span.getSofaTracerSpanContext();
    // traceId
    jsb.append(RpcSpanTags.TRACERID, spanContext.getTraceId());
    // spanId
    jsb.append(RpcSpanTags.SPANID, spanContext.getSpanId());
    // tags
    Map<String, String> tagsWithStr = span.getTagsWithStr();
    if (CommonUtils.isNotEmpty(tagsWithStr)) {
        for (Map.Entry<String, String> entry : tagsWithStr.entrySet()) {
            jsb.append(entry.getKey(), entry.getValue());
        }
    }
    Map<String, Number> tagsWithNumber = span.getTagsWithNumber();
    if (CommonUtils.isNotEmpty(tagsWithNumber)) {
        for (Map.Entry<String, Number> entry : tagsWithNumber.entrySet()) {
            Number value = entry.getValue();
            jsb.append(entry.getKey(), value == null ? null : String.valueOf(value));
        }
    }
    Map<String, Boolean> tagsWithBool = span.getTagsWithBool();
    if (CommonUtils.isNotEmpty(tagsWithBool)) {
        for (Map.Entry<String, Boolean> entry : tagsWithBool.entrySet()) {
            jsb.append(entry.getKey(), entry.getValue());
        }
    }
    // 系统穿透数据(kv 格式,用于传送系统灾备信息等)
    jsb.append(RpcSpanTags.BAGGAGE, baggageSerialized(spanContext));
}
Also used : SofaTracerSpanContext(com.alipay.common.tracer.core.context.span.SofaTracerSpanContext) Map(java.util.Map)

Example 4 with SofaTracerSpanContext

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

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)

Example 5 with SofaTracerSpanContext

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

the class TripleTracerAdapter method beforeSend.

/**
 * 存入tracer信息
 *
 * @param sofaRequest   SofaRequest
 * @param requestHeader Metadata
 */
public static void beforeSend(SofaRequest sofaRequest, ConsumerConfig consumerConfig, Metadata requestHeader) {
    // 客户端设置请求服务端的Header
    // tracer信息放入request 发到服务端
    Map<String, String> header = new HashMap<String, String>();
    header.put(RemotingConstants.HEAD_METHOD_NAME, sofaRequest.getMethodName());
    header.put(RemotingConstants.HEAD_TARGET_SERVICE, sofaRequest.getTargetServiceUniqueName());
    header.put(RemotingConstants.HEAD_TARGET_APP, sofaRequest.getTargetAppName());
    // 客户端的启动
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    // 获取并不弹出
    SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
    if (clientSpan == null) {
        SelfLog.warn("ClientSpan is null.Before call interface=" + sofaRequest.getInterfaceName() + ",method=" + sofaRequest.getMethodName());
    } else {
        SofaTracerSpanContext sofaTracerSpanContext = clientSpan.getSofaTracerSpanContext();
        header.put(TripleHeadKeys.HEAD_KEY_TRACE_ID.name(), sofaTracerSpanContext.getTraceId());
        header.put(TripleHeadKeys.HEAD_KEY_RPC_ID.name(), sofaTracerSpanContext.getSpanId());
        header.put(TripleHeadKeys.HEAD_KEY_OLD_TRACE_ID.name(), sofaTracerSpanContext.getTraceId());
        header.put(TripleHeadKeys.HEAD_KEY_OLD_RPC_ID.name(), sofaTracerSpanContext.getSpanId());
        header.put(TripleHeadKeys.HEAD_KEY_BIZ_BAGGAGE_TYPE.name(), sofaTracerSpanContext.getBizSerializedBaggage());
        header.put(TripleHeadKeys.HEAD_KEY_SYS_BAGGAGE_TYPE.name(), sofaTracerSpanContext.getSysSerializedBaggage());
    }
    // 获取 RPC 上下文
    RpcInvokeContext internalContext = RpcInvokeContext.getContext();
    String route = (String) internalContext.get(USERID_KEY);
    if (StringUtils.isNotBlank(route)) {
        Map<String, String> map = new HashMap<>();
        map.put(USERID_KEY, route);
        header.put(TripleHeadKeys.HEAD_KEY_UNIT_INFO.name(), JSONUtils.toJSONString(map));
    }
    if (StringUtils.isNotEmpty(consumerConfig.getUniqueId())) {
        header.put(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION.name(), consumerConfig.getUniqueId());
    }
    header.put(TripleHeadKeys.HEAD_KEY_META_TYPE.name(), "rpc");
    header.put(TripleHeadKeys.HEAD_KEY_CURRENT_APP.name(), (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
    header.put(TripleHeadKeys.HEAD_KEY_CONSUMER_APP.name(), (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
    header.put(TripleHeadKeys.HEAD_KEY_PROTOCOL_TYPE.name(), (String) sofaRequest.getRequestProp(RemotingConstants.HEAD_PROTOCOL));
    header.put(TripleHeadKeys.HEAD_KEY_INVOKE_TYPE.name(), (String) sofaRequest.getRequestProp(RemotingConstants.HEAD_INVOKE_TYPE));
    final String source = consumerConfig.getParameter("interworking.source");
    if (StringUtils.isNotBlank(source)) {
        header.put(TripleHeadKeys.HEAD_KEY_SOURCE_TENANTID.name(), source);
    }
    final String target = consumerConfig.getParameter("interworking.target");
    if (StringUtils.isNotBlank(target)) {
        header.put(TripleHeadKeys.HEAD_KEY_TARGET_TENANTID.name(), target);
    }
    for (Map.Entry<String, String> entry : header.entrySet()) {
        if (StringUtils.isNotBlank(entry.getValue())) {
            requestHeader.put(TripleHeadKeys.getKey(entry.getKey()), entry.getValue());
        }
    }
    // set custom headers
    try {
        Set<Map.Entry<String, String>> customHeader = RpcInvokeContext.getContext().getCustomHeader().entrySet();
        for (Map.Entry<String, String> entry : customHeader) {
            if (StringUtils.isNotBlank(entry.getValue())) {
                requestHeader.put(TripleHeadKeys.getKey(entry.getKey()), entry.getValue());
            }
        }
    } finally {
        RpcInvokeContext.getContext().clearCustomHeader();
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) HashMap(java.util.HashMap) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) SofaTracerSpanContext(com.alipay.common.tracer.core.context.span.SofaTracerSpanContext) HashMap(java.util.HashMap) Map(java.util.Map)

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