use of com.pamirs.pradar.pressurement.mock.JsonMockStrategy 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);
}
use of com.pamirs.pradar.pressurement.mock.JsonMockStrategy 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);
}
use of com.pamirs.pradar.pressurement.mock.JsonMockStrategy 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);
}
}
});
}
use of com.pamirs.pradar.pressurement.mock.JsonMockStrategy in project LinkAgent by shulieTech.
the class AsyncHttpClientv4MethodInterceptor method doBefore.
@Override
public void doBefore(final Advice advice) throws ProcessControlException {
Object[] args = advice.getParameterArray();
HttpHost httpHost = (HttpHost) args[0];
final HttpRequest request = (HttpRequest) args[1];
if (httpHost == null) {
return;
}
InnerWhiteListCheckUtil.check();
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(" ")));
if (request instanceof HttpUriRequest) {
path = ((HttpUriRequest) request).getURI().getPath();
method = ((HttpUriRequest) request).getMethod();
}
// 判断是否在白名单中
String url = getService(httpHost.getSchemeName(), host, port, path);
final 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);
if (args.length == 3) {
config.addArgs("futureCallback", args[2]);
} else if (args.length == 4) {
config.addArgs("futureCallback", args[3]);
}
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) {
if (null == config.getArgs().get("futureCallback")) {
return null;
}
// 现在先暂时注释掉因为只有jdk8以上才能用
FutureCallback<HttpResponse> futureCallback = (FutureCallback<HttpResponse>) config.getArgs().get("futureCallback");
StatusLine statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "");
try {
HttpEntity entity = null;
if (param instanceof String) {
entity = new StringEntity(String.valueOf(param));
} else {
entity = new ByteArrayEntity(JSONObject.toJSONBytes(param));
}
BasicHttpResponse response = new BasicHttpResponse(statusline);
response.setEntity(entity);
java.util.concurrent.CompletableFuture future = new java.util.concurrent.CompletableFuture();
future.complete(response);
futureCallback.completed(response);
return future;
} catch (Exception e) {
}
return null;
}
});
Pradar.startClientInvoke(path, method);
Pradar.remoteIp(host);
Pradar.remotePort(port);
Pradar.middlewareName(HttpClientConstants.HTTP_CLIENT_NAME_4X);
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.popInvokeContext();
final Object future = args[args.length - 1];
if (!(future instanceof FutureCallback)) {
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 HttpResponse) {
afterTrace(request, (HttpResponse) 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);
}
}
});
}
use of com.pamirs.pradar.pressurement.mock.JsonMockStrategy in project LinkAgent by shulieTech.
the class HttpClientv4MethodInterceptor method beforeLast.
@Override
public void beforeLast(Advice advice) throws ProcessControlException {
Object[] args = advice.getParameterArray();
HttpHost httpHost = (HttpHost) args[0];
final HttpRequest request = (HttpRequest) args[1];
if (httpHost == null) {
return;
}
String host = httpHost.getHostName();
int port = httpHost.getPort();
String path = httpHost.getHostName();
if (request instanceof HttpUriRequest) {
path = ((HttpUriRequest) request).getURI().getPath();
}
// 判断是否在白名单中
String url = getService(httpHost.getSchemeName(), host, port, path);
MatchConfig config = ClusterTestUtils.httpClusterTest(url);
Header[] headers = request.getHeaders(PradarService.PRADAR_WHITE_LIST_CHECK);
if (headers != null && headers.length > 0) {
config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, headers[0].getValue());
}
config.addArgs("url", url);
config.addArgs("request", request);
config.addArgs("method", "uri");
config.addArgs("isInterface", Boolean.FALSE);
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) {
StatusLine statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "");
try {
HttpEntity entity = null;
if (param instanceof String) {
entity = new StringEntity(String.valueOf(param), "UTF-8");
} else {
entity = new ByteArrayEntity(JSONObject.toJSONBytes(param));
}
BasicHttpResponse response = new BasicHttpResponse(statusline);
response.setEntity(entity);
if (HttpClientConstants.clazz == null) {
HttpClientConstants.clazz = Class.forName("org.apache.http.impl.execchain.HttpResponseProxy");
}
return Reflect.on(HttpClientConstants.clazz).create(response, null).get();
} catch (Exception e) {
}
return null;
}
});
}
Aggregations