Search in sources :

Example 16 with SofaTracerSpan

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

the class RpcSofaTracer method clientReceived.

@Override
public void clientReceived(SofaRequest request, SofaResponse response, Throwable exceptionThrow) {
    // 客户端的启动
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan clientSpan = sofaTraceContext.pop();
    if (clientSpan == null) {
        return;
    }
    // Record client receive event
    clientSpan.log(LogData.CLIENT_RECV_EVENT_VALUE);
    // rpc 上下文
    RpcInternalContext context = null;
    if (RpcInternalContext.isAttachmentEnable()) {
        context = RpcInternalContext.getContext();
        if (!clientSpan.getTagsWithStr().containsKey(RpcSpanTags.ROUTE_RECORD)) {
            clientSpan.setTag(RpcSpanTags.ROUTE_RECORD, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
        }
        clientSpan.setTag(RpcSpanTags.REQ_SERIALIZE_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SERIALIZE_TIME));
        clientSpan.setTag(RpcSpanTags.RESP_DESERIALIZE_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_DESERIALIZE_TIME));
        clientSpan.setTag(RpcSpanTags.RESP_SIZE, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE));
        clientSpan.setTag(RpcSpanTags.REQ_SIZE, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE));
        clientSpan.setTag(RpcSpanTags.CLIENT_CONN_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_CONN_CREATE_TIME));
        Long ce = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE);
        if (ce != null) {
            clientSpan.setTag(RpcSpanTags.CLIENT_ELAPSE_TIME, ce);
        }
        InetSocketAddress address = context.getLocalAddress();
        if (address != null) {
            clientSpan.setTag(RpcSpanTags.LOCAL_IP, NetUtils.toIpString(address));
            clientSpan.setTag(RpcSpanTags.LOCAL_PORT, address.getPort());
        }
        // adjust for generic invoke
        clientSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
    }
    RpcInvokeContext invokeContext = RpcInvokeContext.getContext();
    clientSpan.setTag(RpcSpanTags.PHASE_TIME_COST, generateClientPhaseTimeCostSpan(invokeContext));
    clientSpan.setTag(RpcSpanTags.SPECIAL_TIME_MARK, generateClientSpecialTimeMarkSpan(invokeContext));
    Throwable throwableShow = exceptionThrow;
    // 区分出各个异常信息
    String resultCode = StringUtils.EMPTY;
    // 当前应用或者目标应用
    String errorSourceApp = StringUtils.EMPTY;
    String tracerErrorCode = StringUtils.EMPTY;
    if (throwableShow != null) {
        // 客户端异常
        if (throwableShow instanceof SofaRpcException) {
            SofaRpcException exception = (SofaRpcException) throwableShow;
            // 摘要打印
            int errorType = exception.getErrorType();
            switch(errorType) {
                case RpcErrorType.CLIENT_TIMEOUT:
                    resultCode = TracerResultCode.RPC_RESULT_TIMEOUT_FAILED;
                    // filter 已经存放
                    errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
                    tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_TIMEOUT_ERROR;
                    break;
                case RpcErrorType.CLIENT_ROUTER:
                    resultCode = TracerResultCode.RPC_RESULT_ROUTE_FAILED;
                    errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
                    tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_ADDRESS_ROUTE_ERROR;
                    break;
                case RpcErrorType.CLIENT_SERIALIZE:
                case RpcErrorType.CLIENT_DESERIALIZE:
                    resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
                    errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
                    tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_SERIALIZE_ERROR;
                    break;
                default:
                    resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
                    errorSourceApp = ExceptionUtils.isServerException(exception) ? clientSpan.getTagsWithStr().get(RpcSpanTags.REMOTE_APP) : clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
                    tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
                    break;
            }
        } else {
            // 这里是客户端的未知异常,目前不会走到这里
            resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
            errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.LOCAL_APP);
            tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
        }
    } else if (response != null) {
        // 服务端rpc异常
        if (response.isError()) {
            errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.REMOTE_APP);
            tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_UNKNOWN_ERROR;
            resultCode = TracerResultCode.RPC_RESULT_RPC_FAILED;
            // 客户端服务端均打印
            throwableShow = new SofaRpcException(RpcErrorType.SERVER_UNDECLARED_ERROR, response.getErrorMsg());
        } else {
            Object ret = response.getAppResponse();
            // for server throw exception ,but this class can not be found in current
            if (ret instanceof Throwable || "true".equals(response.getResponseProp(RemotingConstants.HEAD_RESPONSE_ERROR))) {
                errorSourceApp = clientSpan.getTagsWithStr().get(RpcSpanTags.REMOTE_APP);
                // 业务异常
                resultCode = TracerResultCode.RPC_RESULT_BIZ_FAILED;
                tracerErrorCode = TracerResultCode.RPC_ERROR_TYPE_BIZ_ERROR;
            } else {
                resultCode = TracerResultCode.RPC_RESULT_SUCCESS;
            }
        }
    }
    if (throwableShow != null) {
        Map<String, String> contextMap = new HashMap<String, String>();
        this.generateClientErrorContext(contextMap, request, clientSpan);
        clientSpan.reportError(tracerErrorCode, contextMap, throwableShow, errorSourceApp, ERROR_SOURCE);
    }
    clientSpan.setTag(RpcSpanTags.RESULT_CODE, resultCode);
    // finish client
    clientSpan.finish();
    if (context != null) {
        context.setAttachment(RpcConstants.INTERNAL_KEY_RESULT_CODE, resultCode);
    }
    // client span
    if (clientSpan.getParentSofaTracerSpan() != null) {
        // restore parent
        sofaTraceContext.push(clientSpan.getParentSofaTracerSpan());
    }
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan)

