Search in sources :

Example 11 with InvokeContext

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

the class AfterTraceInterceptor method endServerInvokeException.

private final void endServerInvokeException(Advice advice) throws Throwable {
    if (!advice.hasMark(BEFORE_TRACE_SUCCESS)) {
        LOGGER.debug("{} before trace not finished.", getClass().getName());
        return;
    }
    boolean isTrace = isTrace0(advice);
    try {
        SpanRecord record = exceptionTrace(advice);
        if (record == null) {
            if (isTrace) {
                Pradar.endTrace(ResultCode.INVOKE_RESULT_UNKNOWN, getPluginType());
            } else {
                Pradar.endServerInvoke(ResultCode.INVOKE_RESULT_UNKNOWN, getPluginType());
            }
            return;
        }
        InvokeContext invokeContext = Pradar.getInvokeContext();
        if (invokeContext == null) {
            return;
        }
        Object response = record.getResponse();
        if (response != null && response instanceof Throwable) {
            advice.attach(response);
        }
        if (Pradar.isExceptionOn()) {
            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());
        }
        if (isTrace) {
            Pradar.endTrace(record.getResultCode(), getPluginType());
        } else {
            Pradar.endServerInvoke(record.getResultCode(), getPluginType());
        }
    } catch (Throwable e) {
        if (isTrace) {
            Pradar.endTrace(ResultCode.INVOKE_RESULT_UNKNOWN, getPluginType());
        } else {
            Pradar.endServerInvoke(ResultCode.INVOKE_RESULT_UNKNOWN, getPluginType());
        }
        throw e;
    } finally {
        advice.unMark(BEFORE_TRACE_SUCCESS);
    }
}
Also used : InvokeContext(com.pamirs.pradar.InvokeContext)

Example 12 with InvokeContext

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

the class AfterTraceInterceptor method endServerInvoke.

/**
 * 结束服务端调用
 *
 * @param advice
 * @throws Throwable
 */
private void endServerInvoke(Advice advice) throws Throwable {
    if (!advice.hasMark(BEFORE_TRACE_SUCCESS)) {
        LOGGER.debug("{} before trace not finished.", getClass().getName());
        return;
    }
    boolean isTrace = isTrace0(advice);
    try {
        SpanRecord record = afterTrace(advice);
        if (record == null) {
            if (isTrace) {
                Pradar.endTrace(ResultCode.INVOKE_RESULT_SUCCESS, getPluginType());
            } else {
                Pradar.endServerInvoke(ResultCode.INVOKE_RESULT_SUCCESS, getPluginType());
            }
            return;
        }
        InvokeContext invokeContext = Pradar.getInvokeContext();
        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());
        }
        if (isTrace) {
            Pradar.endTrace(record.getResultCode(), getPluginType());
        } else {
            Pradar.endServerInvoke(record.getResultCode(), getPluginType());
        }
    } catch (Throwable e) {
        if (isTrace) {
            Pradar.endTrace(ResultCode.INVOKE_RESULT_UNKNOWN, getPluginType());
        } else {
            Pradar.endServerInvoke(ResultCode.INVOKE_RESULT_UNKNOWN, getPluginType());
        }
        throw e;
    } finally {
        advice.unMark(BEFORE_TRACE_SUCCESS);
    }
}
Also used : InvokeContext(com.pamirs.pradar.InvokeContext)

Example 13 with InvokeContext

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

the class AfterTraceInterceptor method endClientInvoke.

/**
 * client invoke 结束
 *
 * @param advice
 * @throws Throwable
 */
private void endClientInvoke(Advice advice) throws Throwable {
    if (!advice.hasMark(BEFORE_TRACE_SUCCESS)) {
        LOGGER.debug("{} before trace not finished.", getClass().getName());
        return;
    }
    try {
        SpanRecord record = afterTrace(advice);
        if (record == null) {
            Pradar.endClientInvoke(ResultCode.INVOKE_RESULT_SUCCESS, getPluginType());
            return;
        }
        InvokeContext invokeContext = Pradar.getInvokeContext();
        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());
        }
        Pradar.endClientInvoke(record.getResultCode(), getPluginType());
    } catch (Throwable e) {
        /**
         * 如果出错了,则强制将上下文提交
         */
        Pradar.endClientInvoke(ResultCode.INVOKE_RESULT_UNKNOWN, getPluginType());
        throw e;
    } finally {
        advice.unMark(BEFORE_TRACE_SUCCESS);
    }
}
Also used : InvokeContext(com.pamirs.pradar.InvokeContext)

