本文主要研究一下skywalking的httpclient-pluginjava
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/resources/skywalking-plugin.defgit
httpclient-4.x=org.apache.skywalking.apm.plugin.httpClient.v4.define.AbstractHttpClientInstrumentation httpclient-4.x=org.apache.skywalking.apm.plugin.httpClient.v4.define.InternalHttpClientInstrumentation httpclient-4.x=org.apache.skywalking.apm.plugin.httpClient.v4.define.MinimalHttpClientInstrumentation httpclient-4.x=org.apache.skywalking.apm.plugin.httpClient.v4.define.DefaultRequestDirectorInstrumentation
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpClient/v4/define/HttpClientInstrumentation.javagithub
public abstract class HttpClientInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { private static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.httpClient.v4.HttpClientExecuteInterceptor"; @Override public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { return null; } protected String getInstanceMethodsInterceptor() { return INTERCEPT_CLASS; } }
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpClient/v4/define/AbstractHttpClientInstrumentation.javaapache
public class AbstractHttpClientInstrumentation extends HttpClientInstrumentation { private static final String ENHANCE_CLASS = "org.apache.http.impl.client.AbstractHttpClient"; @Override public ClassMatch enhanceClass() { return byName(ENHANCE_CLASS); } /** * version 4.2, intercept method: execute, intercept * public final HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) */ @Override public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[] { new InstanceMethodsInterceptPoint() { @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { return named("doExecute"); } @Override public String getMethodsInterceptor() { return getInstanceMethodsInterceptor(); } @Override public boolean isOverrideArgs() { return false; } } }; } }
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpClient/v4/define/InternalHttpClientInstrumentation.javaapp
public class InternalHttpClientInstrumentation extends HttpClientInstrumentation { private static final String ENHANCE_CLASS = "org.apache.http.impl.client.InternalHttpClient"; @Override public ClassMatch enhanceClass() { return NameMatch.byName(ENHANCE_CLASS); } @Override public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[] { new InstanceMethodsInterceptPoint() { @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { return named("doExecute"); } @Override public String getMethodsInterceptor() { return getInstanceMethodsInterceptor(); } @Override public boolean isOverrideArgs() { return false; } } }; } }
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpClient/v4/define/MinimalHttpClientInstrumentation.javaide
public class MinimalHttpClientInstrumentation extends HttpClientInstrumentation { private static final String ENHANCE_CLASS = "org.apache.http.impl.client.MinimalHttpClient"; @Override public ClassMatch enhanceClass() { return NameMatch.byName(ENHANCE_CLASS); } @Override public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[] { new InstanceMethodsInterceptPoint() { @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { return named("doExecute"); } @Override public String getMethodsInterceptor() { return getInstanceMethodsInterceptor(); } @Override public boolean isOverrideArgs() { return false; } } }; } }
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpClient/v4/define/DefaultRequestDirectorInstrumentation.javaui
public class DefaultRequestDirectorInstrumentation extends HttpClientInstrumentation { /** * Enhance class. */ private static final String ENHANCE_CLASS = "org.apache.http.impl.client.DefaultRequestDirector"; /** * DefaultRequestDirector is default implement. * usually use in version 4.0-4.2 * since 4.3, this class is Deprecated. */ @Override public ClassMatch enhanceClass() { return NameMatch.byName(ENHANCE_CLASS); } @Override public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { return new InstanceMethodsInterceptPoint[] { new InstanceMethodsInterceptPoint() { @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { return named("execute"); } @Override public String getMethodsInterceptor() { return getInstanceMethodsInterceptor(); } @Override public boolean isOverrideArgs() { return false; } } }; } }
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/httpClient/v4/HttpClientExecuteInterceptor.javathis
public class HttpClientExecuteInterceptor implements InstanceMethodsAroundInterceptor { @Override public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable { if (allArguments[0] == null || allArguments[1] == null) { // illegal args, can't trace. ignore. return; } final HttpHost httpHost = (HttpHost)allArguments[0]; HttpRequest httpRequest = (HttpRequest)allArguments[1]; final ContextCarrier contextCarrier = new ContextCarrier(); String remotePeer = httpHost.getHostName() + ":" + port(httpHost); String uri = httpRequest.getRequestLine().getUri(); String requestURI = getRequestURI(uri); String operationName = requestURI; AbstractSpan span = ContextManager.createExitSpan(operationName, contextCarrier, remotePeer); span.setComponent(ComponentsDefine.HTTPCLIENT); Tags.URL.set(span, buildSpanValue(httpHost,uri)); Tags.HTTP.METHOD.set(span, httpRequest.getRequestLine().getMethod()); SpanLayer.asHttp(span); CarrierItem next = contextCarrier.items(); while (next.hasNext()) { next = next.next(); httpRequest.setHeader(next.getHeadKey(), next.getHeadValue()); } } @Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { if (allArguments[0] == null || allArguments[1] == null) { return ret; } if (ret != null) { HttpResponse response = (HttpResponse)ret; StatusLine responseStatusLine = response.getStatusLine(); if (responseStatusLine != null) { int statusCode = responseStatusLine.getStatusCode(); AbstractSpan span = ContextManager.activeSpan(); if (statusCode >= 400) { span.errorOccurred(); Tags.STATUS_CODE.set(span, Integer.toString(statusCode)); } } } ContextManager.stopSpan(); return ret; } @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) { AbstractSpan activeSpan = ContextManager.activeSpan(); activeSpan.errorOccurred(); activeSpan.log(t); } private String getRequestURI(String uri) throws MalformedURLException { if (isUrl(uri)) { String requestPath = new URL(uri).getPath(); return requestPath != null && requestPath.length() > 0 ? requestPath : "/"; } else { return uri; } } private boolean isUrl(String uri) { String lowerUrl = uri.toLowerCase(); return lowerUrl.startsWith("http") || lowerUrl.startsWith("https"); } private String buildSpanValue(HttpHost httpHost, String uri) { if (isUrl(uri)) { return uri; } else { StringBuilder buff = new StringBuilder(); buff.append(httpHost.getSchemeName().toLowerCase()); buff.append("://"); buff.append(httpHost.getHostName()); buff.append(":"); buff.append(port(httpHost)); buff.append(uri); return buff.toString(); } } private int port(HttpHost httpHost) { int port = httpHost.getPort(); return port > 0 ? port : "https".equals(httpHost.getSchemeName().toLowerCase()) ? 443 : 80; } }
httpClient-4.x-plugin定義了四個加強,分別是AbstractHttpClientInstrumentation、InternalHttpClientInstrumentation、MinimalHttpClientInstrumentation、DefaultRequestDirectorInstrumentation;它們都繼承了HttpClientInstrumentation;使用HttpClientExecuteInterceptor去加強org.apache.http.impl.client.DefaultRequestDirector的doExecute方法、org.apache.http.impl.client.InternalHttpClient的doExecute方法、org.apache.http.impl.client.MinimalHttpClient的doExecute方法、org.apache.http.impl.client.DefaultRequestDirector的execute方法spa