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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion dd-trace-ot/dd-trace-ot.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ dependencies {

compile group: 'com.datadoghq', name: 'java-dogstatsd-client', version: '2.1.1'

compile deps.jackson
compile deps.slf4j
compile group: 'org.msgpack', name: 'msgpack-core', version: '0.8.20'
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.11.0' // Last version to support Java7
compile group: 'com.squareup.moshi', name: 'moshi', version: '1.9.2'
compile group: 'com.github.jnr', name: 'jnr-unixsocket', version: '0.23'
compile group: 'com.lmax', name: 'disruptor', version: '3.4.2'

Expand All @@ -50,6 +51,7 @@ dependencies {
testCompile project(":dd-java-agent:testing")
testCompile project(':utils:gc-utils')
testCompile group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.17.1'
testCompile group: 'org.msgpack', name: 'jackson-dataformat-msgpack', version: '0.8.20'

traceAgentTestCompile deps.testcontainers

Expand Down
24 changes: 1 addition & 23 deletions dd-trace-ot/src/main/java/datadog/opentracing/DDSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import static io.opentracing.log.Fields.ERROR_OBJECT;
import static io.opentracing.log.Fields.MESSAGE;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import datadog.trace.api.DDTags;
import datadog.trace.api.interceptor.MutableSpan;
import datadog.trace.api.sampling.PrioritySampling;
Expand Down Expand Up @@ -77,7 +75,6 @@ public class DDSpan implements Span, MutableSpan {
context.getTrace().registerSpan(this);
}

@JsonIgnore
public boolean isFinished() {
return durationNano.get() != 0;
}
Expand Down Expand Up @@ -120,20 +117,17 @@ public DDSpan setError(final boolean error) {
*
* @return true if root, false otherwise
*/
@JsonIgnore
public final boolean isRootSpan() {
return BigInteger.ZERO.equals(context.getParentId());
}

@Override
@Deprecated
@JsonIgnore
public MutableSpan getRootSpan() {
return getLocalRootSpan();
}

@Override
@JsonIgnore
public MutableSpan getLocalRootSpan() {
return context().getTrace().getRootSpan();
}
Expand Down Expand Up @@ -297,14 +291,13 @@ public final DDSpan setSpanType(final String type) {
return this;
}

// Getters and JSON serialisation instructions
// Getters

/**
* Meta merges baggage and tags (stringified values)
*
* @return merged context baggage and tags
*/
@JsonGetter
public Map<String, String> getMeta() {
final Map<String, String> meta = new HashMap<>();
for (final Map.Entry<String, String> entry : context().getBaggageItems().entrySet()) {
Expand All @@ -321,58 +314,48 @@ public Map<String, String> getMeta() {
*
* @return metrics for this span
*/
@JsonGetter
public Map<String, Number> getMetrics() {
return context.getMetrics();
}

@Override
@JsonGetter("start")
public long getStartTime() {
return startTimeNano > 0 ? startTimeNano : TimeUnit.MICROSECONDS.toNanos(startTimeMicro);
}

@Override
@JsonGetter("duration")
public long getDurationNano() {
return durationNano.get();
}

@Override
@JsonGetter("service")
public String getServiceName() {
return context.getServiceName();
}

@JsonGetter("trace_id")
public BigInteger getTraceId() {
return context.getTraceId();
}

@JsonGetter("span_id")
public BigInteger getSpanId() {
return context.getSpanId();
}

@JsonGetter("parent_id")
public BigInteger getParentId() {
return context.getParentId();
}

@Override
@JsonGetter("resource")
public String getResourceName() {
return context.getResourceName();
}

@Override
@JsonGetter("name")
public String getOperationName() {
return context.getOperationName();
}

@Override
@JsonIgnore
public Integer getSamplingPriority() {
final int samplingPriority = context.getSamplingPriority();
if (samplingPriority == PrioritySampling.UNSET) {
Expand All @@ -383,29 +366,24 @@ public Integer getSamplingPriority() {
}

@Override
@JsonIgnore
public String getSpanType() {
return context.getSpanType();
}

@Override
@JsonIgnore
public Map<String, Object> getTags() {
return context().getTags();
}

@JsonGetter
public String getType() {
return context.getSpanType();
}

@Override
@JsonIgnore
public Boolean isError() {
return context.getErrorFlag();
}

@JsonGetter
public int getError() {
return context.getErrorFlag() ? 1 : 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package datadog.opentracing;

import com.fasterxml.jackson.annotation.JsonIgnore;
import datadog.opentracing.decorators.AbstractDecorator;
import datadog.trace.api.DDTags;
import datadog.trace.api.sampling.PrioritySampling;
Expand Down Expand Up @@ -289,12 +288,10 @@ public Iterable<Map.Entry<String, String>> baggageItems() {
return baggageItems.entrySet();
}

@JsonIgnore
public PendingTrace getTrace() {
return trace;
}

@JsonIgnore
public DDTracer getTracer() {
return tracer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import static java.util.Collections.singletonMap;
import static java.util.Collections.unmodifiableMap;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NumericNode;
import datadog.opentracing.DDSpan;
import datadog.trace.api.sampling.PrioritySampling;
import datadog.trace.common.writer.ddagent.DDAgentResponseListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -70,23 +67,16 @@ private static String getSpanEnv(final DDSpan span) {
}

@Override
public void onResponse(final String endpoint, final JsonNode responseJson) {
final JsonNode newServiceRates = responseJson.get("rate_by_service");
public void onResponse(
final String endpoint, final Map<String, Map<String, Number>> responseJson) {
final Map<String, Number> newServiceRates = responseJson.get("rate_by_service");
if (null != newServiceRates) {
log.debug("Update service sampler rates: {} -> {}", endpoint, responseJson);
final Map<String, RateSampler> updatedServiceRates = new HashMap<>();
final Iterator<String> itr = newServiceRates.fieldNames();
while (itr.hasNext()) {
final String key = itr.next();
final JsonNode value = newServiceRates.get(key);
try {
if (value instanceof NumericNode) {
updatedServiceRates.put(key, createRateSampler(value.doubleValue()));
} else {
log.debug("Unable to parse new service rate {} -> {}", key, value);
}
} catch (final NumberFormatException nfe) {
log.debug("Unable to parse new service rate {} -> {}", key, value);
for (final Map.Entry<String, Number> entry : newServiceRates.entrySet()) {
if (entry.getValue() != null) {
updatedServiceRates.put(
entry.getKey(), createRateSampler(entry.getValue().doubleValue()));
}
}
if (!updatedServiceRates.containsKey(DEFAULT_KEY)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package datadog.trace.common.serialization;

import datadog.opentracing.DDSpan;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public abstract class FormatWriter<DEST> {
public abstract void writeKey(String key, DEST destination) throws IOException;

public abstract void writeListHeader(int size, DEST destination) throws IOException;

public abstract void writeListFooter(DEST destination) throws IOException;

public abstract void writeMapHeader(int size, DEST destination) throws IOException;

public abstract void writeMapFooter(DEST destination) throws IOException;

public abstract void writeString(String key, String value, DEST destination) throws IOException;

public abstract void writeShort(String key, short value, DEST destination) throws IOException;

public abstract void writeByte(String key, byte value, DEST destination) throws IOException;

public abstract void writeInt(String key, int value, DEST destination) throws IOException;

public abstract void writeLong(String key, long value, DEST destination) throws IOException;

public abstract void writeFloat(String key, float value, DEST destination) throws IOException;

public abstract void writeDouble(String key, double value, DEST destination) throws IOException;

public abstract void writeBigInteger(String key, BigInteger value, DEST destination)
throws IOException;

public void writeNumber(final String key, final Number value, final DEST destination)
throws IOException {
if (value instanceof Double) {
writeDouble(key, value.doubleValue(), destination);
} else if (value instanceof Long) {
writeLong(key, value.longValue(), destination);
} else if (value instanceof Integer) {
writeInt(key, value.intValue(), destination);
} else if (value instanceof Float) {
writeFloat(key, value.floatValue(), destination);
} else if (value instanceof Byte) {
writeByte(key, value.byteValue(), destination);
} else if (value instanceof Short) {
writeShort(key, value.shortValue(), destination);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

else with an exception for unsupported types?

}

public void writeNumberMap(
final String key, final Map<String, Number> value, final DEST destination)
throws IOException {
writeKey(key, destination);
writeMapHeader(value.size(), destination);
for (final Map.Entry<String, Number> entry : value.entrySet()) {
writeNumber(entry.getKey(), entry.getValue(), destination);
}
writeMapFooter(destination);
}

public void writeStringMap(
final String key, final Map<String, String> value, final DEST destination)
throws IOException {
writeKey(key, destination);
writeMapHeader(value.size(), destination);
for (final Map.Entry<String, String> entry : value.entrySet()) {
writeString(entry.getKey(), entry.getValue(), destination);
}
writeMapFooter(destination);
}

public void writeTrace(final List<DDSpan> trace, final DEST destination) throws IOException {
writeListHeader(trace.size(), destination);
for (final DDSpan span : trace) {
writeDDSpan(span, destination);
}
writeListFooter(destination);
}

public void writeDDSpan(final DDSpan span, final DEST destination) throws IOException {
// Some of the tests rely on the specific ordering here.
writeMapHeader(12, destination); // must match count below.
/* 1 */ writeString("service", span.getServiceName(), destination);
/* 2 */ writeString("name", span.getOperationName(), destination);
/* 3 */ writeString("resource", span.getResourceName(), destination);
/* 4 */ writeBigInteger("trace_id", span.getTraceId(), destination);
/* 5 */ writeBigInteger("span_id", span.getSpanId(), destination);
/* 6 */ writeBigInteger("parent_id", span.getParentId(), destination);
/* 7 */ writeLong("start", span.getStartTime(), destination);
/* 8 */ writeLong("duration", span.getDurationNano(), destination);
/* 9 */ writeString("type", span.getType(), destination);
/* 10 */ writeInt("error", span.getError(), destination);
/* 11 */ writeNumberMap("metrics", span.getMetrics(), destination);
/* 12 */ writeStringMap("meta", span.getMeta(), destination);
writeMapFooter(destination);
}
}
Loading