Search in sources :

Example 1 with SofaTraceContext

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

the class RestTracerAdapter method serverSend.

/**
 * 适配服务端serverSend
 */
public static void serverSend(NettyHttpResponse response, Throwable throwable) {
    try {
        SofaRequest sofaRequest = new SofaRequest();
        SofaResponse sofaResponse = new SofaResponse();
        if (response == null) {
            sofaResponse.setErrorMsg("rest path ends with /favicon.ico");
        } else if (throwable != null) {
            if (response.getStatus() == 500) {
                sofaResponse.setAppResponse(throwable);
            } else {
                sofaResponse.setErrorMsg(throwable.getMessage());
            }
            Object method = RpcInternalContext.getContext().getAttachment(METHOD_TYPE_STRING);
            if (method != null) {
                Class[] parameterTypes = ((Method) method).getParameterTypes();
                String[] methodTypeString = new String[parameterTypes.length];
                for (int i = 0; i < methodTypeString.length; i++) {
                    methodTypeString[i] = (parameterTypes[i].getName());
                }
                sofaRequest.setMethodArgSigs(methodTypeString);
            }
        }
        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
        RpcInternalContext context = RpcInternalContext.getContext();
        if (serverSpan != null) {
            serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
        }
        RestBaggageItemsHandler.encodeBaggageItemsToResponse(response, sofaResponse);
        Tracers.serverSend(sofaRequest, sofaResponse, null);
    } catch (Throwable t) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("the process of rest tracer server send occur error ", t);
        }
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 2 with SofaTraceContext

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

the class RestTracerAdapter method beforeSend.

/**
 * 存入tracer信息
 *
 * @param requestContext ClientRequestContext
 */
public static void beforeSend(ClientRequestContext requestContext) {
    // tracer信息放入request 发到服务端
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
    RpcInternalContext context = RpcInternalContext.getContext();
    if (clientSpan != null) {
        requestContext.getHeaders().add(RemotingConstants.NEW_RPC_TRACE_NAME, clientSpan.getSofaTracerSpanContext().serializeSpanContext());
    }
    // 客户端发送自己的应用名
    String appName = (String) context.getAttachment(INTERNAL_KEY_APP_NAME);
    if (appName != null) {
        requestContext.getHeaders().add(RemotingConstants.HEAD_APP_NAME, appName);
    }
    RestBaggageItemsHandler.encodeBaggageItemToRequest(requestContext.getHeaders());
}
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 3 with SofaTraceContext

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

the class TripleTracerAdapter method serverReceived.

/**
 * 适配服务端serverReceived
 */
