Search in sources :

Example 21 with InvokeContext

use of com.pamirs.pradar.InvokeContext in project LinkAgent by shulieTech.

the class TraceInterceptor method endClientInvokeException.

/**
 * 结束客户端异常调用
 *
 * @param advice
 * @throws Throwable
 */
private final void endClientInvokeException(Advice advice) throws Throwable {
    try {
        if (!advice.hasMark(BEFORE_TRACE_SUCCESS)) {
            if (isDebugEnabled) {
                LOGGER.debug("{} before trace not finished.", getClass().getName());
            }
            return;
        }
        SpanRecord record = exceptionTrace(advice);
        if (record == null) {
            endClientInvoke(ResultCode.INVOKE_RESULT_SUCCESS, getPluginType(), advice);
            return;
        }
        InvokeContext invokeContext = readCurrentInvokeContext(advice);
        if (invokeContext == null) {
            return;
        }
        Object response = record.getResponse();
        if (response != null && response instanceof Throwable) {
            advice.attach(response);
        }
        if (Pradar.isResponseOn()) {
            invokeContext.setResponse(response);
        }
        if (StringUtils.isNotBlank(record.getRemoteIp())) {
            invokeContext.setRemoteIp(record.getRemoteIp());
        }
        if (StringUtils.isNotBlank(record.getPort())) {
            invokeContext.setPort(record.getPort());
        }
        if (record.getMiddlewareName() != null) {
            invokeContext.setMiddlewareName(record.getMiddlewareName());
        }
        if (record.getCallbackMsg() != null) {
            invokeContext.setCallBackMsg(record.getCallbackMsg());
        }
        endClientInvoke(record.getResultCode(), getPluginType(), advice);
    } catch (Throwable e) {
        endClientInvoke(ResultCode.INVOKE_RESULT_UNKNOWN, getPluginType(), advice);
        throw e;
    } finally {
        advice.unMark(BEFORE_TRACE_SUCCESS);
    }
}
Also used : InvokeContext(com.pamirs.pradar.InvokeContext)

Example 22 with InvokeContext

use of com.pamirs.pradar.InvokeContext in project LinkAgent by shulieTech.

the class TraceInterceptor method endClientInvoke.

/**
 * client invoke 结束
 *
 * @param advice
 * @throws Throwable
 */
private void endClientInvoke(Advice advice) throws Throwable {
    try {
        if (!advice.hasMark(BEFORE_TRACE_SUCCESS)) {
            if (isDebugEnabled) {
                LOGGER.debug("{} before trace not finished.", getClass().getName());
            }
            return;
        }
        SpanRecord record = afterTrace(advice);
        if (record == null) {
            // 如果上下文开始了,但是这里没有,则需要强制结束
            endClientInvoke(ResultCode.INVOKE_RESULT_SUCCESS, getPluginType(), advice);
            return;
        }
        InvokeContext invokeContext = readCurrentInvokeContext(advice);
        if (invokeContext == null) {
            return;
        }
        if (record.getResponseSize() != 0) {
            invokeContext.setResponseSize(record.getResponseSize());
        }
        Object response = record.getResponse();
        if (response instanceof Throwable) {
            advice.attach(response);
        }
        if (Pradar.isResponseOn()) {
            invokeContext.setResponse(response);
        }
        if (StringUtils.isNotBlank(record.getRemoteIp())) {
            invokeContext.setRemoteIp(record.getRemoteIp());
        }
        if (StringUtils.isNotBlank(record.getPort())) {
            invokeContext.setPort(record.getPort());
        }
        if (record.getMiddlewareName() != null) {
            invokeContext.setMiddlewareName(record.getMiddlewareName());
        }
        if (record.getCallbackMsg() != null) {
            invokeContext.setCallBackMsg(record.getCallbackMsg());
        }
        endClientInvoke(record.getResultCode(), getPluginType(), advice);
    } catch (Throwable e) {
        /**
         * 如果出错了,则强制将上下文提交
         */
        endClientInvoke(ResultCode.INVOKE_RESULT_UNKNOWN, getPluginType(), advice);
        throw e;
    } finally {
        advice.unMark(BEFORE_TRACE_SUCCESS);
    }
}
Also used : InvokeContext(com.pamirs.pradar.InvokeContext)

Aggregations

InvokeContext (com.pamirs.pradar.InvokeContext)22 Map (java.util.Map)3 ServerWebExchange (org.springframework.web.server.ServerWebExchange)2 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)1 ContextTransfer (com.pamirs.pradar.interceptor.ContextTransfer)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 HttpMessage (io.netty.handler.codec.http.HttpMessage)1 NettyRoutingFilter (org.springframework.cloud.gateway.filter.NettyRoutingFilter)1 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)1