Search in sources :

Example 1 with SofaResourceFactory

use of com.alipay.sofa.rpc.server.rest.SofaResourceFactory in project sofa-rpc by sofastack.

the class RestTracerAdapter method serverFilter.

/**
 * 适配服务端filter
 *
 * @param requestContext ContainerRequestContext
 */
public static void serverFilter(ContainerRequestContext requestContext) {
    try {
        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
        if (serverSpan != null) {
            RpcInternalContext context = RpcInternalContext.getContext();
            context.setAttachment(RpcConstants.INTERNAL_KEY_SERVER_RECEIVE_TIME, RpcRuntimeContext.now());
            SofaResourceMethodInvoker resourceMethodInvoker = (SofaResourceMethodInvoker) ((PostMatchContainerRequestContext) requestContext).getResourceMethod();
            SofaResourceFactory factory = resourceMethodInvoker.getResource();
            String serviceName = factory.getServiceName();
            String appName = factory.getAppName();
            if (serviceName == null) {
                serviceName = resourceMethodInvoker.getResourceClass().getName();
            }
            serverSpan.setTag(RpcSpanTags.SERVICE, serviceName);
            if (resourceMethodInvoker.getMethod() != null) {
                serverSpan.setTag(RpcSpanTags.METHOD, resourceMethodInvoker.getMethod().getName());
                // serverSend需要
                context.setAttachment(METHOD_TYPE_STRING, resourceMethodInvoker.getMethod());
            }
            // 客户端地址
            serverSpan.setTag(RpcSpanTags.REMOTE_IP, context.getRemoteHostName());
            String remoteAppName = requestContext.getHeaderString(RemotingConstants.HEAD_APP_NAME);
            if (StringUtils.isNotBlank(remoteAppName)) {
                serverSpan.setTag(RpcSpanTags.REMOTE_APP, remoteAppName);
            }
            serverSpan.setTag(RpcSpanTags.PROTOCOL, RpcConstants.PROTOCOL_TYPE_REST);
            serverSpan.setTag(RpcSpanTags.INVOKE_TYPE, RpcConstants.INVOKER_TYPE_SYNC);
            if (appName == null) {
                appName = (String) RpcRuntimeContext.get(RpcRuntimeContext.KEY_APPNAME);
            }
            serverSpan.setTag(RpcSpanTags.LOCAL_APP, appName);
        }
    } catch (Throwable t) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("the process of rest tracer server filter occur error ", t);
        }
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaResourceMethodInvoker(com.alipay.sofa.rpc.server.rest.SofaResourceMethodInvoker) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResourceFactory(com.alipay.sofa.rpc.server.rest.SofaResourceFactory)

Aggregations

SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)1 SofaTracerSpan (com.alipay.common.tracer.core.span.SofaTracerSpan)1 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)1 SofaResourceFactory (com.alipay.sofa.rpc.server.rest.SofaResourceFactory)1 SofaResourceMethodInvoker (com.alipay.sofa.rpc.server.rest.SofaResourceMethodInvoker)1