Search in sources :

Example 26 with PressureMeasureError

use of com.pamirs.pradar.exception.PressureMeasureError in project LinkAgent by shulieTech.

the class OrderlyTraceContextInterceptor method doBefore.

@Override
public void doBefore(Advice advice) throws Throwable {
    try {
        MessageQueue messageQueue = getMessageQueue(advice.getTarget());
        String consumeGroup = getConsumeGroup(advice.getTarget());
        ConsumeMessageContext consumeMessageContext = new ConsumeMessageContext();
        consumeMessageContext.setConsumerGroup(consumeGroup);
        consumeMessageContext.setMq(messageQueue);
        consumeMessageContext.setSuccess(false);
        OrderlyTraceContexts.set(consumeMessageContext);
    } catch (PradarException e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw e;
        }
    } catch (PressureMeasureError e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw e;
        }
    } catch (Throwable e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError(e);
        }
    }
}
Also used : ConsumeMessageContext(org.apache.rocketmq.client.hook.ConsumeMessageContext) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) PradarException(com.pamirs.pradar.exception.PradarException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError)

Example 27 with PressureMeasureError

use of com.pamirs.pradar.exception.PressureMeasureError in project LinkAgent by shulieTech.

the class OrderlyTraceContextInterceptor method doAfter.

@Override
public void doAfter(Advice advice) throws Throwable {
    try {
        ConsumeMessageContext consumeMessageContext = OrderlyTraceContexts.get();
        if (consumeMessageContext == null || consumeMessageContext.getMsgList() == null) {
            return;
        }
        consumeMessageContext.setSuccess(true);
        consumeMessageContext.setStatus("SUCCESS");
        // 兜底,以免after没有执行(consumeMessageContext.getMsgList() != null 说明before 已经执行了)
        PushConsumeMessageHookImpl.getInstance().consumeMessageAfter(consumeMessageContext);
    } catch (PradarException e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw e;
        }
    } catch (PressureMeasureError e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw e;
        }
    } catch (Throwable e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError(e);
        }
    } finally {
        OrderlyTraceContexts.remove();
    }
}
Also used : ConsumeMessageContext(org.apache.rocketmq.client.hook.ConsumeMessageContext) PradarException(com.pamirs.pradar.exception.PradarException) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError)

Example 28 with PressureMeasureError

use of com.pamirs.pradar.exception.PressureMeasureError in project LinkAgent by shulieTech.

the class AsyncHttpClientv5MethodInterceptor method doBefore.

