Search in sources :

Example 11 with SofaTracerSpan

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

the class ConsumerTracerFilter method invoke.

@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
    clientSpan.setTag(RpcSpanTags.INVOKE_TYPE, request.getInvokeType());
    RpcInternalContext context = RpcInternalContext.getContext();
    clientSpan.setTag(RpcSpanTags.ROUTE_RECORD, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
    ProviderInfo providerInfo = context.getProviderInfo();
    if (providerInfo != null) {
        clientSpan.setTag(RpcSpanTags.REMOTE_APP, providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME));
        clientSpan.setTag(RpcSpanTags.REMOTE_IP, providerInfo.getHost() + ":" + providerInfo.getPort());
    }
    return invoker.invoke(request);
// 因为异步的场景,所以received不写在这里
}
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) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext)

Example 12 with SofaTracerSpan

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

the class ProviderTracerFilter method invoke.

@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    SofaTracerSpan serverSpan = null;
    try {
        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        serverSpan = sofaTraceContext.getCurrentSpan();
        if (serverSpan != null) {
            RpcInternalContext context = RpcInternalContext.getContext();
            serverSpan.setTag(RpcSpanTags.SERVICE, request.getTargetServiceUniqueName());
            serverSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
            // 客户端地址
            serverSpan.setTag(RpcSpanTags.REMOTE_IP, context.getRemoteHostName());
            // 从请求里获取ConsumerTracerFilter额外传递的信息
            serverSpan.setTag(RpcSpanTags.REMOTE_APP, (String) request.getRequestProp(HEAD_APP_NAME));
            serverSpan.setTag(RpcSpanTags.PROTOCOL, (String) request.getRequestProp(HEAD_PROTOCOL));
            serverSpan.setTag(RpcSpanTags.INVOKE_TYPE, (String) request.getRequestProp(HEAD_INVOKE_TYPE));
            ProviderConfig providerConfig = (ProviderConfig) invoker.getConfig();
            serverSpan.setTag(RpcSpanTags.LOCAL_APP, providerConfig.getAppName());
            serverSpan.setTag(RpcSpanTags.SERVER_THREAD_POOL_WAIT_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_PROCESS_WAIT_TIME));
        }
        return invoker.invoke(request);
    } finally {
        if (serverSpan != null) {
            serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME, (Number) RpcInternalContext.getContext().getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
        }
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext)

Example 13 with SofaTracerSpan

use of com.alipay.common.tracer.core.span.SofaTracerSpan 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 14 with SofaTracerSpan

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

the class RpcSofaTracer method clientAsyncAfterSend.

@Override
public void clientAsyncAfterSend(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;
    }
    RpcInternalContext rpcInternalContext = RpcInternalContext.getContext();
    // 异步callback同步
    if (request.isAsync()) {
        // 异步,这个时候除了缓存spanContext clientBeforeSendRequest() rpc 已经调用
        // 还需要这个时候需要还原回父 span
        // 弹出;不弹出的话当前线程就会一直是client了
        clientSpan = sofaTraceContext.pop();
        if (clientSpan != null) {
            // Record client send event
            clientSpan.log(LogData.CLIENT_SEND_EVENT_VALUE);
        }
        // 将当前 span 缓存在 request 中,注意:这个只是缓存不需要序列化到服务端
        rpcInternalContext.setAttachment(RpcConstants.INTERNAL_KEY_TRACER_SPAN, clientSpan);
        if (clientSpan != null && clientSpan.getParentSofaTracerSpan() != null) {
            // restore parent
            sofaTraceContext.push(clientSpan.getParentSofaTracerSpan());
        }
    } else {
        // Record client send event
        clientSpan.log(LogData.CLIENT_SEND_EVENT_VALUE);
    }
}
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 15 with SofaTracerSpan

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

the class RpcSofaTracer method serverSend.

@Override
public void serverSend(SofaRequest request, SofaResponse response, Throwable exception) {
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan serverSpan = sofaTraceContext.pop();
    if (serverSpan == null) {
        return;
    }
    // Record server send event
    serverSpan.log(LogData.SERVER_SEND_EVENT_VALUE);
    RpcInternalContext context = RpcInternalContext.getContext();
    RpcInvokeContext invokeContext = RpcInvokeContext.getContext();
    serverSpan.setTag(RpcSpanTags.RESP_SERIALIZE_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SERIALIZE_TIME));
    serverSpan.setTag(RpcSpanTags.REQ_DESERIALIZE_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_DESERIALIZE_TIME));
    serverSpan.setTag(RpcSpanTags.RESP_SIZE, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE));
    serverSpan.setTag(RpcSpanTags.REQ_SIZE, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE));
    // 当前线程名
    serverSpan.setTag(RpcSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
    serverSpan.setTag(RpcSpanTags.SERVER_PHASE_TIME_COST, generateServerPhaseTimeCostSpan(invokeContext));
    serverSpan.setTag(RpcSpanTags.SERVER_SPECIAL_TIME_MARK, generateServerSpecialTimeMark(invokeContext));
    Throwable throwableShow = exception;
    String tracerErrorCode = StringUtils.EMPTY;
    String errorSourceApp = StringUtils.EMPTY;
    String resultCode = StringUtils.EMPTY;
    if (throwableShow != null) {
        // 当前即服务端应用
        errorSourceApp = serverSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
        // 结果码(00=成功/01=业务异常/02=RPC逻辑错误)
        // 不会业务异常
        resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
        tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
    } else if (response != null) {
        // 判断是否是业务异常
        if (response.isError()) {
            errorSourceApp = serverSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
            resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
            tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
            // 改变打印的 throwable
            throwableShow = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
        } else {
            Object ret = response.getAppResponse();
            if (ret instanceof Throwable) {
                throwableShow = (Throwable) ret;
                errorSourceApp = serverSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
                // 业务异常
                resultCode = TracerResultCode.RPC_RESULT_BIZ_FAILED;
                tracerErrorCode = TracerResultCode.RPC_RESULT_BIZ_FAILED;
            } else {
                resultCode = TracerResultCode.RPC_RESULT_SUCCESS;
            }
        }
    }
    if (throwableShow != null) {
        // 打印错误
        // result code
        Map<String, String> errorContext = new HashMap<String, String>();
        // 记录的上下文信息
        this.generateServerErrorContext(errorContext, request, serverSpan);
        // report
        serverSpan.reportError(tracerErrorCode, errorContext, throwableShow, errorSourceApp, ERROR_SOURCE);
    }
    // 结果码(00=成功/01=业务异常/02=RPC逻辑错误)
    serverSpan.setTag(RpcSpanTags.RESULT_CODE, resultCode);
    serverSpan.finish();
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) HashMap(java.util.HashMap) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

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