Skip to content

Commit da71297

Browse files
Implement a mechanism for adding ad-hoc listeners to OKHTTP requests (#6660)
1 parent 2dcbd3b commit da71297

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

communication/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies {
2020
testImplementation deps.bytebuddy
2121
testImplementation group: 'org.msgpack', name: 'msgpack-core', version: '0.8.20'
2222
testImplementation group: 'org.msgpack', name: 'jackson-dataformat-msgpack', version: '0.8.20'
23+
testImplementation group: 'com.squareup.okhttp3', name: 'mockwebserver', version: versions.okhttp_legacy
2324
}
2425

2526
ext {
@@ -33,6 +34,7 @@ ext {
3334
'datadog.communication.http.OkHttpUtils',
3435
'datadog.communication.http.OkHttpUtils.1',
3536
'datadog.communication.http.OkHttpUtils.ByteBufferRequestBody',
37+
'datadog.communication.http.OkHttpUtils.CustomListener',
3638
'datadog.communication.http.OkHttpUtils.GZipByteBufferRequestBody',
3739
'datadog.communication.http.OkHttpUtils.GZipRequestBodyDecorator',
3840
'datadog.communication.http.OkHttpUtils.JsonRequestBody',

communication/src/main/java/datadog/communication/http/OkHttpUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import okhttp3.ConnectionSpec;
2424
import okhttp3.Credentials;
2525
import okhttp3.Dispatcher;
26+
import okhttp3.EventListener;
2627
import okhttp3.HttpUrl;
2728
import okhttp3.MediaType;
2829
import okhttp3.OkHttpClient;
@@ -100,6 +101,8 @@ public static OkHttpClient buildHttpClient(
100101
timeoutMillis);
101102
}
102103

104+
public abstract static class CustomListener extends EventListener {}
105+
103106
private static OkHttpClient buildHttpClient(
104107
final String unixDomainSocketPath,
105108
final String namedPipe,
@@ -115,6 +118,12 @@ private static OkHttpClient buildHttpClient(
115118
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
116119

117120
builder
121+
.eventListenerFactory(
122+
call -> {
123+
Request request = call.request();
124+
CustomListener listener = request.tag(CustomListener.class);
125+
return listener != null ? listener : EventListener.NONE;
126+
})
118127
.connectTimeout(timeoutMillis, MILLISECONDS)
119128
.writeTimeout(timeoutMillis, MILLISECONDS)
120129
.readTimeout(timeoutMillis, MILLISECONDS)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package datadog.communication.http
2+
3+
import okhttp3.Call
4+
import okhttp3.Request
5+
import okhttp3.mockwebserver.MockResponse
6+
import okhttp3.mockwebserver.MockWebServer
7+
import spock.lang.Shared
8+
import spock.lang.Specification
9+
10+
class OkHttpUtilsTest extends Specification {
11+
12+
@Shared
13+
MockWebServer server = new MockWebServer()
14+
15+
def cleanupSpec() {
16+
server.shutdown()
17+
}
18+
19+
def "It is possible to register a custom listener for HTTP requests"() {
20+
setup:
21+
def url = server.url("/")
22+
def client = OkHttpUtils.buildHttpClient(url, 1000)
23+
def listener = new TestListener()
24+
25+
server.enqueue(new MockResponse())
26+
27+
when:
28+
def request = new Request.Builder()
29+
.url(url)
30+
.get()
31+
.tag(OkHttpUtils.CustomListener, listener)
32+
.build()
33+
client.newCall(request).execute()
34+
35+
then:
36+
listener.notified
37+
}
38+
39+
private static final class TestListener extends OkHttpUtils.CustomListener {
40+
private boolean notified
41+
42+
void callStart(Call call) {
43+
notified = true
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)