Added writer, serialisation, API + changed some types
This commit is contained in:
parent
af244a039d
commit
1a981bb9f3
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>raclette-java</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
93
pom.xml
93
pom.xml
|
@ -1,51 +1,56 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.datadog</groupId>
|
||||
<artifactId>raclette-java</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<groupId>com.datadog</groupId>
|
||||
<artifactId>raclette-java</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.opentracing</groupId>
|
||||
<artifactId>opentracing-api</artifactId>
|
||||
<version>0.21.0</version>
|
||||
</dependency>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.opentracing</groupId>
|
||||
<artifactId>opentracing-api</artifactId>
|
||||
<version>0.21.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.8.8</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Testing dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.6.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>2.7.22</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<!-- Testing dependencies -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.6.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>2.7.22</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -3,7 +3,7 @@ package com.datadoghq.trace;
|
|||
|
||||
import com.datadoghq.trace.impl.Span;
|
||||
|
||||
public interface ISampler {
|
||||
public interface Sampler {
|
||||
|
||||
public boolean sample(Span span);
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.datadoghq.trace;
|
||||
|
||||
import io.opentracing.Span;
|
||||
|
||||
public interface SpanSerializer {
|
||||
|
||||
public String serialize(Span t) throws Exception;
|
||||
|
||||
public Span deserialize(String str) throws Exception;
|
||||
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
package com.datadoghq.trace;
|
||||
|
||||
import com.datadoghq.trace.impl.Span;
|
||||
import io.opentracing.Span;
|
||||
|
||||
public interface IWriter {
|
||||
public interface Writer {
|
||||
|
||||
public void write(Span span);
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package com.datadoghq.trace.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.datadoghq.trace.SpanSerializer;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class DDSpanSerializer implements SpanSerializer {
|
||||
|
||||
protected final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public String serialize(io.opentracing.Span t) throws JsonProcessingException {
|
||||
return objectMapper.writeValueAsString(t);
|
||||
}
|
||||
|
||||
public io.opentracing.Span deserialize(String str) throws JsonParseException, JsonMappingException, IOException {
|
||||
return objectMapper.readValue(str, Span.class);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception{
|
||||
|
||||
Tracer tracer = new Tracer();
|
||||
io.opentracing.Span span = tracer.buildSpan("Hello!")
|
||||
.withTag("port", 1234)
|
||||
.withTag("bool", true)
|
||||
.withTag("hello", "world")
|
||||
.start();
|
||||
DDSpanSerializer ddSpanSerializer = new DDSpanSerializer();
|
||||
|
||||
System.out.println(ddSpanSerializer.serialize(span));
|
||||
|
||||
}
|
||||
}
|
|
@ -13,14 +13,14 @@ public class Span implements io.opentracing.Span {
|
|||
private Map<String, Object> tags;
|
||||
private long startTime;
|
||||
private long durationMilliseconds;
|
||||
private final SpanContext context;
|
||||
private final com.datadoghq.trace.impl.SpanContext context;
|
||||
|
||||
Span(
|
||||
Tracer tracer,
|
||||
String operationName,
|
||||
Map<String, Object> tags,
|
||||
Optional<Long> timestamp,
|
||||
SpanContext context) {
|
||||
com.datadoghq.trace.impl.SpanContext context) {
|
||||
this.tracer = tracer;
|
||||
this.operationName = operationName;
|
||||
this.tags = tags;
|
||||
|
@ -104,4 +104,8 @@ public class Span implements io.opentracing.Span {
|
|||
return startTime;
|
||||
}
|
||||
|
||||
public com.datadoghq.trace.impl.SpanContext getContext(){
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,16 +27,16 @@ public class Tracer implements io.opentracing.Tracer {
|
|||
class SpanBuilder implements io.opentracing.Tracer.SpanBuilder {
|
||||
|
||||
private final String operationName;
|
||||
private Map<String, Object> tags = new HashMap();
|
||||
private Map<String, Object> tags = new HashMap<String, Object>();
|
||||
private Long timestamp;
|
||||
private Optional<SpanContext> parent = Optional.empty();
|
||||
private Optional<com.datadoghq.trace.impl.SpanContext> parent = Optional.empty();
|
||||
|
||||
public SpanBuilder(String operationName) {
|
||||
this.operationName = operationName;
|
||||
}
|
||||
|
||||
public io.opentracing.Tracer.SpanBuilder asChildOf(SpanContext spanContext) {
|
||||
this.parent = Optional.ofNullable(spanContext);
|
||||
this.parent = Optional.ofNullable((com.datadoghq.trace.impl.SpanContext)spanContext);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -75,7 +75,7 @@ public class Tracer implements io.opentracing.Tracer {
|
|||
public Span start() {
|
||||
|
||||
// build the context
|
||||
SpanContext context = buildTheSpanContext();
|
||||
com.datadoghq.trace.impl.SpanContext context = buildTheSpanContext();
|
||||
|
||||
return new com.datadoghq.trace.impl.Span(
|
||||
Tracer.this,
|
||||
|
@ -85,13 +85,13 @@ public class Tracer implements io.opentracing.Tracer {
|
|||
context);
|
||||
}
|
||||
|
||||
private SpanContext buildTheSpanContext() {
|
||||
private com.datadoghq.trace.impl.SpanContext buildTheSpanContext() {
|
||||
|
||||
SpanContext context = null;
|
||||
com.datadoghq.trace.impl.SpanContext context = null;
|
||||
|
||||
long generatedId = generateNewId();
|
||||
if (parent.isPresent()) {
|
||||
com.datadoghq.trace.impl.SpanContext p = (com.datadoghq.trace.impl.SpanContext) parent.get();
|
||||
com.datadoghq.trace.impl.SpanContext p = parent.get();
|
||||
context = new com.datadoghq.trace.impl.SpanContext(
|
||||
p.getTraceId(),
|
||||
generatedId,
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
package com.datadoghq.trace.writer.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
import com.datadoghq.trace.Writer;
|
||||
|
||||
import io.opentracing.Span;
|
||||
|
||||
public class DDAgentWriter implements Writer {
|
||||
|
||||
protected static final String DEFAULT_HOSTNAME = "localhost";
|
||||
protected static final int DEFAULT_PORT = 8126;
|
||||
|
||||
protected static final int DEFAULT_MAX_TRACES = 1000;
|
||||
protected static final int DEFAULT_BATCH_SIZE = 10;
|
||||
protected static final int DEFAULT_MAX_SERVICES = 1000;
|
||||
protected static final long DEFAULT_TIMEOUT = 5000;
|
||||
|
||||
protected final BlockingQueue<Span> commandQueue;
|
||||
protected final Thread asyncWriterThread;
|
||||
|
||||
public DDAgentWriter() {
|
||||
super();
|
||||
commandQueue = new ArrayBlockingQueue<Span>(DEFAULT_MAX_TRACES);
|
||||
|
||||
asyncWriterThread = new Thread(new SpansSendingTask(), "dd.DDAgentWriter-SpansSendingTask");
|
||||
asyncWriterThread.setDaemon(true);
|
||||
asyncWriterThread.start();
|
||||
}
|
||||
|
||||
public void write(Span span) {
|
||||
try{
|
||||
//Try to add a new span in the queue
|
||||
commandQueue.add(span);
|
||||
}catch(IllegalStateException e){
|
||||
//It was not possible to add the span the queue is full!
|
||||
//FIXME proper logging
|
||||
System.out.println("Cannot add the following span as the async queue is full: "+span);
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
asyncWriterThread.interrupt();
|
||||
}
|
||||
|
||||
protected class SpansSendingTask implements Runnable {
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
//Wait until a new span comes
|
||||
Span span = commandQueue.take();
|
||||
|
||||
//Drain all spans up to a certain batch suze
|
||||
List<Span> spans = new ArrayList<Span>();
|
||||
spans.add(span);
|
||||
commandQueue.drainTo(spans, DEFAULT_BATCH_SIZE);
|
||||
|
||||
//Then write to the agent
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
// FIXME proper logging
|
||||
e.printStackTrace();
|
||||
|
||||
//The thread was interrupted, we break the LOOP
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.datadoghq.trace.writer.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
import io.opentracing.Span;
|
||||
|
||||
public class DDApi {
|
||||
|
||||
protected static final String TRACES_ENDPOINT = "/v0.3/traces";
|
||||
protected static final String TRACES_SERVICES = "/v0.3/services";
|
||||
|
||||
protected final String host;
|
||||
protected final int port;
|
||||
protected final String tracesEndpoint;
|
||||
protected final String servicesEndpoint;
|
||||
|
||||
public DDApi(String host, int port) {
|
||||
super();
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.tracesEndpoint = "http://"+host+":"+port+TRACES_ENDPOINT;
|
||||
this.servicesEndpoint = "http://"+host+":"+port+TRACES_SERVICES;
|
||||
}
|
||||
|
||||
public void sendSpans(List<Span> spans){
|
||||
|
||||
}
|
||||
|
||||
public void sendServices(List<String> services){
|
||||
|
||||
}
|
||||
|
||||
private int callPUT(String endpoint,String content){
|
||||
try {
|
||||
URL url = new URL(tracesEndpoint);
|
||||
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
|
||||
httpCon.setDoOutput(true);
|
||||
httpCon.setRequestMethod("PUT");
|
||||
httpCon.setRequestProperty("Content-Type", "application/json");
|
||||
OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
|
||||
out.write("Resource content");
|
||||
out.close();
|
||||
return httpCon.getResponseCode();
|
||||
} catch (MalformedURLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return -1;
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue