Skip to content

Commit 94b8d95

Browse files
fix(core): Port remote API response to Java 8+ (#9603)
1 parent c442de6 commit 94b8d95

File tree

9 files changed

+50
-39
lines changed

9 files changed

+50
-39
lines changed

dd-trace-core/src/main/java/datadog/trace/common/writer/RemoteApi.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import datadog.trace.relocate.api.IOLogger;
44
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
55
import java.io.IOException;
6+
import java.util.Optional;
7+
import java.util.OptionalInt;
68
import org.slf4j.Logger;
79

810
public abstract class RemoteApi {
@@ -141,21 +143,19 @@ private Response(
141143
this.response = response;
142144
}
143145

144-
public final boolean success() {
146+
public boolean success() {
145147
return success;
146148
}
147149

148-
// TODO: DQH - In Java 8, switch to OptionalInteger
149-
public final Integer status() {
150-
return status;
150+
public OptionalInt status() {
151+
return status == null ? OptionalInt.empty() : OptionalInt.of(status);
151152
}
152153

153-
// TODO: DQH - In Java 8, switch to Optional<Throwable>?
154-
public final Throwable exception() {
155-
return exception;
154+
public Optional<Throwable> exception() {
155+
return Optional.ofNullable(exception);
156156
}
157157

158-
public final String response() {
158+
public String response() {
159159
return response;
160160
}
161161
}

dd-trace-core/src/main/java/datadog/trace/core/monitor/TracerHealthMetrics.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,14 @@ private void onSendAttempt(
308308
// TODO: missing queue.spans (# of spans being sent)
309309
flushedBytes.add(sizeInBytes);
310310

311-
if (response.exception() != null) {
311+
if (response.exception().isPresent()) {
312312
// covers communication errors -- both not receiving a response or
313313
// receiving malformed response (even when otherwise successful)
314314
apiErrors.increment();
315315
}
316316

317-
Integer status = response.status();
318-
if (status != null) {
317+
int status = response.status().orElse(0);
318+
if (status != 0) {
319319
if (200 == status) {
320320
apiResponsesOK.increment();
321321
} else {

dd-trace-core/src/test/groovy/datadog/trace/common/writer/DDAgentApiTest.groovy

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ class DDAgentApiTest extends DDCoreSpecification {
6868
expect:
6969
def response = client.sendSerializedTraces(payload)
7070
response.success()
71-
response.status() == 200
71+
response.status().present
72+
response.status().asInt == 200
7273
agent.getLastRequest().path == "/" + agentVersion
7374

7475
cleanup:
@@ -96,7 +97,8 @@ class DDAgentApiTest extends DDCoreSpecification {
9697
expect:
9798
def clientResponse = client.sendSerializedTraces(payload)
9899
!clientResponse.success()
99-
clientResponse.status() == 404
100+
clientResponse.status().present
101+
clientResponse.status().asInt == 404
100102
agent.getLastRequest().path == "/v0.3/traces"
101103

102104
cleanup:

dd-trace-core/src/test/groovy/datadog/trace/common/writer/DDAgentWriterCombinedTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ class DDAgentWriterCombinedTest extends DDCoreSpecification {
335335
1 * healthMetrics.onPublish(minimalTrace, _)
336336
1 * healthMetrics.onSerialize(_)
337337
1 * healthMetrics.onFlush(false)
338-
1 * healthMetrics.onSend(1, _, { response -> response.success() && response.status() == 200 })
338+
1 * healthMetrics.onSend(1, _, { response -> response.success() && response.status().present && response.status().asInt == 200 })
339339

340340
when:
341341
writer.close()
@@ -394,7 +394,7 @@ class DDAgentWriterCombinedTest extends DDCoreSpecification {
394394
1 * healthMetrics.onPublish(minimalTrace, _)
395395
1 * healthMetrics.onSerialize(_)
396396
1 * healthMetrics.onFlush(false)
397-
1 * healthMetrics.onFailedSend(1, _, { response -> !response.success() && response.status() == 500 })
397+
1 * healthMetrics.onFailedSend(1, _, { response -> !response.success() && response.status().present && response.status().asInt == 500 })
398398

399399
when:
400400
writer.close()

dd-trace-core/src/test/groovy/datadog/trace/common/writer/DDIntakeWriterCombinedTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class DDIntakeWriterCombinedTest extends DDCoreSpecification {
297297
1 * healthMetrics.onPublish(minimalTrace, _)
298298
1 * healthMetrics.onSerialize(_)
299299
1 * healthMetrics.onFlush(false)
300-
1 * healthMetrics.onSend(1, _, { response -> response.success() && response.status() == 200 })
300+
1 * healthMetrics.onSend(1, _, { response -> response.success() && response.status().present && response.status().asInt == 200 })
301301

302302
when:
303303
writer.close()
@@ -355,7 +355,7 @@ class DDIntakeWriterCombinedTest extends DDCoreSpecification {
355355
1 * healthMetrics.onPublish(minimalTrace, _)
356356
1 * healthMetrics.onSerialize(_)
357357
1 * healthMetrics.onFlush(false)
358-
1 * healthMetrics.onFailedSend(1, _, { response -> !response.success() && response.status() == 500 })
358+
1 * healthMetrics.onFailedSend(1, _, { response -> !response.success() && response.status().present && response.status().asInt == 500 })
359359

360360
when:
361361
writer.close()

dd-trace-core/src/test/groovy/datadog/trace/common/writer/ddintake/DDEvpProxyApiTest.groovy

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class DDEvpProxyApiTest extends DDCoreSpecification {
6060
expect:
6161
def clientResponse = client.sendSerializedTraces(payload)
6262
clientResponse.success()
63-
clientResponse.status() == 200
63+
clientResponse.status().present
64+
clientResponse.status().asInt == 200
6465
agentEvpProxy.getLastRequest().path == path
6566
agentEvpProxy.getLastRequest().getHeader(DDEvpProxyApi.DD_EVP_SUBDOMAIN_HEADER) == intakeSubdomain
6667

@@ -95,7 +96,8 @@ class DDEvpProxyApiTest extends DDCoreSpecification {
9596
expect:
9697
def clientResponse = client.sendSerializedTraces(payload)
9798
clientResponse.success()
98-
clientResponse.status() == 200
99+
clientResponse.status().present
100+
clientResponse.status().asInt == 200
99101
agentEvpProxy.getLastRequest().path == path
100102
agentEvpProxy.getLastRequest().getHeader(DDEvpProxyApi.DD_EVP_SUBDOMAIN_HEADER) == intakeSubdomain
101103

dd-trace-core/src/test/groovy/datadog/trace/common/writer/ddintake/DDIntakeApiTest.groovy

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class DDIntakeApiTest extends DDCoreSpecification {
5858
expect:
5959
def clientResponse = client.sendSerializedTraces(payload)
6060
clientResponse.success()
61-
clientResponse.status() == 200
61+
clientResponse.status().present
62+
clientResponse.status().asInt == 200
6263
intake.getLastRequest().path == path
6364

6465
cleanup:
@@ -92,7 +93,8 @@ class DDIntakeApiTest extends DDCoreSpecification {
9293
expect:
9394
def clientResponse = client.sendSerializedTraces(payload)
9495
clientResponse.success()
95-
clientResponse.status() == 200
96+
clientResponse.status().present
97+
clientResponse.status().asInt == 200
9698
intake.getLastRequest().path == path
9799

98100
cleanup:
@@ -126,7 +128,8 @@ class DDIntakeApiTest extends DDCoreSpecification {
126128
expect:
127129
def clientResponse = client.sendSerializedTraces(payload)
128130
clientResponse.success()
129-
clientResponse.status() == 200
131+
clientResponse.status().present
132+
clientResponse.status().asInt == 200
130133
intake.getLastRequest().path == path
131134

132135
cleanup:
@@ -152,7 +155,7 @@ class DDIntakeApiTest extends DDCoreSpecification {
152155
def payload = prepareTraces(trackType, traces)
153156

154157
expect:
155-
client.sendSerializedTraces(payload).status()
158+
client.sendSerializedTraces(payload).status().present
156159
intake.lastRequest.contentType == "application/msgpack"
157160
convertMap(intake.lastRequest.body) == expectedRequestBody
158161

dd-trace-core/src/test/groovy/datadog/trace/core/monitor/HealthMetricsTest.groovy

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class HealthMetricsTest extends Specification {
172172

173173
def "test onSend #iterationIndex"() {
174174
setup:
175-
def latch = new CountDownLatch(3 + (response.exception() ? 1 : 0) + (response.status() ? 1 : 0))
175+
def latch = new CountDownLatch(3 + (response.exception().present ? 1 : 0) + (response.status().present ? 1 : 0))
176176
def healthMetrics = new TracerHealthMetrics(new Latched(statsD, latch), 100, TimeUnit.MILLISECONDS)
177177
healthMetrics.start()
178178

@@ -184,11 +184,11 @@ class HealthMetricsTest extends Specification {
184184
1 * statsD.count('api.requests.total', 1)
185185
1 * statsD.count('flush.traces.total', traceCount)
186186
1 * statsD.count('flush.bytes.total', sendSize)
187-
if (response.exception()) {
187+
if (response.exception().present) {
188188
1 * statsD.count('api.errors.total', 1)
189189
}
190-
if (response.status()) {
191-
1 * statsD.incrementCounter('api.responses.total', ["status:${response.status()}"])
190+
if (response.status().present) {
191+
1 * statsD.incrementCounter('api.responses.total', ["status:${response.status().asInt}"])
192192
}
193193
0 * _
194194

@@ -209,7 +209,7 @@ class HealthMetricsTest extends Specification {
209209

210210
def "test onFailedSend #iterationIndex"() {
211211
setup:
212-
def latch = new CountDownLatch(3 + (response.exception() ? 1 : 0) + (response.status() ? 1 : 0))
212+
def latch = new CountDownLatch(3 + (response.exception().present ? 1 : 0) + (response.status().present ? 1 : 0))
213213
def healthMetrics = new TracerHealthMetrics(new Latched(statsD, latch), 100, TimeUnit.MILLISECONDS)
214214
healthMetrics.start()
215215

@@ -221,11 +221,11 @@ class HealthMetricsTest extends Specification {
221221
1 * statsD.count('api.requests.total', 1)
222222
1 * statsD.count('flush.traces.total', traceCount)
223223
1 * statsD.count('flush.bytes.total', sendSize)
224-
if (response.exception()) {
224+
if (response.exception().present) {
225225
1 * statsD.count('api.errors.total', 1)
226226
}
227-
if (response.status()) {
228-
1 * statsD.incrementCounter('api.responses.total', ["status:${response.status()}"])
227+
if (response.status().present) {
228+
1 * statsD.incrementCounter('api.responses.total', ["status:${response.status().asInt}"])
229229
}
230230
0 * _
231231

dd-trace-core/src/traceAgentTest/groovy/DDApiIntegrationTest.groovy

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ class DDApiIntegrationTest extends AbstractTraceAgentTest {
9797
expect:
9898
RemoteApi.Response response = api.sendSerializedTraces(prepareRequest(traces, mapper))
9999
assert !response.response().isEmpty()
100-
assert null == response.exception()
101-
assert 200 == response.status()
100+
assert !response.exception().present
101+
assert response.status().present
102+
assert 200 == response.status().asInt
102103
assert response.success()
103104
assert discovery.getTraceEndpoint() == "${version}/traces"
104105
assert endpoint.get() == "${Config.get().getAgentUrl()}/${version}/traces"
@@ -120,8 +121,9 @@ class DDApiIntegrationTest extends AbstractTraceAgentTest {
120121
expect:
121122
RemoteApi.Response response = api.sendSerializedTraces(prepareRequest([[span]], mapper))
122123
assert !response.response().isEmpty()
123-
assert null == response.exception()
124-
assert 200 == response.status()
124+
assert !response.exception().present
125+
assert response.status().present
126+
assert 200 == response.status().asInt
125127
assert response.success()
126128
assert discovery.getTraceEndpoint() == "${version}/traces"
127129
assert endpoint.get() == "${Config.get().getAgentUrl()}/${version}/traces"
@@ -137,8 +139,9 @@ class DDApiIntegrationTest extends AbstractTraceAgentTest {
137139
expect:
138140
RemoteApi.Response response = unixDomainSocketApi.sendSerializedTraces(prepareRequest(traces, mapper))
139141
assert !response.response().isEmpty()
140-
assert null == response.exception()
141-
assert 200 == response.status()
142+
assert !response.exception().present
143+
assert response.status().present
144+
assert 200 == response.status().asInt
142145
assert response.success()
143146
assert udsDiscovery.getTraceEndpoint() == "${version}/traces"
144147
assert endpoint.get() == "http://${SOMEHOST}:${SOMEPORT}/${version}/traces"
@@ -158,8 +161,9 @@ class DDApiIntegrationTest extends AbstractTraceAgentTest {
158161
expect:
159162
RemoteApi.Response response = unixDomainSocketApi.sendSerializedTraces(prepareRequest([[span]], mapper))
160163
assert !response.response().isEmpty()
161-
assert null == response.exception()
162-
assert 200 == response.status()
164+
assert !response.exception().present
165+
assert response.status().present
166+
assert 200 == response.status().asInt
163167
assert response.success()
164168
assert udsDiscovery.getTraceEndpoint() == "${version}/traces"
165169
assert endpoint.get() == "http://${SOMEHOST}:${SOMEPORT}/${version}/traces"

0 commit comments

Comments
 (0)