新增HttpClient插件
This commit is contained in:
parent
52e268f577
commit
d19b0fe709
|
|
@ -0,0 +1,33 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>skywalking-sdk-plugin</artifactId>
|
||||
<groupId>com.ai.cloud</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>httpClient-plugin</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>httpClient-plugin</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.ai.cloud</groupId>
|
||||
<artifactId>skywalking-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.2.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,132 @@
|
|||
package com.ai.cloud.skywalking.plugin.httpclient;
|
||||
|
||||
import com.ai.cloud.skywalking.plugin.httpclient.trace.HttpClientTracing;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.ResponseHandler;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SWTracingHttpClient implements HttpClient {
|
||||
|
||||
private static final String DEFAULT_TRACE_NAME = "SkyWalking-TRACING-NAME";
|
||||
|
||||
private HttpClient client;
|
||||
private String traceName;
|
||||
|
||||
public SWTracingHttpClient(HttpClient client, String traceName) {
|
||||
this.client = client;
|
||||
this.traceName = traceName;
|
||||
}
|
||||
|
||||
public SWTracingHttpClient(String traceName) {
|
||||
super();
|
||||
this.traceName = traceName;
|
||||
}
|
||||
|
||||
public SWTracingHttpClient() {
|
||||
client = new DefaultHttpClient();
|
||||
this.traceName = DEFAULT_TRACE_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpParams getParams() {
|
||||
return client.getParams();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientConnectionManager getConnectionManager() {
|
||||
return client.getConnectionManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpResponse execute(final HttpUriRequest httpUriRequest) throws IOException, ClientProtocolException {
|
||||
return HttpClientTracing.execute(httpUriRequest.getURI().toString(), traceName, httpUriRequest, new HttpClientTracing.Executor<HttpResponse>() {
|
||||
@Override
|
||||
public HttpResponse execute() throws IOException {
|
||||
return client.execute(httpUriRequest);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpResponse execute(final HttpUriRequest httpUriRequest, final HttpContext httpContext) throws IOException, ClientProtocolException {
|
||||
return HttpClientTracing.execute(httpUriRequest.getURI().toString(), traceName, httpUriRequest, new HttpClientTracing.Executor<HttpResponse>() {
|
||||
@Override
|
||||
public HttpResponse execute() throws IOException {
|
||||
return client.execute(httpUriRequest, httpContext);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpResponse execute(final HttpHost httpHost, final HttpRequest httpRequest) throws IOException, ClientProtocolException {
|
||||
return HttpClientTracing.execute(httpHost.toURI(), traceName, httpRequest, new HttpClientTracing.Executor<HttpResponse>() {
|
||||
@Override
|
||||
public HttpResponse execute() throws IOException {
|
||||
return client.execute(httpHost, httpRequest);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpResponse execute(final HttpHost httpHost, final HttpRequest httpRequest, final HttpContext httpContext) throws IOException, ClientProtocolException {
|
||||
return HttpClientTracing.execute(httpHost.toURI(), traceName, httpRequest, new HttpClientTracing.Executor<HttpResponse>() {
|
||||
@Override
|
||||
public HttpResponse execute() throws IOException {
|
||||
return client.execute(httpHost, httpRequest, httpContext);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T execute(final HttpUriRequest httpUriRequest, final ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
|
||||
return HttpClientTracing.execute(httpUriRequest.getURI().toString(), traceName, httpUriRequest, new HttpClientTracing.Executor<T>() {
|
||||
@Override
|
||||
public T execute() throws IOException {
|
||||
return client.execute(httpUriRequest, responseHandler);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T execute(final HttpUriRequest httpUriRequest, final ResponseHandler<? extends T> responseHandler, final HttpContext httpContext) throws IOException, ClientProtocolException {
|
||||
return HttpClientTracing.execute(httpUriRequest.getURI().toString(), traceName, httpUriRequest, new HttpClientTracing.Executor<T>() {
|
||||
@Override
|
||||
public T execute() throws IOException {
|
||||
return client.execute(httpUriRequest, responseHandler, httpContext);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T execute(final HttpHost httpHost, final HttpRequest httpRequest, final ResponseHandler<? extends T> responseHandler) throws IOException, ClientProtocolException {
|
||||
return HttpClientTracing.execute(httpHost.toURI(), traceName, httpRequest, new HttpClientTracing.Executor<T>() {
|
||||
@Override
|
||||
public T execute() throws IOException {
|
||||
return client.execute(httpHost, httpRequest, responseHandler);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T execute(final HttpHost httpHost, final HttpRequest httpRequest, final ResponseHandler<? extends T> responseHandler, HttpContext httpContext) throws IOException, ClientProtocolException {
|
||||
return HttpClientTracing.execute(httpHost.toURI(), traceName, httpRequest, new HttpClientTracing.Executor<T>() {
|
||||
@Override
|
||||
public T execute() throws IOException {
|
||||
return client.execute(httpHost, httpRequest, responseHandler);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package com.ai.cloud.skywalking.plugin.httpclient.trace;
|
||||
|
||||
import com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
import org.apache.http.HttpRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class HttpClientTracing {
|
||||
|
||||
private static RPCBuriedPointSender sender = new RPCBuriedPointSender();
|
||||
|
||||
public static <R> R execute(String url, String traceHearName, HttpRequest httpRequest, Executor<R> executor) throws IOException {
|
||||
try {
|
||||
httpRequest.setHeader(traceHearName,
|
||||
sender.beforeSend(Identification.newBuilder()
|
||||
.viewPoint(url)
|
||||
.spanType('W')
|
||||
.build())
|
||||
.toString());
|
||||
return executor.execute();
|
||||
} catch (IOException e) {
|
||||
sender.handleException(e);
|
||||
throw e;
|
||||
} finally {
|
||||
sender.afterSend();
|
||||
}
|
||||
}
|
||||
|
||||
public interface Executor<R> {
|
||||
R execute() throws IOException;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@
|
|||
<module>spring-plugin</module>
|
||||
<module>jdbc-plugin</module>
|
||||
<module>web-plugin</module>
|
||||
<module>httpclient-plugin</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue