Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ gather all traces in your [Datadog](https://app.datadoghq.com) account.
* [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.
* [Install the Datadog Java agent](dd-java-agent). Instructions for supported technologies, web-servers and frameworks.
* [Browse examples](dd-trace-examples). See how to instrument legacy projects based on the most used tehcnologies.
* [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.
* [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.
* [DD Trace](dd-trace). This Java implementation of the opentracing api is used to report traces to Datadog.

### Help or questions?

Expand Down
18 changes: 18 additions & 0 deletions dd-trace/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@
<version>2.7.22</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.ratpack</groupId>
<artifactId>ratpack-groovy-test</artifactId>
<version>1.4.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
23 changes: 23 additions & 0 deletions dd-trace/src/test/groovy/com/datadog/trace/SpanFactory.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.datadog.trace

import com.datadoghq.trace.DDSpan
import com.datadoghq.trace.DDSpanContext

class SpanFactory {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitively a great idea :)

static def newSpanOf(long timestampMicro) {
def context = new DDSpanContext(
1L,
1L,
0L,
"fakeService",
"fakeOperation",
"fakeResource",
Collections.emptyMap(),
false,
"fakeType",
Collections.emptyMap(),
null,
null);
return new DDSpan(timestampMicro, context)
}
}
117 changes: 117 additions & 0 deletions dd-trace/src/test/groovy/com/datadoghq/trace/writer/DDApiTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.datadoghq.trace.writer

import com.datadog.trace.SpanFactory
import com.datadoghq.trace.DDSpan
import com.fasterxml.jackson.databind.ObjectMapper
import ratpack.http.MediaType
import spock.lang.AutoCleanup
import spock.lang.Specification

import java.util.concurrent.atomic.AtomicReference

import static ratpack.groovy.test.embed.GroovyEmbeddedApp.ratpack
import static ratpack.http.MediaType.APPLICATION_JSON

class DDApiTest extends Specification {
static def mapper = new ObjectMapper()

def "sending an empty list of traces returns no errors"() {
setup:
def agent = ratpack {
handlers {
put("v0.3/traces") {
response.status(200).send()
}
}
}
def client = new DDApi("localhost", agent.address.port)

expect:
client.sendTraces([])

cleanup:
agent.close()
}

def "non-200 response results in false returned"() {
setup:
def agent = ratpack {
handlers {
put("v0.3/traces") {
response.status(404).send()
}
}
}
def client = new DDApi("localhost", agent.address.port)

expect:
!client.sendTraces([])

cleanup:
agent.close()
}

def "content is sent as JSON"() {
setup:
def requestContentType = new AtomicReference<MediaType>()
def requestBody = new AtomicReference<String>()
def agent = ratpack {
handlers {
put("v0.3/traces") {
requestContentType.set(request.contentType)
request.body.then {
requestBody.set(it.text)
response.send()
}
}
}
}
def client = new DDApi("localhost", agent.address.port)

expect:
client.sendTraces(traces)
requestContentType.get().type == APPLICATION_JSON
areEqual(requestBody.get(), expectedRequestBody)

cleanup:
agent.close()

where:
traces | expectedRequestBody
[] | '[]'
[SpanFactory.newSpanOf(1L)] | '''[{
"duration":0,
"error":0,
"meta":{"thread-name":"main","thread-id":"1"},
"name":"fakeOperation",
"parent_id":0,
"resource":"fakeResource"
"service":"fakeService",
"span_id":1,
"start":1000,
"trace_id":1,
"type":"fakeType",
}]'''
[SpanFactory.newSpanOf(100L)] | '''[{
"duration":0,
"error":0,
"meta":{"thread-name":"main","thread-id":"1"},
"name":"fakeOperation",
"parent_id":0,
"resource":"fakeResource"
"service":"fakeService",
"span_id":1,
"start":100000,
"trace_id":1,
"type":"fakeType",
}]'''
}


static void areEqual(String json1, String json2) {
def tree1 = mapper.readTree json1
def tree2 = mapper.readTree json2

assert tree1.equals(tree2)
}
}