Search in sources :

Example 1 with InvokeContext

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

the class HttpEncoderInterceptor method getContextTransfer.

@Override
protected ContextTransfer getContextTransfer(Advice advice) {
    Object[] args = advice.getParameterArray();
    final HttpMessage httpMessage = (HttpMessage) args[1];
    Map<String, String> context = manager.getDynamicField(httpMessage, NettyConstants.DYNAMIC_FIELD_ASYNC_CONTEXT);
    InvokeContext invokeContext_ = Pradar.fromMap(context);
    if (Pradar.hasInvokeContext(context) || Pradar.hasInvokeContext()) {
        return new ContextTransfer() {

            @Override
            public void transfer(String key, String value) {
                HttpHeaders headers = httpMessage.headers();
                if (headers != null && !headers.contains(key)) {
                    headers.set(key, value);
                }
            }
        };
    }
    return null;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) InvokeContext(com.pamirs.pradar.InvokeContext) ContextTransfer(com.pamirs.pradar.interceptor.ContextTransfer) HttpMessage(io.netty.handler.codec.http.HttpMessage)

Example 2 with InvokeContext

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

the class TraceInterceptor method endServerInvokeException.

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

Example 3 with InvokeContext

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

the class TraceInterceptor 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 = readCurrentInvokeContext(advice);
    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 (record.getMiddlewareName() == null) {
        invokeContext.setMiddlewareName(getPluginName());
    } else {
        invokeContext.setMiddlewareName(record.getMiddlewareName());
    }
}
Also used : InvokeContext(com.pamirs.pradar.InvokeContext)

Example 4 with InvokeContext

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

the class TraceInterceptor method endServerInvoke.

/**
 * 结束服务端调用
 *
 * @param advice
 * @throws Throwable
 */
private void endServerInvoke(Advice advice) throws Throwable {
    if (!advice.hasMark(BEFORE_TRACE_SUCCESS)) {
        if (isDebugEnabled) {
            LOGGER.debug("{} before trace not finished.", getClass().getName());
        }
        return;
    }
    boolean isTrace = isTrace0(advice);
    try {
        SpanRecord record = afterTrace(advice);
        if (record == null) {
            if (!Pradar.hasInvokeContext() || readCurrentInvokeContext(advice).isEmpty()) {
                return;
            }
            // 如果上下文开始了,但是这里没有,则需要强制结束
            if (isTrace) {
                endTrace(ResultCode.INVOKE_RESULT_UNKNOWN, getPluginType(), advice);
            } else {
                endServerInvoke(ResultCode.INVOKE_RESULT_UNKNOWN, 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());
        }
        if (isTrace) {
            endTrace(record.getResultCode(), getPluginType(), advice);
        } else {
            endServerInvoke(record.getResultCode(), getPluginType(), advice);
        }
    } catch (Throwable e) {
        if (isTrace) {
            endTrace(ResultCode.INVOKE_RESULT_UNKNOWN, getPluginType(), advice);
        } else {
            endServerInvoke(ResultCode.INVOKE_RESULT_UNKNOWN, getPluginType(), advice);
        }
        throw e;
    } finally {
        advice.unMark(BEFORE_TRACE_SUCCESS);
    }
}
Also used : InvokeContext(com.pamirs.pradar.InvokeContext)

Example 5 with InvokeContext

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

the class TraceInterceptor method endInvoke.

private void endInvoke(String resultCode, int type, Advice advice, EndCall endCall) {
    InvokeContext invokeContext = Pradar.getInvokeContext();
    InvokeContext adviceInvokeContext = (InvokeContext) advice.getInvokeContext();
    if (invokeContext != adviceInvokeContext) {
        LOGGER.error("invokeContext is not same, thread: {}, class : {},  \n\rthread context: {}, \n\radvice context: {}", Thread.currentThread(), this.getClass(), JSON.toJSONString(invokeContext), JSON.toJSONString(adviceInvokeContext));
        try {
            Pradar.setInvokeContext(adviceInvokeContext);
            endCall.call(resultCode, type);
        } finally {
            Pradar.setInvokeContext(invokeContext);
        }
    } else {
        endCall.call(resultCode, type);
    }
}
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