Example 14 with InvokeContext

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

the class ReverseTraceInterceptor method startServerInvoke.

/**
 * 开始服务端调用
 *
 * @param advice
 */
private void startServerInvoke(Advice advice) {
    SpanRecord record = beforeTrace(advice);
    if (record == null) {
        return;
    }
    if (record.getContext() != null) {
        if (record.getContext() == SpanRecord.CLEAN_CONTEXT) {
            Pradar.clearInvokeContext();
        }
    }
    if (isTrace0(advice)) {
        String traceId = TraceIdGenerator.generate(record.getRemoteIp(), Pradar.isClusterTest());
        Pradar.clearInvokeContext();
        Pradar.startTrace(traceId, record.getService(), record.getMethod());
    } else {
        Pradar.startServerInvoke(record.getService(), record.getMethod(), null, record.getContext());
    }
    InvokeContext invokeContext = Pradar.getInvokeContext();
    if (invokeContext == null) {
        return;
    }
    if (record.getClusterTest() != null) {
        invokeContext.setClusterTest(record.getClusterTest());
    }
    if (Pradar.isRequestOn()) {
        invokeContext.setRequest(record.getRequest());
    }
    if (Pradar.isResponseOn()) {
        if (record.getResponse() != null) {
            invokeContext.setResponse(record.getResponse());
        }
    }
    if (record.getRequestSize() != 0) {
        invokeContext.setRequestSize(record.getRequestSize());
    }
    if (StringUtils.isNotBlank(record.getRemoteIp())) {
        invokeContext.setRemoteIp(record.getRemoteIp());
    }
    if (StringUtils.isNotBlank(record.getPort())) {
        invokeContext.setPort(record.getPort());
    }
    if (record.getCallbackMsg() != null) {
        invokeContext.setCallBackMsg(record.getCallbackMsg());
    }
    if (record.getMiddlewareName() == null) {
        invokeContext.setMiddlewareName(getPluginName());
    } else {
        invokeContext.setMiddlewareName(record.getMiddlewareName());
    }
}
Also used : InvokeContext(com.pamirs.pradar.InvokeContext)

Example 15 with InvokeContext

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

the class ReverseTraceInterceptor method startClientInvoke.

/**
 * 开始客户端调用
 *
 * @param advice
 */
private void startClientInvoke(Advice advice) {
    boolean traceEnabled = true;
    try {
        SpanRecord record = beforeTrace(advice);
        if (record == null) {
            traceEnabled = false;
            return;
        }
        Pradar.startClientInvoke(record.getService(), record.getMethod());
        InvokeContext invokeContext = Pradar.getInvokeContext();
        if (record.getRequestSize() != 0) {
            invokeContext.setRequestSize(record.getRequestSize());
        }
        if (Pradar.isRequestOn()) {
            invokeContext.setRequest(record.getRequest());
        }
        if (StringUtils.isNotBlank(record.getRemoteIp())) {
            invokeContext.setRemoteIp(record.getRemoteIp());
        }
        if (StringUtils.isNotBlank(record.getPort())) {
            invokeContext.setPort(record.getPort());
        }
        if (record.getResponse() != null) {
            invokeContext.setResponse(record.getResponse());
        }
        if (record.getCallbackMsg() != null) {
            invokeContext.setCallBackMsg(record.getCallbackMsg());
        }
        if (record.getMiddlewareName() == null) {
            invokeContext.setMiddlewareName(getPluginName());
        } else {
            invokeContext.setMiddlewareName(record.getMiddlewareName());
        }
    } finally {
        if (traceEnabled) {
            try {
                ContextTransfer contextTransfer = getContextTransfer(advice);
                if (contextTransfer != null) {
                    Map<String, String> contextMap = Pradar.getInvokeContextTransformMap();
                    for (Map.Entry<String, String> entry : contextMap.entrySet()) {
                        contextTransfer.transfer(entry.getKey(), entry.getValue());
                    }
                }
            } catch (Throwable e) {
                LOGGER.error("AGENT: {} trace context transfer err, trace context may be lost.", getClass().getName(), e);
            }
        }
    }
}
Also used : InvokeContext(com.pamirs.pradar.InvokeContext) Map(java.util.Map)

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