@Override
public void doBefore(final Advice advice) throws ProcessControlException {
    Object[] args = advice.getParameterArray();
    // HttpHost
    HttpHost httpHost = (HttpHost) args[0];
    if (httpHost == null) {
        return;
    }
    BasicRequestProducer requestBasic = (BasicRequestProducer) args[1];
    final SimpleHttpRequest request;
    try {
        Field field = requestBasic.getClass().getDeclaredField("request");
        field.setAccessible(true);
        request = (SimpleHttpRequest) field.get(requestBasic);
    } catch (Exception e) {
        logger.error("获取request参数错误", e);
        return;
    }
    if (request == null) {
        return;
    }
    String host = httpHost.getHostName();
    int port = httpHost.getPort();
    String path = httpHost.getHostName();
    String reqStr = request.toString();
    String method = StringUtils.upperCase(reqStr.substring(0, reqStr.indexOf(" ")));
    // 判断是否在白名单中
    String url = getService(httpHost.getSchemeName(), host, port, path);
    MatchConfig config = ClusterTestUtils.httpClusterTest(url);
    Header[] wHeaders = request.getHeaders(PradarService.PRADAR_WHITE_LIST_CHECK);
    if (wHeaders != null && wHeaders.length > 0) {
        config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, wHeaders[0].getValue());
    }
    config.addArgs("url", url);
    config.addArgs("request", request);
    config.addArgs("method", "uri");
    config.addArgs("isInterface", Boolean.FALSE);
    Pradar.startClientInvoke(path, method);
    Pradar.remoteIp(host);
    Pradar.remotePort(port);
    Pradar.middlewareName(HttpClientConstants.HTTP_CLIENT_NAME_5X);
    Header[] headers = request.getHeaders("content-length");
    if (headers != null && headers.length != 0) {
        try {
            Header header = headers[0];
            Pradar.requestSize(Integer.valueOf(header.getValue()));
        } catch (NumberFormatException e) {
        }
    }
    final Map<String, String> context = Pradar.getInvokeContextMap();
    for (Map.Entry<String, String> entry : context.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (request.getHeaders(HeaderMark.DONT_MODIFY_HEADER) == null || request.getHeaders(HeaderMark.DONT_MODIFY_HEADER).length == 0) {
            request.setHeader(key, value);
        }
    }
    // pradar启动提前为的是捕获白名单异常能trace进去的
    try {
        config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionCall() {

            @Override
            public Object call(Object param) {
                // return future;
                return null;
            }
        });
    } catch (PressureMeasureError e) {
        Pradar.response(e);
        Pradar.endClientInvoke(ResultCode.INVOKE_RESULT_FAILED, HttpClientConstants.PLUGIN_TYPE);
        throw e;
    }
    Pradar.popInvokeContext();
    final Object future = args[args.length - 1];
    if (!(future instanceof FutureCallback) && future != null) {
        return;
    }
    advice.changeParameter(args.length - 1, new FutureCallback() {

        @Override
        public void completed(Object result) {
            Pradar.setInvokeContext(context);
            ((FutureCallback) future).completed(result);
            try {
                if (result instanceof SimpleHttpResponse) {
                    afterTrace(request, (SimpleHttpResponse) result);
                } else {
                    afterTrace(request, null);
                }
            } catch (Throwable e) {
                LOGGER.error("AsyncHttpClient execute future endTrace error.", e);
                Pradar.endClientInvoke("200", HttpClientConstants.PLUGIN_TYPE);
            }
        }

        @Override
        public void failed(Exception ex) {
            Pradar.setInvokeContext(context);
            ((FutureCallback) future).failed(ex);
            try {
                exceptionTrace(request, ex);
            } catch (Throwable e) {
                LOGGER.error("AsyncHttpClient execute future endTrace error.", e);
                Pradar.endClientInvoke("200", HttpClientConstants.PLUGIN_TYPE);
            }
        }

        @Override
        public void cancelled() {
            Pradar.setInvokeContext(context);
            ((FutureCallback) future).cancelled();
            try {
                exceptionTrace(request, null);
            } catch (Throwable e) {
                LOGGER.error("AsyncHttpClient execute future endTrace error.", e);
                Pradar.endClientInvoke("200", HttpClientConstants.PLUGIN_TYPE);
            }
        }
    });
}
Also used : BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) SocketTimeoutException(java.net.SocketTimeoutException) ProcessControlException(com.shulie.instrument.simulator.api.ProcessControlException) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) Field(java.lang.reflect.Field) Header(org.apache.hc.core5.http.Header) HttpHost(org.apache.hc.core5.http.HttpHost) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) JSONObject(com.alibaba.fastjson.JSONObject) ExecutionCall(com.pamirs.pradar.internal.config.ExecutionCall) Map(java.util.Map) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback)

Example 29 with PressureMeasureError

use of com.pamirs.pradar.exception.PressureMeasureError in project LinkAgent by shulieTech.

the class HttpClientv3MethodInterceptor method beforeLast.

