use of com.pamirs.pradar.InvokeContext in project LinkAgent by shulieTech.
the class AfterTraceInterceptor method endClientInvokeException.
/**
* 结束客户端异常调用
*
* @param advice
* @throws Throwable
*/
private final void endClientInvokeException(Advice advice) throws Throwable {
if (!advice.hasMark(BEFORE_TRACE_SUCCESS)) {
LOGGER.debug("{} before trace not finished.", getClass().getName());
return;
}
try {
SpanRecord record = exceptionTrace(advice);
if (record == null) {
Pradar.endClientInvoke(ResultCode.INVOKE_RESULT_SUCCESS, 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.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);
}
}
use of com.pamirs.pradar.InvokeContext in project LinkAgent by shulieTech.
the class ReverseTraceInterceptor method endServerInvoke.
/**
* 结束服务端调用
*
* @param advice
* @throws Throwable
*/
private void endServerInvoke(Advice advice) throws Throwable {
if (!Pradar.hasInvokeContext()) {
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;
}
}
use of com.pamirs.pradar.InvokeContext in project LinkAgent by shulieTech.
the class FilteringWebHandlerHandleInterceptor method beforeFirst.
@Override
public void beforeFirst(Advice advice) {
final Object[] args = advice.getParameterArray();
ServerWebExchange arg = (ServerWebExchange) args[0];
ServerHttpRequest serverHttpRequest = arg.getRequest();
requestTracer.startTrace(serverHttpRequest, null, SpringCloudGatewayConstants.MODULE_NAME);
final InvokeContext invokeContext = Pradar.getInvokeContext();
arg.getAttributes().put(SpringCloudGatewayConstants.GATEWAY_CONTEXT, invokeContext);
}
use of com.pamirs.pradar.InvokeContext in project LinkAgent by shulieTech.
the class GatewayFilterChainFilterInterceptor method beforeFirst.
@Override
public void beforeFirst(Advice advice) {
final NettyRoutingFilter target = (NettyRoutingFilter) advice.getTarget();
final Object[] parameterArray = advice.getParameterArray();
ServerWebExchange exchange = (ServerWebExchange) parameterArray[0];
final InvokeContext context = exchange.getAttribute(SpringCloudGatewayConstants.GATEWAY_CONTEXT);
if (context == null || context.isEmpty()) {
return;
}
Pradar.setInvokeContext(context);
if (!target.getHeadersFilters().contains(requestHttpHeadersFilter)) {
target.getHeadersFilters().add(requestHttpHeadersFilter);
}
if (!target.getHeadersFilters().contains(responseHttpHeadersFilter)) {
target.getHeadersFilters().add(responseHttpHeadersFilter);
}
}
use of com.pamirs.pradar.InvokeContext in project LinkAgent by shulieTech.
the class TraceInterceptor 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 = readCurrentInvokeContext(advice);
if (invokeContext == null) {
return;
}
advice.mark(BEFORE_TRACE_SUCCESS);
advice.setInvokeContext(invokeContext);
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());
}
invokeContext.setPassCheck(record.isPassedCheck());
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);
}
}
}
}
Aggregations