Search in sources :

Example 11 with MatchConfig

use of com.pamirs.pradar.internal.config.MatchConfig in project LinkAgent by shulieTech.

the class HttpURLConnectionGetInputStreamInterceptor method beforeLast.

@Override
public void beforeLast(Advice advice) throws ProcessControlException {
    if (!Pradar.isClusterTest()) {
        return;
    }
    Object target = advice.getTarget();
    final HttpURLConnection request = (HttpURLConnection) target;
    final URL url = request.getURL();
    String fullPath = getService(url.getProtocol(), url.getHost(), url.getPort(), url.getPath());
    String whiteList = request.getRequestProperty(PradarService.PRADAR_WHITE_LIST_CHECK);
    MatchConfig config = ClusterTestUtils.httpClusterTest(fullPath);
    ExecutionStrategy strategy = config.getStrategy();
    // 仅mock在getInputStream里执行
    if (!(strategy instanceof JsonMockStrategy) && !(strategy instanceof MockStrategy)) {
        return;
    }
    config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, whiteList);
    // 白名单需要的信息
    config.addArgs("url", fullPath);
    // mock转发需要信息
    config.addArgs("request", request);
    config.addArgs("method", "url");
    config.addArgs("isInterface", Boolean.FALSE);
    config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config);
}
Also used : JsonMockStrategy(com.pamirs.pradar.pressurement.mock.JsonMockStrategy) HttpURLConnection(java.net.HttpURLConnection) MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) ExecutionStrategy(com.pamirs.pradar.internal.adapter.ExecutionStrategy) URL(java.net.URL) MockStrategy(com.pamirs.pradar.pressurement.mock.MockStrategy) JsonMockStrategy(com.pamirs.pradar.pressurement.mock.JsonMockStrategy)

Example 12 with MatchConfig

use of com.pamirs.pradar.internal.config.MatchConfig in project LinkAgent by shulieTech.

the class HttpURLConnectionInterceptor method beforeLast.

@Override
public void beforeLast(Advice advice) throws ProcessControlException {
    if (!Pradar.isClusterTest()) {
        return;
    }
    /**
     * 保持trace一致
     */
    boolean connecting = advice.hasMark(MARK_CONNECTING);
    boolean connected = advice.hasMark(MARK_CONNECTED);
    if (connected || connecting) {
        return;
    }
    Object target = advice.getTarget();
    final HttpURLConnection request = (HttpURLConnection) target;
    final URL url = request.getURL();
    String fullPath = getService(url.getProtocol(), url.getHost(), url.getPort(), url.getPath());
    String whiteList = request.getRequestProperty(PradarService.PRADAR_WHITE_LIST_CHECK);
    MatchConfig config = ClusterTestUtils.httpClusterTest(fullPath);
    ExecutionStrategy strategy = config.getStrategy();
    // mock不在connect里执行
    if (strategy instanceof JsonMockStrategy || strategy instanceof MockStrategy) {
        return;
    }
    config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, whiteList);
    // 白名单需要的信息
    config.addArgs("url", fullPath);
    // mock转发需要信息
    config.addArgs("request", request);
    config.addArgs("method", "url");
    config.addArgs("isInterface", Boolean.FALSE);
    config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config);
}
Also used : JsonMockStrategy(com.pamirs.pradar.pressurement.mock.JsonMockStrategy) HttpURLConnection(java.net.HttpURLConnection) MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) ExecutionStrategy(com.pamirs.pradar.internal.adapter.ExecutionStrategy) URL(java.net.URL) JsonMockStrategy(com.pamirs.pradar.pressurement.mock.JsonMockStrategy) MockStrategy(com.pamirs.pradar.pressurement.mock.MockStrategy)

Example 13 with MatchConfig

use of com.pamirs.pradar.internal.config.MatchConfig in project LinkAgent by shulieTech.

the class ClientInterceptor method beforeFirst.

