|
1 | 1 | package com.yammer.metrics.reporting;
|
2 | 2 |
|
| 3 | +import org.apache.http.HttpResponse; |
| 4 | +import org.apache.http.client.methods.HttpPost; |
| 5 | +import org.apache.http.client.methods.HttpUriRequest; |
| 6 | +import org.apache.http.concurrent.FutureCallback; |
| 7 | +import org.apache.http.entity.ByteArrayEntity; |
| 8 | +import org.apache.http.entity.ContentType; |
| 9 | +import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; |
| 10 | +import org.apache.http.impl.nio.client.HttpAsyncClients; |
| 11 | + |
3 | 12 | import java.io.ByteArrayOutputStream;
|
4 | 13 | import java.io.IOException;
|
5 | 14 | import java.io.OutputStream;
|
6 |
| - |
7 |
| -import org.slf4j.Logger; |
8 |
| -import org.slf4j.LoggerFactory; |
9 |
| - |
10 |
| -import com.ning.http.client.AsyncHandler; |
11 |
| -import com.ning.http.client.AsyncHttpClient; |
12 |
| -import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; |
13 |
| -import com.ning.http.client.HttpResponseBodyPart; |
14 |
| -import com.ning.http.client.HttpResponseHeaders; |
15 |
| -import com.ning.http.client.HttpResponseStatus; |
| 15 | +import java.util.concurrent.Future; |
16 | 16 |
|
17 | 17 | public class HttpTransport implements Transport {
|
18 |
| - private final String apiKey; |
19 |
| - private final AsyncHttpClient client; |
20 |
| - private final String seriesUrl; |
21 |
| - private static final Logger LOG = LoggerFactory |
22 |
| - .getLogger(HttpTransport.class); |
| 18 | + private final CloseableHttpAsyncClient client; |
| 19 | + private final String seriesUrl; |
23 | 20 |
|
24 |
| - public HttpTransport(String host, String apiKey) { |
25 |
| - this.apiKey = apiKey; |
26 |
| - this.client = new AsyncHttpClient(); |
27 |
| - this.seriesUrl = String.format("https://%s/api/v1/series?api_key=%s", host, |
28 |
| - apiKey); |
29 |
| - } |
30 |
| - |
31 |
| - public static class HttpRequest implements Transport.Request { |
32 |
| - private final BoundRequestBuilder requestBuilder; |
33 |
| - private final ByteArrayOutputStream out; |
34 |
| - |
35 |
| - public HttpRequest(HttpTransport transport, String apiKey, |
36 |
| - BoundRequestBuilder requestBuilder) throws IOException { |
37 |
| - this.requestBuilder = requestBuilder; |
38 |
| - this.requestBuilder.addHeader("Content-Type", "application/json"); |
39 |
| - this.out = new ByteArrayOutputStream(); |
| 21 | + public HttpTransport(String host, String apiKey) { |
| 22 | + this.client = HttpAsyncClients.createDefault(); |
| 23 | + this.seriesUrl = String.format("https://%s/api/v1/series?api_key=%s", host, apiKey); |
40 | 24 | }
|
41 | 25 |
|
42 |
| - public OutputStream getBodyWriter() { |
43 |
| - return out; |
| 26 | + public Request prepare() throws IOException { |
| 27 | + return new HttpRequest(this); |
44 | 28 | }
|
45 | 29 |
|
46 |
| - public void send() throws Exception { |
47 |
| - out.flush(); |
48 |
| - out.close(); |
49 |
| - requestBuilder.setBody(out.toByteArray()) |
50 |
| - .execute(new AsyncHandler<Void>() { |
51 |
| - |
52 |
| - public STATE onBodyPartReceived(HttpResponseBodyPart bp) |
53 |
| - throws Exception { |
54 |
| - return STATE.CONTINUE; |
| 30 | + public Future<HttpResponse> execute(HttpUriRequest request) throws Exception { |
| 31 | + return this.client.execute(request, new FutureCallback<HttpResponse>() { |
| 32 | + public void completed(HttpResponse result) { |
| 33 | + LOG.debug("Completed sending metrics to datadog"); |
55 | 34 | }
|
56 | 35 |
|
57 |
| - public Void onCompleted() throws Exception { |
58 |
| - return null; |
| 36 | + public void failed(Exception ex) { |
| 37 | + LOG.error("Error Writing Datadog metrics", ex); |
59 | 38 | }
|
60 | 39 |
|
61 |
| - public STATE onHeadersReceived(HttpResponseHeaders headers) |
62 |
| - throws Exception { |
63 |
| - return STATE.CONTINUE; |
| 40 | + public void cancelled() { |
| 41 | + LOG.debug("Cancelled request to send metrics to datadog"); |
64 | 42 | }
|
65 |
| - |
66 |
| - public STATE onStatusReceived(HttpResponseStatus arg0) |
67 |
| - throws Exception { |
68 |
| - return STATE.CONTINUE; |
69 |
| - } |
70 |
| - |
71 |
| - public void onThrowable(Throwable t) { |
72 |
| - LOG.error("Error Writing Datadog metrics", t); |
73 |
| - } |
74 |
| - |
75 |
| - }).get(); |
| 43 | + }); |
76 | 44 | }
|
77 |
| - } |
78 | 45 |
|
79 |
| - public HttpRequest prepare() throws IOException { |
80 |
| - BoundRequestBuilder builder = client.preparePost(seriesUrl); |
81 |
| - return new HttpRequest(this, apiKey, builder); |
82 |
| - } |
| 46 | + public static class HttpRequest implements Transport.Request { |
| 47 | + private final HttpTransport transport; |
| 48 | + private final HttpPost request; |
| 49 | + private final ByteArrayOutputStream out; |
| 50 | + |
| 51 | + public HttpRequest(HttpTransport transport) throws IOException { |
| 52 | + this.transport = transport; |
| 53 | + this.request = new HttpPost(this.transport.seriesUrl); |
| 54 | + this.out = new ByteArrayOutputStream(); |
| 55 | + } |
| 56 | + |
| 57 | + public OutputStream getBodyWriter() { |
| 58 | + return out; |
| 59 | + } |
| 60 | + |
| 61 | + public void send() throws Exception { |
| 62 | + this.out.flush(); |
| 63 | + this.out.close(); |
| 64 | + this.request.setEntity(new ByteArrayEntity(out.toByteArray(), ContentType.APPLICATION_JSON)); |
| 65 | + this.transport.execute(this.request).get(); |
| 66 | + } |
| 67 | + } |
83 | 68 | }
|
0 commit comments