Skip to content

Commit 03c79a1

Browse files
author
Ankur Chauhan
committed
Use apachecomponents asyncHttpClient instead of ning
1 parent 93ae818 commit 03c79a1

File tree

7 files changed

+76
-114
lines changed

7 files changed

+76
-114
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
.project
33
.settings
44
target
5+
.idea
6+
*.iml

Diff for: pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@
8787
<scope>compile</scope>
8888
</dependency>
8989
<dependency>
90-
<groupId>com.ning</groupId>
91-
<artifactId>async-http-client</artifactId>
92-
<version>1.7.20</version>
90+
<groupId>org.apache.httpcomponents</groupId>
91+
<artifactId>httpasyncclient</artifactId>
92+
<version>4.0.1</version>
9393
</dependency>
9494
<dependency>
9595
<groupId>com.fasterxml.jackson.core</groupId>

Diff for: src/main/java/com/yammer/metrics/reporting/AwsHelper.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package com.yammer.metrics.reporting;
22

3-
import java.io.IOException;
4-
import java.util.concurrent.Future;
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.client.methods.HttpGet;
5+
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
6+
import org.apache.http.impl.nio.client.HttpAsyncClients;
7+
import org.apache.http.util.EntityUtils;
58

6-
import com.ning.http.client.*;
9+
import java.io.IOException;
710

811
public class AwsHelper {
912

1013
public static final String url = "http://169.254.169.254/latest/meta-data/instance-id";
1114

1215
public static String getEc2InstanceId() throws IOException {
13-
AsyncHttpClient client = new AsyncHttpClient();
16+
CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();;
1417
try {
15-
Future<Response> f = client.prepareGet(url).execute();
16-
Response resp = f.get();
17-
18-
return resp.getResponseBody();
18+
final HttpResponse response = client.execute(new HttpGet(url), null).get();
19+
return EntityUtils.toString(response.getEntity());
1920
} catch (Throwable t) {
2021
throw new IOException(t);
2122
}
+48-63
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,68 @@
11
package com.yammer.metrics.reporting;
22

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+
312
import java.io.ByteArrayOutputStream;
413
import java.io.IOException;
514
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;
1616

1717
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;
2320

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);
4024
}
4125

42-
public OutputStream getBodyWriter() {
43-
return out;
26+
public Request prepare() throws IOException {
27+
return new HttpRequest(this);
4428
}
4529

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");
5534
}
5635

57-
public Void onCompleted() throws Exception {
58-
return null;
36+
public void failed(Exception ex) {
37+
LOG.error("Error Writing Datadog metrics", ex);
5938
}
6039

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");
6442
}
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+
});
7644
}
77-
}
7845

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+
}
8368
}

Diff for: src/main/java/com/yammer/metrics/reporting/Transport.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package com.yammer.metrics.reporting;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
import java.io.IOException;
47
import java.io.OutputStream;
58

69
public interface Transport {
7-
public Request prepare() throws IOException ;
8-
10+
11+
static final Logger LOG = LoggerFactory.getLogger(Transport.class);
12+
13+
public Request prepare() throws IOException;
14+
915
public interface Request {
1016
OutputStream getBodyWriter();
1117
void send() throws Exception;
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.yammer.metrics.reporting;
22

3-
import static org.junit.Assert.*;
3+
import com.yammer.metrics.reporting.model.DatadogCounter;
4+
import org.junit.Test;
45

56
import java.util.List;
67

7-
import org.junit.Ignore;
8-
import org.junit.Test;
9-
10-
import com.yammer.metrics.reporting.model.DatadogCounter;
8+
import static org.junit.Assert.assertEquals;
119

1210
public class DatadogCounterTest {
1311

@@ -23,17 +21,4 @@ public void testSplitNameAndTags() {
2321
assertEquals("tag3:value3", tags.get(2));
2422
}
2523

26-
@Ignore("Rely on datadog to strip tags")
27-
@Test
28-
public void testStripInvalidCharsFromTags() {
29-
DatadogCounter counter = new DatadogCounter(
30-
"test[tag1:va lue1,tag2:va .%lue2,ta %# g3:value3]", 1L, 1234L, "Test Host");
31-
List<String> tags = counter.getTags();
32-
33-
assertEquals(3, tags.size());
34-
assertEquals("tag1:value1", tags.get(0));
35-
assertEquals("tag2:value2", tags.get(1));
36-
assertEquals("tag3:value3", tags.get(2));
37-
}
38-
3924
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.yammer.metrics.reporting;
22

3-
import static org.junit.Assert.*;
3+
import com.yammer.metrics.reporting.model.DatadogGauge;
4+
import org.junit.Test;
45

56
import java.util.List;
67

7-
import org.junit.Ignore;
8-
import org.junit.Test;
9-
10-
import com.yammer.metrics.reporting.model.DatadogGauge;
8+
import static org.junit.Assert.assertEquals;
119

1210
public class DatadogGaugeTest {
1311

@@ -22,19 +20,4 @@ public void testSplitNameAndTags() {
2220
assertEquals("tag2:value2", tags.get(1));
2321
assertEquals("tag3:value3", tags.get(2));
2422
}
25-
26-
@Ignore("Rely on datadog to strip tags")
27-
@Test
28-
public void testStripInvalidCharsFromTags() {
29-
DatadogGauge gauge = new DatadogGauge(
30-
"test[tag1:va lue1,tag2:va .%lue2,ta %# g3:value3]", 1L, 1234L,
31-
"Test Host");
32-
List<String> tags = gauge.getTags();
33-
34-
assertEquals(3, tags.size());
35-
assertEquals("tag1:value1", tags.get(0));
36-
assertEquals("tag2:value2", tags.get(1));
37-
assertEquals("tag3:value3", tags.get(2));
38-
}
39-
4023
}

0 commit comments

Comments
 (0)