Search in sources :

Example 1 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.

the class SofaRpcMetrics method onEvent.

private void onEvent(ClientEndInvokeEvent event) {
    InvokeMeta meta = new InvokeMeta(event.getRequest(), event.getResponse(), getLongAvoidNull(RpcInternalContext.getContext().getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE)));
    RpcInternalContext context = RpcInternalContext.getContext();
    Duration elapsed = meta.elapsed();
    Tags tags = meta.tags(this.common);
    clientTotal.apply(tags).record(elapsed);
    if (!meta.success()) {
        clientFail.apply(tags).record(elapsed);
    }
    requestSize.apply(tags).record(getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE)));
    responseSize.apply(tags).record(getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE)));
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) Duration(java.time.Duration) Tags(io.micrometer.core.instrument.Tags)

Example 2 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext 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 3 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext 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 4 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.

the class TraceClientResponseFilter method filter.

@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
    if (RpcInternalContext.isAttachmentEnable()) {
        // 补充客户端response长度
        RpcInternalContext context = RpcInternalContext.getContext();
        context.setAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE, responseContext.getLength());
    }
    RestTracerAdapter.clientReceived(responseContext);
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext)

Example 5 with RpcInternalContext

use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.

the class RpcReferenceContextFilter method invoke.

@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    ConsumerConfig config = (ConsumerConfig) invoker.getConfig();
    RpcReferenceContext referenceCtx = new RpcReferenceContext();
    referenceCtx.setGeneric(config.isGeneric());
    referenceCtx.setInterfaceName(config.getInterfaceId());
    referenceCtx.setUniqueId(config.getUniqueId());
    referenceCtx.setServiceName(request.getTargetServiceUniqueName());
    referenceCtx.setMethodName(request.getMethodName());
    RpcInternalContext context = RpcInternalContext.getContext();
    ProviderInfo providerInfo = context.getProviderInfo();
    if (providerInfo != null) {
        referenceCtx.setTargetAppName(providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME));
        referenceCtx.setTargetUrl(providerInfo.getHost() + ":" + providerInfo.getPort());
    }
    referenceCtx.setProtocol(config.getProtocol());
    referenceCtx.setInvokeType(request.getInvokeType());
    referenceCtx.setRouteRecord((String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
    RpcInvokeContext.getContext().put(RemotingConstants.INVOKE_CTX_RPC_REF_CTX, referenceCtx);
    SofaResponse response = invoker.invoke(request);
    // 调用后
    InetSocketAddress local = context.getLocalAddress();
    if (local != null) {
        referenceCtx.setClientIP(NetUtils.toIpString(local));
        referenceCtx.setClientPort(local.getPort());
    }
    Long ct = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CONN_CREATE_TIME);
    if (ct != null) {
        referenceCtx.setConnEstablishedSpan(ct);
    }
    Integer qs = (Integer) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE);
    if (qs != null) {
        referenceCtx.setRequestSize(qs.longValue());
    }
    Integer ps = (Integer) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE);
    if (ps != null) {
        referenceCtx.setResponseSize(ps.longValue());
    }
    referenceCtx.setTraceId((String) context.getAttachment(RpcConstants.INTERNAL_KEY_TRACE_ID));
    referenceCtx.setRpcId((String) context.getAttachment(RpcConstants.INTERNAL_KEY_SPAN_ID));
    Long ce = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE);
    if (ce != null) {
        referenceCtx.setCostTime(ce);
    }
    return response;
}
Also used : ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) InetSocketAddress(java.net.InetSocketAddress) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) RpcReferenceContext(com.alipay.sofa.rpc.api.context.RpcReferenceContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Aggregations

RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)52 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)15 SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)13 SofaTracerSpan (com.alipay.common.tracer.core.span.SofaTracerSpan)12 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)12 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)10 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)6 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)5 HashMap (java.util.HashMap)5 InvokeContext (com.alipay.remoting.InvokeContext)4 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)4 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)4 ClientSyncReceiveEvent (com.alipay.sofa.rpc.event.ClientSyncReceiveEvent)4 Test (org.junit.Test)4 SofaTracerSpanContext (com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)3 ConnectionClosedException (com.alipay.remoting.exception.ConnectionClosedException)3 DeserializationException (com.alipay.remoting.exception.DeserializationException)3 RemotingException (com.alipay.remoting.exception.RemotingException)3 SerializationException (com.alipay.remoting.exception.SerializationException)3 InvokeSendFailedException (com.alipay.remoting.rpc.exception.InvokeSendFailedException)3