use of com.shulie.instrument.simulator.api.ProcessControlException in project LinkAgent by shulieTech.
the class ClientCallStartInterceptor method beforeFirst.
@Override
public void beforeFirst(final Advice advice) throws Exception {
if (!Pradar.isClusterTest()) {
return;
}
/**
* 白名单校验
*/
Object[] args = advice.getParameterArray();
Object target = advice.getTarget();
if (args == null || args.length != 2) {
return;
}
if (!(args[1] instanceof Metadata)) {
return;
}
/**
* 将"/"转换为#分割符 方便配置
*/
String fullName = getMethodName(target);
String interfaceName = "";
String methodName = "";
if (fullName.contains("/")) {
String[] arr = fullName.split("/");
interfaceName = arr[0];
methodName = arr[1];
}
MatchConfig matchConfig = ClusterTestUtils.rpcClusterTest(interfaceName, methodName);
matchConfig.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, true);
matchConfig.addArgs("class", interfaceName);
matchConfig.addArgs("method", methodName);
matchConfig.addArgs("isInterface", Boolean.TRUE);
matchConfig.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), matchConfig, new ExecutionCall() {
@Override
public Object call(Object param) throws ProcessControlException {
ClientCall.Listener observer = (ClientCall.Listener) advice.getParameterArray()[0];
observer.onMessage(param);
return null;
}
});
}
use of com.shulie.instrument.simulator.api.ProcessControlException in project LinkAgent by shulieTech.
the class RealCallEnqueueV2Interceptor method beforeFirst.
@Override
public void beforeFirst(Advice advice) throws ProcessControlException {
Object target = advice.getTarget();
Request request = null;
try {
request = Reflect.on(target).get(OKHttpConstants.DYNAMIC_FIELD_REQUEST);
} catch (ReflectException e) {
request = Reflect.on(target).get(OKHttpConstants.DYNAMIC_FIELD_ORIGINAL_REQUEST);
}
final String url = request.urlString();
final MatchConfig config = ClusterTestUtils.httpClusterTest(url);
String check = request.header(OKHttpConstants.DYNAMIC_FIELD_HEADER);
config.addArgs(PradarService.PRADAR_WHITE_LIST_CHECK, check);
config.addArgs("url", url);
config.addArgs("isInterface", Boolean.FALSE);
final Request finalRequest = request;
config.getStrategy().processBlock(advice.getBehavior().getReturnType(), advice.getClassLoader(), config, new ExecutionForwardCall() {
@Override
public Object forward(Object param) throws ProcessControlException {
HttpUrl httpUrl = HttpUrl.parse(config.getForwarding());
Reflect.on(finalRequest).set("url", httpUrl);
return null;
}
@Override
public Object call(Object param) {
Headers header = Headers.of(new String[0]);
Buffer buffer = new Buffer();
try {
if (param instanceof String) {
buffer.write(String.valueOf(param).getBytes("UTF-8"));
} else {
buffer.write(JSONObject.toJSONBytes(param));
}
} catch (IOException e) {
}
return new Response.Builder().code(200).body(new RealResponseBody(header, buffer)).request(finalRequest).protocol(Protocol.HTTP_1_0).message("OK").build();
}
});
}
use of com.shulie.instrument.simulator.api.ProcessControlException in project LinkAgent by shulieTech.
the class AsyncHttpClientv4MethodInterceptor1 method doBefore.
@Override
public void doBefore(final Advice advice) throws ProcessControlException {
Object[] args = advice.getParameterArray();
final HttpUriRequest request = (HttpUriRequest) args[0];
if (request == null) {
return;
}
InnerWhiteListCheckUtil.check();
String host = request.getURI().getHost();
int port = request.getURI().getPort();
String path = request.getURI().getPath();
// 判断是否在白名单中
String url = getService(request.getURI().getScheme(), 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 == 2) {
config.addArgs("futureCallback", args[1]);
} else if (args.length == 3) {
config.addArgs("futureCallback", args[2]);
}
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);
futureCallback.completed(response);
java.util.concurrent.CompletableFuture future = new java.util.concurrent.CompletableFuture();
future.complete(response);
return future;
} catch (Exception e) {
}
return null;
}
});
String method = request.getMethod();
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.shulie.instrument.simulator.api.ProcessControlException in project LinkAgent by shulieTech.
the class AsyncHttpClientv4MethodInterceptor2 method doBefore.
@Override
public void doBefore(final Advice advice) throws ProcessControlException {
Object[] args = advice.getParameterArray();
HttpAsyncRequestProducer httpAsyncRequestProducer = (HttpAsyncRequestProducer) args[0];
HttpHost httpHost = httpAsyncRequestProducer.getTarget();
HttpRequest request = null;
try {
request = httpAsyncRequestProducer.generateRequest();
} catch (Throwable e) {
LOGGER.error("AsyncHttpClient org.apache.http.impl.nio.client.CloseableHttpAsyncClient.execute(org.apache.http.nio.protocol.HttpAsyncRequestProducer, org.apache.http.nio.protocol.HttpAsyncResponseConsumer<T>, org.apache.http.concurrent.FutureCallback<T>) generateRequest error. ignore it", e);
}
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]);
}
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;
}
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);
futureCallback.completed(response);
java.util.concurrent.CompletableFuture future = new java.util.concurrent.CompletableFuture();
future.complete(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;
}
final HttpRequest finalRequest = request;
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(finalRequest, (HttpResponse) result);
} else {
afterTrace(finalRequest, 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(finalRequest, 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(finalRequest, null);
} catch (Throwable e) {
LOGGER.error("AsyncHttpClient execute future endTrace error.", e);
Pradar.endClientInvoke("200", HttpClientConstants.PLUGIN_TYPE);
}
}
});
}
use of com.shulie.instrument.simulator.api.ProcessControlException 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);
}
}
});
}
Aggregations