Skip to content

Commit 1100461

Browse files
authored
Merge pull request #783 from DataDog/tyler/fix-content-length
Fix content length calculation for larger trace counts
2 parents 722e6c4 + 2052647 commit 1100461

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

dd-trace-ot/src/main/java/datadog/trace/common/writer/DDApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ public long contentLength() {
122122
if (traceCount < (1 << 4)) {
123123
return sizeInBytes + 1; // byte
124124
} else if (traceCount < (1 << 16)) {
125-
return sizeInBytes + 2; // short
125+
return sizeInBytes + 3; // byte + short
126126
} else {
127-
return sizeInBytes + 4; // int
127+
return sizeInBytes + 5; // byte + int
128128
}
129129
}
130130

dd-trace-ot/src/test/groovy/datadog/trace/api/writer/DDApiTest.groovy

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import datadog.trace.common.writer.DDApi
77
import datadog.trace.common.writer.DDApi.ResponseListener
88
import spock.lang.Specification
99

10+
import java.util.concurrent.atomic.AtomicLong
1011
import java.util.concurrent.atomic.AtomicReference
1112

1213
import static datadog.trace.agent.test.server.http.TestHttpServer.httpServer
@@ -199,6 +200,38 @@ class DDApiTest extends Specification {
199200
"v0.3" | 30000 | false
200201
}
201202

203+
def "verify content length"() {
204+
setup:
205+
def receivedContentLength = new AtomicLong()
206+
def agent = httpServer {
207+
handlers {
208+
put("v0.4/traces") {
209+
receivedContentLength.set(request.contentLength)
210+
response.status(200).send()
211+
}
212+
}
213+
}
214+
def client = new DDApi("localhost", agent.address.port, null)
215+
216+
when:
217+
def success = client.sendTraces(traces)
218+
then:
219+
success
220+
receivedContentLength.get() == expectedLength
221+
222+
cleanup:
223+
agent.close()
224+
225+
where:
226+
expectedLength | traces
227+
1 | []
228+
3 | [[], []]
229+
16 | (1..15).collect { [] }
230+
19 | (1..16).collect { [] }
231+
65538 | (1..((1 << 16) - 1)).collect { [] }
232+
65541 | (1..(1 << 16)).collect { [] }
233+
}
234+
202235
static List<List<TreeMap<String, Object>>> convertList(byte[] bytes) {
203236
return mapper.readValue(bytes, new TypeReference<List<List<TreeMap<String, Object>>>>() {})
204237
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ class DDApiIntegrationTest {
134134
[[], []] | 2
135135
[[new DDSpan(1, CONTEXT)]] | 3
136136
[[new DDSpan(TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis()), CONTEXT)]] | 4
137+
(1..15).collect { [] } | 5
138+
(1..16).collect { [] } | 6
139+
// Larger traces take more than 1 second to send to the agent and get a timeout exception:
140+
// (1..((1 << 16) - 1)).collect { [] } | 7
141+
// (1..(1 << 16)).collect { [] } | 8
137142
}
138143

139144
def "Sending traces to unix domain socket succeeds (test #test)"() {

dd-trace/src/test/groovy/datadog/trace/tracer/writer/AgentWriterTest.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package datadog.trace.tracer.writer
22

33
import datadog.trace.tracer.Trace
4+
import spock.lang.Retry
45
import spock.lang.Specification
56

67
import java.util.concurrent.ExecutorService
@@ -138,6 +139,7 @@ class AgentWriterTest extends Specification {
138139
sampleRateByService == SampleRateByService.EMPTY_INSTANCE
139140
}
140141

142+
@Retry
141143
def "test start/#closeMethod"() {
142144
setup:
143145
def writer = new AgentWriter(client)
@@ -178,6 +180,7 @@ class AgentWriterTest extends Specification {
178180
}
179181

180182
boolean isWriterThreadRunning() {
183+
// This is known to fail sometimes.
181184
return Thread.getAllStackTraces().keySet().any { t -> t.getName() == "dd-agent-writer" }
182185
}
183186
}

0 commit comments

Comments
 (0)