@Override
public void beforeFirst(Advice advice) throws ProcessControlException, MalformedURLException {
    Object[] args = advice.getParameterArray();
    if (!(args[0] instanceof ClientRequest)) {
        return;
    }
    final ClientRequest clientRequest = (ClientRequest) args[0];
    final String url = clientRequest.getUri().toURL().toString();
    final MatchConfig config = ClusterTestUtils.httpClusterTest(url);
    config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, clientRequest.getHeaderString(PradarService.PRADAR_WHITE_LIST_CHECK));
    config.addArgs("url", url);
    config.addArgs("isInterface", Boolean.FALSE);
    config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionForwardCall() {

        @Override
        public Object forward(Object param) {
            clientRequest.setUri(URI.create(config.getForwarding()));
            return null;
        }

        @Override
        public Object call(Object param) {
            return new ClientResponse(clientRequest, Response.ok(param).build());
        }
    });
}
Also used : ExecutionForwardCall(com.pamirs.pradar.internal.adapter.ExecutionForwardCall) ClientResponse(org.glassfish.jersey.client.ClientResponse) MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) ClientRequest(org.glassfish.jersey.client.ClientRequest)

Example 14 with MatchConfig

use of com.pamirs.pradar.internal.config.MatchConfig in project LinkAgent by shulieTech.

the class DubboConsumerInterceptor method beforeLast.

@Override
public void beforeLast(Advice advice) throws ProcessControlException {
    final RpcInvocation invocation = (RpcInvocation) advice.getParameterArray()[0];
    String interfaceName = getInterfaceName(invocation);
    String methodName = invocation.getMethodName();
    MatchConfig config = ClusterTestUtils.rpcClusterTest(interfaceName, methodName);
    invocation.setAttachment(PradarService.PRADAR_WHITE_LIST_CHECK, String.valueOf(config.isSuccess()));
    config.addArgs("args", advice.getParameterArray());
    config.addArgs("url", interfaceName.concat("#").concat(methodName));
    config.addArgs("isInterface", Boolean.TRUE);
    config.addArgs("class", interfaceName);
    config.addArgs("method", methodName);
    config.addArgs("invocation", invocation);
    if (isShentongEvent(interfaceName)) {
        config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, "true");
    }
    if (config.getStrategy() instanceof JsonMockStrategy) {
        fixJsonStrategy.processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config);
    }
    config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionCall() {

        @Override
        public Object call(Object param) {
            try {
                // for 2.8.4
                return Reflect.on("org.apache.dubbo.rpc.RpcResult").create(param).get();
            } catch (Exception e) {
                if (logger.isInfoEnabled()) {
                    logger.info("find dubbo 2.8.4 class org.apache.dubbo.rpc.RpcResult fail, find others!", e);
                }
            // 
            }
            try {
                Reflect reflect = Reflect.on("org.apache.dubbo.rpc.AsyncRpcResult");
                try {
                    // for 2.7.5
                    java.util.concurrent.CompletableFuture<AppResponse> future = new java.util.concurrent.CompletableFuture<AppResponse>();
                    future.complete(new AppResponse(param));
                    Reflect result = reflect.create(future, invocation);
                    return result.get();
                } catch (ReflectException e) {
                    // for 2.7.3
                    Reflect result = reflect.create(invocation);
                    return result.get();
                }
            } catch (Exception e) {
                logger.error("fail to load dubbo 2.7.x class org.apache.dubbo.rpc.AsyncRpcResult", e);
                throw new ReflectException("fail to load dubbo 2.7.x class org.apache.dubbo.rpc.AsyncRpcResult", e);
            }
        }
    });
}
Also used : MatchConfig(com.pamirs.pradar.internal.config.MatchConfig) ReflectException(com.shulie.instrument.simulator.api.reflect.ReflectException) PradarException(com.pamirs.pradar.exception.PradarException) ReflectException(com.shulie.instrument.simulator.api.reflect.ReflectException) ProcessControlException(com.shulie.instrument.simulator.api.ProcessControlException) JsonMockStrategy(com.pamirs.pradar.pressurement.mock.JsonMockStrategy) Reflect(com.shulie.instrument.simulator.api.reflect.Reflect) ExecutionCall(com.pamirs.pradar.internal.config.ExecutionCall)