Example 17 with SofaTracerSpan

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

the class ZipkinSofaTracerSpanRemoteReporterTest method testDoClientReport.

@Test
public void testDoClientReport() throws Exception {
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    sofaTraceContext.clear();
    // cs
    SofaTracerSpan clientSpan = this.remoteTracer.clientSend("testDoClientReport");
    assertTrue(clientSpan != null);
    // cr
    this.remoteTracer.clientReceive("0");
    // assert
    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 18 with SofaTracerSpan

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

the class ZipkinSofaTracerSpanRemoteReporterTest method testDoReport.

/**
 * Method: doReport(SofaTracerSpan span)
 */
@Test
public void testDoReport() throws Exception {
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    // sr TL
    SofaTracerSpan sofaTracerServerSpan = this.remoteTracer.serverReceive();
    sofaTracerServerSpan.setOperationName("ServerReveive0");
    // assert
    assertEquals(sofaTracerServerSpan, sofaTraceContext.getCurrentSpan());
    // cs
    SofaTracerSpan clientSpan = this.remoteTracer.clientSend("ClientSend0");
    // assert
    assertEquals(clientSpan, sofaTraceContext.getCurrentSpan());
    // mock sync remote call
    this.mockRemoteCall();
    // cr
    this.remoteTracer.clientReceive("0");
    assertEquals(sofaTracerServerSpan, sofaTraceContext.getCurrentSpan());
    // ss TL
    this.remoteTracer.serverSend("0");
    // 异步汇报,所以 sleep 1s
    Thread.sleep(1000);
    // assert
    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 19 with SofaTracerSpan

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

the class SofaTracerMethodInvocationProcessor method proceedProxyMethodWithTracerAnnotation.

private Object proceedProxyMethodWithTracerAnnotation(MethodInvocation invocation, Tracer tracerSpan) throws Throwable {
    if (tracer instanceof FlexibleTracer) {
        try {
            String operationName = tracerSpan.operateName();
            if (StringUtils.isBlank(operationName)) {
                operationName = invocation.getMethod().getName();
            }
            SofaTracerSpan sofaTracerSpan = ((FlexibleTracer) tracer).beforeInvoke(operationName);
            sofaTracerSpan.setTag(CommonSpanTags.METHOD, invocation.getMethod().getName());
            if (invocation.getArguments() != null && invocation.getArguments().length != 0) {
                StringBuilder stringBuilder = new StringBuilder();
                for (Object obj : invocation.getArguments()) {
                    stringBuilder.append(obj.getClass().getName()).append(";");
                }
                sofaTracerSpan.setTag("param.types", stringBuilder.toString().substring(0, stringBuilder.length() - 1));
            }
            Object result = invocation.proceed();
            ((FlexibleTracer) tracer).afterInvoke();
            return result;
        } catch (Throwable t) {
            ((FlexibleTracer) tracer).afterInvoke(t.getMessage());
            throw t;
        }
    } else {
        return invocation.proceed();
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) FlexibleTracer(com.alipay.sofa.tracer.plugin.flexible.FlexibleTracer)

Example 20 with SofaTracerSpan

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

the class SofaTracerMethodInvocationProcessor method proceedProxyMethodWithTracerAnnotation.

private Object proceedProxyMethodWithTracerAnnotation(MethodInvocation invocation, Tracer tracerSpan) throws Throwable {
    if (tracer instanceof FlexibleTracer) {
        try {
            String operationName = tracerSpan.operateName();
            if (StringUtils.isBlank(operationName)) {
                operationName = invocation.getMethod().getName();
            }
            SofaTracerSpan sofaTracerSpan = ((FlexibleTracer) tracer).beforeInvoke(operationName);
            sofaTracerSpan.setTag(CommonSpanTags.METHOD, invocation.getMethod().getName());
            if (invocation.getArguments() != null && invocation.getArguments().length != 0) {
                StringBuilder stringBuilder = new StringBuilder();
                for (Object obj : invocation.getArguments()) {
                    stringBuilder.append(obj.getClass().getName()).append(";");
                }
                sofaTracerSpan.setTag("param.types", stringBuilder.toString().substring(0, stringBuilder.length() - 1));
            }
            Object result = invocation.proceed();
            ((FlexibleTracer) tracer).afterInvoke();
            return result;
        } catch (Throwable t) {
            ((FlexibleTracer) tracer).afterInvoke(t.getMessage());
            throw t;
        }
    } else {
        return invocation.proceed();
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) FlexibleTracer(com.alipay.sofa.tracer.plugin.flexible.FlexibleTracer)

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