use of com.creditease.uav.apm.slowoper.spi.SlowOperContext in project uavstack by uavorg.
the class AbstractHttpClientAdapter method handleSlowOperSupporter.
protected void handleSlowOperSupporter(HttpRequest request, Span span, InvokeChainContext context) {
if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
SlowOperContext slowOperContext = new SlowOperContext();
String queryParams = getParamsFromUri(request.getRequestLine().getUri());
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getRequestHeaders(request) + queryParams);
try {
// 通过此种方式判断是否存在body,通过httpclient类继承确定
if (HttpEntityEnclosingRequestBase.class.isAssignableFrom(request.getClass())) {
HttpEntityEnclosingRequestBase req = (HttpEntityEnclosingRequestBase) request;
HttpEntity entity = req.getEntity();
// 兼容不正当使用
if (entity == null) {
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, "");
} else {
Header header = entity.getContentEncoding();
String encoding = header == null ? "utf-8" : header.getValue();
try {
BufferedHttpEntity httpEntityWrapper = new BufferedHttpEntity(entity);
req.setEntity(httpEntityWrapper);
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, getHttpEntityContent(httpEntityWrapper, encoding));
} catch (IOException e) {
logger.warn("HttpEntityWrapper failed!", e);
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, e.toString());
}
}
} else {
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, "");
}
} catch (Exception e) {
// 由于会存在无法缓冲的情况,故此处添加捕获
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, e.toString());
}
Object[] params = { span, slowOperContext };
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap", span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.PRECAP, context, params);
}
}
use of com.creditease.uav.apm.slowoper.spi.SlowOperContext in project uavstack by uavorg.
the class ApacheAsyncHttpClientAdapter method afterDoCap.
@Override
public void afterDoCap(InvokeChainContext context, Object[] args) {
if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
Span span = (Span) context.get(InvokeChainConstants.PARAM_SPAN_KEY);
SlowOperContext slowOperContext = new SlowOperContext();
if (args[0] != null) {
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_EXCEPTION, args[0].toString());
} else if (Exception.class.isAssignableFrom(args[1].getClass())) {
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_EXCEPTION, args[1].toString());
} else if (HttpResponse.class.isAssignableFrom(args[1].getClass())) {
HttpResponse response = (HttpResponse) args[1];
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getResponHeaders(response));
HttpEntity entity = response.getEntity();
// 由于存在读取失败和无法缓存的大entity会使套壳失败,故此处添加如下判断
if (BufferedHttpEntity.class.isAssignableFrom(entity.getClass())) {
Header header = entity.getContentEncoding();
String encoding = header == null ? "utf-8" : header.getValue();
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, getHttpEntityContent(entity, encoding));
} else {
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, "HttpEntityWrapper failed! Maybe HTTP entity too large to be buffered in memory");
}
} else {
HttpResponse response = (HttpResponse) args[1];
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getResponHeaders(response));
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, "unsupported response");
}
Object[] params = { span, slowOperContext };
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap", span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.DOCAP, context, params);
}
}
use of com.creditease.uav.apm.slowoper.spi.SlowOperContext in project uavstack by uavorg.
the class ApacheHttpClientAdapter method afterDoCap.
@Override
public void afterDoCap(InvokeChainContext context, Object[] args) {
if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
Span span = (Span) context.get(InvokeChainConstants.PARAM_SPAN_KEY);
SlowOperContext slowOperContext = new SlowOperContext();
if (Throwable.class.isAssignableFrom(args[0].getClass())) {
Throwable e = (Throwable) args[0];
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_EXCEPTION, e.toString());
} else {
HttpResponse response = (HttpResponse) args[0];
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getResponHeaders(response));
HttpEntity entity = response.getEntity();
// 由于存在读取失败和无法缓存的大entity会使套壳失败,故此处添加如下判断
if (BufferedHttpEntity.class.isAssignableFrom(entity.getClass())) {
Header header = entity.getContentEncoding();
String encoding = header == null ? "utf-8" : header.getValue();
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, getHttpEntityContent(entity, encoding));
} else {
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, "HttpEntityWrapper failed! Maybe HTTP entity too large to be buffered in memory");
}
}
Object[] params = { span, slowOperContext };
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap", span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.DOCAP, context, params);
}
}
use of com.creditease.uav.apm.slowoper.spi.SlowOperContext in project uavstack by uavorg.
the class ApacheHttpClient3Adapter method afterPreCap.
@Override
public void afterPreCap(InvokeChainContext context, Object[] args) {
/**
* after precap the client's span is created, set the span meta into http request header
*/
String url = (String) context.get(InvokeChainConstants.CLIENT_SPAN_THREADLOCAL_STOREKEY);
Span span = this.spanFactory.getSpanFromContext(url);
String spanMeta = this.spanFactory.getSpanMeta(span);
HttpMethod method = (HttpMethod) args[1];
method.removeRequestHeader(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO);
method.addRequestHeader(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO, spanMeta);
if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
// httpclient3好像没有encoding参数
SlowOperContext slowOperContext = new SlowOperContext();
String quertParams = method.getQueryString();
if (quertParams == null) {
quertParams = "{}";
}
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getHeadersContent(method.getRequestHeaders()) + quertParams);
// 通过此种方式判断是否存在body,通过httpclient类继承确定
String requestBody = "";
if (EntityEnclosingMethod.class.isAssignableFrom(method.getClass())) {
EntityEnclosingMethod entityMethod = (EntityEnclosingMethod) method;
RequestEntity requestEntity = entityMethod.getRequestEntity();
// 根据不同类型entity采取不同读取方式(由于httpclient3没有显视encoding,故此处使用默认的编码格式)
if (ByteArrayRequestEntity.class.isAssignableFrom(requestEntity.getClass())) {
requestBody = new String(((ByteArrayRequestEntity) requestEntity).getContent());
} else if (InputStreamRequestEntity.class.isAssignableFrom(requestEntity.getClass())) {
InputStreamRequestEntity inputStreamRequestEntity = (InputStreamRequestEntity) requestEntity;
if (!inputStreamRequestEntity.isRepeatable()) {
inputStreamRequestEntity.getContentLength();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStreamRequestEntity.getContent()));
StringBuilder body = new StringBuilder();
String str;
while ((str = reader.readLine()) != null) {
body.append(str);
}
requestBody = body.toString();
} catch (UnsupportedOperationException e) {
logger.warn("body type is not supported!", e);
requestBody = "body type is not supported!" + e.toString();
} catch (IOException e) {
logger.error("IOException!", e);
requestBody = e.toString();
}
} else if (MultipartRequestEntity.class.isAssignableFrom(requestEntity.getClass())) {
// MultipartRequestEntity暂时不支持
requestBody = "MultipartRequestEntity is not supported";
} else if (StringRequestEntity.class.isAssignableFrom(requestEntity.getClass())) {
requestBody = ((StringRequestEntity) requestEntity).getContent();
} else {
requestBody = "unsupported type";
}
}
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, requestBody);
Object[] params = { span, slowOperContext };
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap", span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.PRECAP, context, params);
}
}
use of com.creditease.uav.apm.slowoper.spi.SlowOperContext in project uavstack by uavorg.
the class ApacheHttpClient3Adapter method afterDoCap.
@Override
public void afterDoCap(InvokeChainContext context, Object[] args) {
if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
Span span = (Span) context.get(InvokeChainConstants.PARAM_SPAN_KEY);
SlowOperContext slowOperContext = new SlowOperContext();
if (Throwable.class.isAssignableFrom(args[0].getClass())) {
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_EXCEPTION, args[0].toString());
} else {
HttpMethod method = (HttpMethod) args[0];
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_HEADER, getHeadersContent(method.getRequestHeaders()));
try {
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_BODY, method.getResponseBodyAsString());
} catch (IOException e) {
slowOperContext.put(SlowOperConstants.PROTOCOL_HTTP_EXCEPTION, e.toString());
}
}
Object[] params = { span, slowOperContext };
UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap", span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.DOCAP, context, params);
}
}
Aggregations