Search in sources :

Example 6 with InvokeContext

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

the class ReverseTraceInterceptor method endClientInvoke.

/**
 * client invoke 结束
 *
 * @param advice
 * @throws Throwable
 */
private void endClientInvoke(Advice advice) throws Throwable {
    if (!Pradar.hasInvokeContext()) {
        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 != null) {
            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;
    }
}
Also used : InvokeContext(com.pamirs.pradar.InvokeContext)

Example 7 with InvokeContext

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

the class BeforeTraceInterceptor method startClientInvoke.

/**
 * 开始客户端调用
 *
 * @param advice
 */
private void startClientInvoke(Advice advice) {
    boolean traceEnabled = true;
    SpanRecord record = null;
    try {
        record = beforeTrace(advice);
        if (record == null) {
            traceEnabled = false;
            return;
        }
        Pradar.startClientInvoke(record.getService(), record.getMethod());
        advice.mark(BEFORE_TRACE_SUCCESS);
        InvokeContext invokeContext = Pradar.getInvokeContext();
        if (invokeContext == null) {
            return;
        }
        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.getCallbackMsg() != null) {
            invokeContext.setCallBackMsg(record.getCallbackMsg());
        }
        if (advice.getThrowable() != null) {
            invokeContext.setResponse(advice.getThrowable());
            Pradar.setError(true);
        }
        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) {
                if (Pradar.isClusterTest()) {
                    throw new PressureMeasureError(e);
                }
                LOGGER.error("AGENT: {} trace context transfer err, trace context may be lost.", getClass().getName(), e);
            }
            if (record != null && record.getContextInject() != null) {
                Map<String, String> ctx = Pradar.getInvokeContextMap();
                advice.attach(ctx);
                record.getContextInject().injectContext(ctx);
            }
        }
    }
}
Also used : InvokeContext(com.pamirs.pradar.InvokeContext) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) Map(java.util.Map)

Example 8 with InvokeContext

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

the class BeforeTraceInterceptor method endServerInvokeException.

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

Example 9 with InvokeContext

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

the class BeforeTraceInterceptor 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());
    }
    advice.mark(BEFORE_TRACE_SUCCESS);
    advice.setInvokeContext(invokeContext);
    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 (advice.getThrowable() != null) {
        invokeContext.setResponse(advice.getThrowable());
        Pradar.setError(true);
    }
    if (record.getMiddlewareName() == null) {
        invokeContext.setMiddlewareName(getPluginName());
    } else {
        invokeContext.setMiddlewareName(record.getMiddlewareName());
    }
    if (record != null && record.getContextInject() != null) {
        Map<String, String> ctx = Pradar.getInvokeContextMap();
        advice.attach(ctx);
        record.getContextInject().injectContext(ctx);
        if (popContextBefore()) {
            Pradar.popInvokeContext();
        }
    }
}
Also used : InvokeContext(com.pamirs.pradar.InvokeContext)

Example 10 with InvokeContext

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

the class BeforeTraceInterceptor method endClientInvokeException.

/**
 * 结束客户端异常调用
 *
 * @param advice
 * @throws Throwable
 */
private final void endClientInvokeException(Advice advice) throws Throwable {
    try {
        if (!advice.hasMark(BEFORE_TRACE_SUCCESS)) {
            LOGGER.debug("{} before trace not finished.", getClass().getName());
            return;
        }
        SpanRecord record = exceptionTrace(advice);
        if (record == null) {
            return;
        }
        InvokeContext invokeContext = Pradar.getInvokeContext();
        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());
        }
        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)

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