Skip to content

Commit 70e43a1

Browse files
authored
Merge pull request #31 from DataDog/tyler/testing
Add deeper tests for DDApi using ratpack as a mock http server.
2 parents 1a1767c + b521276 commit 70e43a1

File tree

4 files changed

+160
-1
lines changed

4 files changed

+160
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ gather all traces in your [Datadog](https://app.datadoghq.com) account.
1616
* [Introduction to the Datadog APM](https://www.datadoghq.com/apm/). Learn what you can do with the Next-Gen APM and how to get started.
1717
* [Install the Datadog Java agent](dd-java-agent). Instructions for supported technologies, web-servers and frameworks.
1818
* [Browse examples](dd-trace-examples). See how to instrument legacy projects based on the most used tehcnologies.
19-
* [DD Trace API](dd-trace). We choose to embrace the Opentracting initiative. So feel free to use the Trace Java API to customize your instrumentation.
19+
* [Instrument with OpenTracing](https://github.com/opentracing/opentracing-java). Datadog embraces the OpenTracing initiative. So feel free to use the Trace Java API to customize your instrumentation.
20+
* [DD Trace](dd-trace). This Java implementation of the opentracing api is used to report traces to Datadog.
2021

2122
### Help or questions?
2223

dd-trace/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@
8888
<version>2.7.22</version>
8989
<scope>test</scope>
9090
</dependency>
91+
<dependency>
92+
<groupId>org.spockframework</groupId>
93+
<artifactId>spock-core</artifactId>
94+
<version>1.0-groovy-2.4</version>
95+
<scope>test</scope>
96+
</dependency>
97+
<dependency>
98+
<groupId>org.codehaus.groovy</groupId>
99+
<artifactId>groovy-all</artifactId>
100+
<version>2.4.4</version>
101+
<scope>test</scope>
102+
</dependency>
103+
<dependency>
104+
<groupId>io.ratpack</groupId>
105+
<artifactId>ratpack-groovy-test</artifactId>
106+
<version>1.4.6</version>
107+
<scope>test</scope>
108+
</dependency>
91109
</dependencies>
92110

93111
<build>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.datadog.trace
2+
3+
import com.datadoghq.trace.DDSpan
4+
import com.datadoghq.trace.DDSpanContext
5+
6+
class SpanFactory {
7+
static def newSpanOf(long timestampMicro) {
8+
def context = new DDSpanContext(
9+
1L,
10+
1L,
11+
0L,
12+
"fakeService",
13+
"fakeOperation",
14+
"fakeResource",
15+
Collections.emptyMap(),
16+
false,
17+
"fakeType",
18+
Collections.emptyMap(),
19+
null,
20+
null);
21+
return new DDSpan(timestampMicro, context)
22+
}
23+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.datadoghq.trace.writer
2+
3+
import com.datadog.trace.SpanFactory
4+
import com.datadoghq.trace.DDSpan
5+
import com.fasterxml.jackson.databind.ObjectMapper
6+
import ratpack.http.MediaType
7+
import spock.lang.AutoCleanup
8+
import spock.lang.Specification
9+
10+
import java.util.concurrent.atomic.AtomicReference
11+
12+
import static ratpack.groovy.test.embed.GroovyEmbeddedApp.ratpack
13+
import static ratpack.http.MediaType.APPLICATION_JSON
14+
15+
class DDApiTest extends Specification {
16+
static def mapper = new ObjectMapper()
17+
18+
def "sending an empty list of traces returns no errors"() {
19+
setup:
20+
def agent = ratpack {
21+
handlers {
22+
put("v0.3/traces") {
23+
response.status(200).send()
24+
}
25+
}
26+
}
27+
def client = new DDApi("localhost", agent.address.port)
28+
29+
expect:
30+
client.sendTraces([])
31+
32+
cleanup:
33+
agent.close()
34+
}
35+
36+
def "non-200 response results in false returned"() {
37+
setup:
38+
def agent = ratpack {
39+
handlers {
40+
put("v0.3/traces") {
41+
response.status(404).send()
42+
}
43+
}
44+
}
45+
def client = new DDApi("localhost", agent.address.port)
46+
47+
expect:
48+
!client.sendTraces([])
49+
50+
cleanup:
51+
agent.close()
52+
}
53+
54+
def "content is sent as JSON"() {
55+
setup:
56+
def requestContentType = new AtomicReference<MediaType>()
57+
def requestBody = new AtomicReference<String>()
58+
def agent = ratpack {
59+
handlers {
60+
put("v0.3/traces") {
61+
requestContentType.set(request.contentType)
62+
request.body.then {
63+
requestBody.set(it.text)
64+
response.send()
65+
}
66+
}
67+
}
68+
}
69+
def client = new DDApi("localhost", agent.address.port)
70+
71+
expect:
72+
client.sendTraces(traces)
73+
requestContentType.get().type == APPLICATION_JSON
74+
areEqual(requestBody.get(), expectedRequestBody)
75+
76+
cleanup:
77+
agent.close()
78+
79+
where:
80+
traces | expectedRequestBody
81+
[] | '[]'
82+
[SpanFactory.newSpanOf(1L)] | '''[{
83+
"duration":0,
84+
"error":0,
85+
"meta":{"thread-name":"main","thread-id":"1"},
86+
"name":"fakeOperation",
87+
"parent_id":0,
88+
"resource":"fakeResource"
89+
"service":"fakeService",
90+
"span_id":1,
91+
"start":1000,
92+
"trace_id":1,
93+
"type":"fakeType",
94+
}]'''
95+
[SpanFactory.newSpanOf(100L)] | '''[{
96+
"duration":0,
97+
"error":0,
98+
"meta":{"thread-name":"main","thread-id":"1"},
99+
"name":"fakeOperation",
100+
"parent_id":0,
101+
"resource":"fakeResource"
102+
"service":"fakeService",
103+
"span_id":1,
104+
"start":100000,
105+
"trace_id":1,
106+
"type":"fakeType",
107+
}]'''
108+
}
109+
110+
111+
static void areEqual(String json1, String json2) {
112+
def tree1 = mapper.readTree json1
113+
def tree2 = mapper.readTree json2
114+
115+
assert tree1.equals(tree2)
116+
}
117+
}

0 commit comments

Comments
 (0)