Example 15 with MatchConfig

use of com.pamirs.pradar.internal.config.MatchConfig in project LinkAgent by shulieTech.

the class ArbiterHttpExit method matching.

/**
 * Internal matching recursive function.
 */
private static MatchConfig matching(String string, MatchConfig matchConfig, int stringStartNdx, int patternStartNdx) {
    String wildcard = matchConfig.getUrl();
    int pNdx = patternStartNdx;
    int sNdx = stringStartNdx;
    int pLen = wildcard.length();
    if (pLen == 1) {
        if (wildcard.charAt(0) == '*') {
            // speed-up
            return matchConfig;
        }
    }
    int sLen = string.length();
    boolean nextIsNotWildcard = false;
    MatchConfig config = failure();
    while (true) {
        // check if end of string and/or pattern occurred
        if ((sNdx >= sLen)) {
            // end of string still may have pending '*' callback pattern
            while ((pNdx < pLen) && (wildcard.charAt(pNdx) == '*')) {
                pNdx++;
            }
            return pNdx >= pLen ? matchConfig : null;
        }
        if (pNdx >= pLen) {
            // end of pattern, but not end of the string
            return config;
        }
        // pattern char
        char p = wildcard.charAt(pNdx);
        // perform logic
        if (!nextIsNotWildcard) {
            if (p == '\\') {
                pNdx++;
                nextIsNotWildcard = true;
                continue;
            }
            if (p == '?') {
                sNdx++;
                pNdx++;
                continue;
            }
            if (p == '*') {
                // next pattern char
                char pnext = 0;
                if (pNdx + 1 < pLen) {
                    pnext = wildcard.charAt(pNdx + 1);
                }
                if (pnext == '*') {
                    // double '*' have the same effect as one '*'
                    pNdx++;
                    continue;
                }
                int i;
                pNdx++;
                // line that matches the rest of the pattern !!!
                for (i = string.length(); i >= sNdx; i--) {
                    if (null != matching(string, matchConfig, i, pNdx)) {
                        return matchConfig;
                    }
                }
                return config;
            }
        } else {
            nextIsNotWildcard = false;
        }
        // check if pattern char and string char are equals
        if (p != string.charAt(sNdx)) {
            return config;
        }
        // everything matches for now, continue
        sNdx++;
        pNdx++;
    }
}
Also used : MatchConfig(com.pamirs.pradar.internal.config.MatchConfig)

Aggregations

MatchConfig (com.pamirs.pradar.internal.config.MatchConfig)33 ProcessControlException (com.shulie.instrument.simulator.api.ProcessControlException)22 JSONObject (com.alibaba.fastjson.JSONObject)15 ExecutionCall (com.pamirs.pradar.internal.config.ExecutionCall)12 JsonMockStrategy (com.pamirs.pradar.pressurement.mock.JsonMockStrategy)12 SocketTimeoutException (java.net.SocketTimeoutException)10 PressureMeasureError (com.pamirs.pradar.exception.PressureMeasureError)8 IOException (java.io.IOException)8 ExecutionForwardCall (com.pamirs.pradar.internal.adapter.ExecutionForwardCall)7 URISyntaxException (java.net.URISyntaxException)6 Map (java.util.Map)6 URI (java.net.URI)5 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)5 StringEntity (org.apache.http.entity.StringEntity)5 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)5 BasicStatusLine (org.apache.http.message.BasicStatusLine)5 URL (java.net.URL)4 Buffer (okio.Buffer)4 ScriptEvaluator (com.pamirs.pradar.script.ScriptEvaluator)3 ReflectException (com.shulie.instrument.simulator.api.reflect.ReflectException)3