public static void serverReceived(SofaRequest sofaRequest, ServerServiceDefinition serverServiceDefinition, final ServerCall call, Metadata requestHeaders) {
    try {
        if (sofaRequest == null) {
            sofaRequest = new SofaRequest();
        }
        Map<String, String> traceMap = new HashMap<String, String>();
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_TARGET_SERVICE)) {
            sofaRequest.setTargetServiceUniqueName(requestHeaders.get(TripleHeadKeys.HEAD_KEY_TARGET_SERVICE));
            sofaRequest.setInterfaceName(requestHeaders.get(TripleHeadKeys.HEAD_KEY_TARGET_SERVICE));
        } else {
            String serviceName = serverServiceDefinition.getServiceDescriptor().getName();
            sofaRequest.setTargetServiceUniqueName(serviceName);
            sofaRequest.setInterfaceName(serviceName);
        }
        final String serviceName = call.getMethodDescriptor().getServiceName();
        sofaRequest.setTargetServiceUniqueName(serviceName);
        sofaRequest.setInterfaceName(serviceName);
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_TARGET_APP)) {
            sofaRequest.setTargetAppName(requestHeaders.get(TripleHeadKeys.HEAD_KEY_TARGET_APP));
        }
        // 先取兼容的
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_OLD_TRACE_ID)) {
            traceMap.put(TracerCompatibleConstants.TRACE_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_OLD_TRACE_ID));
        } else if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_TRACE_ID)) {
            traceMap.put(TracerCompatibleConstants.TRACE_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_TRACE_ID));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_OLD_RPC_ID)) {
            traceMap.put(TracerCompatibleConstants.RPC_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_OLD_RPC_ID));
        } else if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_RPC_ID)) {
            traceMap.put(TracerCompatibleConstants.RPC_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_RPC_ID));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION)) {
            RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, requestHeaders.get(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION));
        } else {
            RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, "");
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SAMP_TYPE)) {
            traceMap.put(TracerCompatibleConstants.SAMPLING_MARK, requestHeaders.get(TripleHeadKeys.HEAD_KEY_SAMP_TYPE));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_CURRENT_APP)) {
            sofaRequest.addRequestProp(HEAD_APP_NAME, requestHeaders.get(TripleHeadKeys.HEAD_KEY_CURRENT_APP));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_PROTOCOL_TYPE)) {
            sofaRequest.addRequestProp(RemotingConstants.HEAD_PROTOCOL, requestHeaders.get(TripleHeadKeys.HEAD_KEY_PROTOCOL_TYPE));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_INVOKE_TYPE)) {
            sofaRequest.addRequestProp(RemotingConstants.HEAD_INVOKE_TYPE, requestHeaders.get(TripleHeadKeys.HEAD_KEY_INVOKE_TYPE));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_BIZ_BAGGAGE_TYPE)) {
            traceMap.put(TracerCompatibleConstants.PEN_ATTRS_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_BIZ_BAGGAGE_TYPE));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SYS_BAGGAGE_TYPE)) {
            traceMap.put(TracerCompatibleConstants.PEN_SYS_ATTRS_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_SYS_BAGGAGE_TYPE));
        }
        if (!traceMap.isEmpty()) {
            sofaRequest.addRequestProp(RemotingConstants.RPC_TRACE_NAME, traceMap);
        }
        if (EventBus.isEnable(ServerReceiveEvent.class)) {
            EventBus.post(new ServerReceiveEvent(sofaRequest));
        }
        String remoteIp = "";
        SocketAddress socketAddress = call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
        if (socketAddress instanceof InetSocketAddress) {
            remoteIp = ((InetSocketAddress) socketAddress).getHostName();
        }
        String methodName;
        String fullMethodName = call.getMethodDescriptor().getFullMethodName();
        methodName = StringUtils.substringAfter(fullMethodName, serviceName + "/");
        sofaRequest.setMethodName(methodName);
        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
        if (serverSpan != null) {
            // FIXME modify the dep relation
            serverSpan.setTag("service", sofaRequest.getTargetServiceUniqueName());
            serverSpan.setTag("method", methodName);
            // 从请求里获取ConsumerTracerFilter额外传递的信息
            serverSpan.setTag("remote.app", (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
            serverSpan.setTag("protocol", RpcConstants.PROTOCOL_TYPE_TRIPLE);
            serverSpan.setTag("remote.ip", remoteIp);
        }
    } catch (Throwable e) {
        LOGGER.warn("triple serverReceived tracer error", e);
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) HashMap(java.util.HashMap) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) InetSocketAddress(java.net.InetSocketAddress) ServerReceiveEvent(com.alipay.sofa.rpc.event.ServerReceiveEvent) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 4 with SofaTraceContext

use of com.alipay.common.tracer.core.context.trace.SofaTraceContext 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 5 with SofaTraceContext

use of com.alipay.common.tracer.core.context.trace.SofaTraceContext 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)

Aggregations

SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)26 SofaTracerSpan (com.alipay.common.tracer.core.span.SofaTracerSpan)25 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)13 Test (org.junit.Test)7 SofaTracerSpanContext (com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)6 HashMap (java.util.HashMap)6 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)4 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)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 InetSocketAddress (java.net.InetSocketAddress)2 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 RpcRuntimeContext (com.alipay.sofa.rpc.context.RpcRuntimeContext)1 ServerReceiveEvent (com.alipay.sofa.rpc.event.ServerReceiveEvent)1 SofaResourceFactory (com.alipay.sofa.rpc.server.rest.SofaResourceFactory)1 SofaResourceMethodInvoker (com.alipay.sofa.rpc.server.rest.SofaResourceMethodInvoker)1 Context (io.grpc.Context)1 ForwardingServerCall (io.grpc.ForwardingServerCall)1 ForwardingServerCallListener (io.grpc.ForwardingServerCallListener)1