1
1
package datadog .telemetry ;
2
2
3
+ import datadog .communication .http .HttpRetryPolicy ;
3
4
import datadog .communication .http .OkHttpUtils ;
4
5
import datadog .trace .api .Config ;
5
6
import datadog .trace .util .Strings ;
6
7
import java .io .IOException ;
8
+ import java .io .InterruptedIOException ;
7
9
import java .util .concurrent .TimeUnit ;
8
10
import okhttp3 .HttpUrl ;
9
11
import okhttp3 .OkHttpClient ;
10
12
import okhttp3 .Request ;
11
- import okhttp3 .Response ;
12
13
import org .slf4j .Logger ;
13
14
import org .slf4j .LoggerFactory ;
14
15
@@ -17,16 +18,19 @@ public class TelemetryClient {
17
18
public enum Result {
18
19
SUCCESS ,
19
20
FAILURE ,
20
- NOT_FOUND ;
21
+ NOT_FOUND ,
22
+ INTERRUPTED
21
23
}
22
24
23
- public static TelemetryClient buildAgentClient (OkHttpClient okHttpClient , HttpUrl agentUrl ) {
25
+ public static TelemetryClient buildAgentClient (
26
+ OkHttpClient okHttpClient , HttpUrl agentUrl , HttpRetryPolicy .Factory httpRetryPolicy ) {
24
27
HttpUrl agentTelemetryUrl =
25
28
agentUrl .newBuilder ().addPathSegments (AGENT_TELEMETRY_API_ENDPOINT ).build ();
26
- return new TelemetryClient (okHttpClient , agentTelemetryUrl , null );
29
+ return new TelemetryClient (okHttpClient , httpRetryPolicy , agentTelemetryUrl , null );
27
30
}
28
31
29
- public static TelemetryClient buildIntakeClient (Config config ) {
32
+ public static TelemetryClient buildIntakeClient (
33
+ Config config , HttpRetryPolicy .Factory httpRetryPolicy ) {
30
34
String apiKey = config .getApiKey ();
31
35
if (apiKey == null ) {
32
36
log .debug ("Cannot create Telemetry Intake because DD_API_KEY unspecified." );
@@ -44,7 +48,7 @@ public static TelemetryClient buildIntakeClient(Config config) {
44
48
45
49
long timeoutMillis = TimeUnit .SECONDS .toMillis (config .getAgentTimeout ());
46
50
OkHttpClient httpClient = OkHttpUtils .buildHttpClient (url , timeoutMillis );
47
- return new TelemetryClient (httpClient , url , apiKey );
51
+ return new TelemetryClient (httpClient , httpRetryPolicy , url , apiKey );
48
52
}
49
53
50
54
private static String buildIntakeTelemetryUrl (Config config ) {
@@ -71,11 +75,17 @@ private static String buildIntakeTelemetryUrl(Config config) {
71
75
private static final String DD_TELEMETRY_REQUEST_TYPE = "DD-Telemetry-Request-Type" ;
72
76
73
77
private final OkHttpClient okHttpClient ;
78
+ private final HttpRetryPolicy .Factory httpRetryPolicy ;
74
79
private final HttpUrl url ;
75
80
private final String apiKey ;
76
81
77
- public TelemetryClient (OkHttpClient okHttpClient , HttpUrl url , String apiKey ) {
82
+ public TelemetryClient (
83
+ OkHttpClient okHttpClient ,
84
+ HttpRetryPolicy .Factory httpRetryPolicy ,
85
+ HttpUrl url ,
86
+ String apiKey ) {
78
87
this .okHttpClient = okHttpClient ;
88
+ this .httpRetryPolicy = httpRetryPolicy ;
79
89
this .url = url ;
80
90
this .apiKey = apiKey ;
81
91
}
@@ -92,7 +102,9 @@ public Result sendHttpRequest(Request.Builder httpRequestBuilder) {
92
102
93
103
Request httpRequest = httpRequestBuilder .build ();
94
104
String requestType = httpRequest .header (DD_TELEMETRY_REQUEST_TYPE );
95
- try (Response response = okHttpClient .newCall (httpRequest ).execute ()) {
105
+
106
+ try (okhttp3 .Response response =
107
+ OkHttpUtils .sendWithRetries (okHttpClient , httpRetryPolicy , httpRequest )) {
96
108
if (response .code () == 404 ) {
97
109
log .debug ("Telemetry endpoint is disabled, dropping {} message." , requestType );
98
110
return Result .NOT_FOUND ;
@@ -105,6 +117,10 @@ public Result sendHttpRequest(Request.Builder httpRequestBuilder) {
105
117
response .message ());
106
118
return Result .FAILURE ;
107
119
}
120
+ } catch (InterruptedIOException e ) {
121
+ log .debug ("Telemetry message {} sending interrupted: {}." , requestType , e .toString ());
122
+ return Result .INTERRUPTED ;
123
+
108
124
} catch (IOException e ) {
109
125
log .debug ("Telemetry message {} failed with exception: {}." , requestType , e .toString ());
110
126
return Result .FAILURE ;
0 commit comments