@Override
public void beforeLast(Advice advice) throws ProcessControlException {
    Object[] args = advice.getParameterArray();
    try {
        final HttpMethod method = (HttpMethod) args[1];
        if (method == null) {
            return;
        }
        int port = method.getURI().getPort();
        String path = method.getURI().getPath();
        String url = getService(method.getURI().getScheme(), method.getURI().getHost(), port, path);
        final MatchConfig config = ClusterTestUtils.httpClusterTest(url);
        Header header = method.getRequestHeader(PradarService.PRADAR_WHITE_LIST_CHECK);
        if (header == null) {
            config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, true);
        } else {
            config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, header.getValue());
        }
        config.addArgs("url", url);
        config.addArgs("isInterface", Boolean.FALSE);
        if (config.getStrategy() instanceof JsonMockStrategy) {
            config.addArgs("extraMethod", method);
            fixJsonStrategy.processBlock(java.lang.String.class, advice.getClassLoader(), config);
        }
        config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionForwardCall() {

            @Override
            public Object call(Object param) throws ProcessControlException {
                byte[] bytes = JSONObject.toJSONBytes(param);
                Reflect.on(method).set("responseBody", bytes);
                ProcessController.returnImmediately(int.class, 200);
                return true;
            }

            @Override
            public Object forward(Object param) throws ProcessControlException {
                String forwarding = config.getForwarding();
                try {
                    method.setURI(new URI(forwarding));
                } catch (URIException e) {
                }
                return null;
            }
        });
    } catch (URIException e) {
        LOGGER.error("", e);
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError(e);
        }
    } catch (ProcessControlException pce) {
        throw pce;
    } catch (Throwable t) {
        LOGGER.error("", t);
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError(t);
        }
    }
}
Also used : ProcessControlException(com.shulie.instrument.simulator.api.ProcessControlException) MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) URI(org.apache.commons.httpclient.URI) JsonMockStrategy(com.pamirs.pradar.pressurement.mock.JsonMockStrategy) ExecutionForwardCall(com.pamirs.pradar.internal.adapter.ExecutionForwardCall) URIException(org.apache.commons.httpclient.URIException) Header(org.apache.commons.httpclient.Header) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) JSONObject(com.alibaba.fastjson.JSONObject) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 30 with PressureMeasureError

use of com.pamirs.pradar.exception.PressureMeasureError in project LinkAgent by shulieTech.

the class HessianServiceExporterHandleRequestInterceptor method afterTrace.

@Override
public SpanRecord afterTrace(Advice advice) {
    Object[] args = advice.getParameterArray();
    Object target = advice.getTarget();
    if (args == null || args.length == 0) {
        return null;
    }
    if (!(args[0] instanceof HttpServletRequest)) {
        if (Pradar.isClusterTest()) {
            throw new PressureMeasureError("hessian servlet trace err! can't cast to HttpServletRequest");
        }
        return null;
    }
    WrapperRequest request = (WrapperRequest) args[0];
    if (!request.getMethod().equals("POST") && !request.getMethod().equals("post")) {
        return null;
    }
    String method = request.getHeader(HessianConstants.METHOD_HEADER);
    if (method == null) {
        method = Pradar.getMethod();
    }
    if (method != null && isSkip(method)) {
        return null;
    }
    SpanRecord spanRecord = new SpanRecord();
    return spanRecord;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SpanRecord(com.pamirs.pradar.interceptor.SpanRecord) PressureMeasureError(com.pamirs.pradar.exception.PressureMeasureError) WrapperRequest(com.pamirs.attach.plugin.hessian.common.WrapperRequest)

Aggregations

PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)150 PradarException (com.pamirs.pradar.exception.PradarException)34 DataSourceMeta (com.pamirs.pradar.pressurement.agent.shared.service.DataSourceMeta)14 Connection (java.sql.Connection)14 SQLException (java.sql.SQLException)13 ArrayList (java.util.ArrayList)13 Map (java.util.Map)11 MatchConfig (com.pamirs.pradar.internal.config.MatchConfig)8 ShadowDatabaseConfig (com.pamirs.pradar.internal.config.ShadowDatabaseConfig)8 HashMap (java.util.HashMap)8 MongoNamespace (com.mongodb.MongoNamespace)7 RequestIndexRename (com.pamirs.attach.plugin.es.common.RequestIndexRename)7 ProcessControlException (com.shulie.instrument.simulator.api.ProcessControlException)7 MQTraceContext (com.pamirs.attach.plugin.alibaba.rocketmq.common.MQTraceContext)6 SpanRecord (com.pamirs.pradar.interceptor.SpanRecord)6 ReflectException (com.shulie.instrument.simulator.api.reflect.ReflectException)6 List (java.util.List)6 ConsumeMessageContext (org.apache.rocketmq.client.hook.ConsumeMessageContext)6 ConsumeMessageContext (com.alibaba.rocketmq.client.hook.ConsumeMessageContext)5 WrapperRequest (com.pamirs.attach.plugin.hessian.common.WrapperRequest)5