Skip to content

Commit

Permalink
Timestamp and duration conversion for datadog (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
worldtiki authored May 28, 2021
1 parent 573f794 commit a23c5f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
import java.util.Map;

import static java.util.Collections.emptyMap;
import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static org.springframework.util.StringUtils.hasText;

/**
* Converter between {@code Zipkin} and {@code Datadog} domains.
*/
public class DatadogDomainConverter {

public static final Long MILLIS_TO_NANOS = 1_000_000L;
private static final Integer ERROR = 1;

/**
Expand Down Expand Up @@ -78,12 +79,12 @@ private static String valueOrDefault(String input, String defaultValue) {
return input != null ? input : defaultValue;
}

private static Long toNanos(Long timestamp) {
return timestamp != null ? timestamp * MILLIS_TO_NANOS : null;
private static Long toNanos(Long microseconds) {
return microseconds != null ? MICROSECONDS.toNanos(microseconds) : null;
}

private static Long toMillis(Long timestamp) {
return timestamp != null ? timestamp / MILLIS_TO_NANOS : null;
private static Long toMicros(Long nanoseconds) {
return nanoseconds != null ? NANOSECONDS.toMicros(nanoseconds) : null;
}

private static BigInteger hexadecimalToDecimal(String input) {
Expand Down Expand Up @@ -137,10 +138,10 @@ public static Span toZipkin(DatadogSpan datadogSpan) {
builder.parentId(decimalToHexadecimal(datadogSpan.parentId()));
}
if (datadogSpan.start() != null) {
builder.timestamp(toMillis(datadogSpan.start()));
builder.timestamp(toMicros(datadogSpan.start()));
}
if (datadogSpan.duration() != null) {
builder.duration(toMillis(datadogSpan.duration()));
builder.duration(toMicros(datadogSpan.duration()));
}
// TODO: annotations, resource_name, ...

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Collections;
import java.util.Map;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.assertj.core.api.Assertions.assertThat;

class DatadogDomainConverterTest {
Expand All @@ -20,15 +21,15 @@ public void shouldCreateZipkinSpanFromDatadog() {
long traceId = 123L; // 7b hexadecimal
long parentId = 456L; // 1c8 hexadecimal
long spanId = 789L; // 315 hexadecimal
long timestamp = 1621233762447000000L;
long duration = 100000000L;
long timestampMs = 1621233762447L;
long durationMs = 150;

DatadogSpan datadogSpan = new DatadogSpan(
BigInteger.valueOf(traceId),
BigInteger.valueOf(spanId),
BigInteger.valueOf(parentId),
timestamp,
duration,
MILLISECONDS.toNanos(timestampMs),
MILLISECONDS.toNanos(durationMs),
1,
Map.of("tag1", "value1",
"tag2", "value2"),
Expand All @@ -47,8 +48,8 @@ public void shouldCreateZipkinSpanFromDatadog() {
assertThat(zipkinSpan.name()).isEqualTo(name);
assertThat(zipkinSpan.tags().get("type")).isEqualTo("web");
assertThat(zipkinSpan.localServiceName()).isEqualTo(serviceName);
assertThat(zipkinSpan.duration()).isEqualTo(100);
assertThat(zipkinSpan.timestamp()).isEqualTo(1621233762447L);
assertThat(zipkinSpan.duration()).isEqualTo(MILLISECONDS.toMicros(durationMs));
assertThat(zipkinSpan.timestamp()).isEqualTo(MILLISECONDS.toMicros(timestampMs));

// No error
assertThat(zipkinSpan.tags().get("error")).isNotBlank();
Expand All @@ -66,18 +67,18 @@ public void shouldConvertZipkinToDatadog() {
String traceId = "7b"; // 123 decimal
String parentId = "1c8"; // 456 decimal
String spanId = "315"; // 789 decimal
long timestamp = 1621233762447L;
var kind = Span.Kind.CLIENT;
long duration = 100L;
long timestampMs = 1621233762447L;
long durationMs = 150;

zipkin2.Span zipkinSpan = Span.newBuilder()
.traceId(traceId)
.id(spanId)
.parentId(parentId)
.name(name)
.localEndpoint(Endpoint.newBuilder().serviceName(serviceName).build())
.timestamp(timestamp)
.duration(duration)
.timestamp(MILLISECONDS.toMicros(timestampMs))
.duration(MILLISECONDS.toMicros(durationMs))
.kind(kind)
.putTag("tag1", "value1")
.putTag("tag2", "value2")
Expand All @@ -91,16 +92,12 @@ public void shouldConvertZipkinToDatadog() {
assertThat(datadogSpan.parentId()).isEqualTo(456L);
assertThat(datadogSpan.name()).isEqualTo(name);
assertThat(datadogSpan.service()).isEqualTo(serviceName);
assertThat(datadogSpan.duration()).isEqualTo(100_000_000);
assertThat(datadogSpan.start()).isEqualTo(1621233762447000000L);
assertThat(datadogSpan.start()).isEqualTo(MILLISECONDS.toNanos(timestampMs));
assertThat(datadogSpan.duration()).isEqualTo(MILLISECONDS.toNanos(durationMs));

// No error
assertThat(datadogSpan.error()).isNull();

// 2 user defined tags
assertThat(datadogSpan.meta()).hasSize(3); // 2 user tags + span.kind tag
assertThat(datadogSpan.meta().get("tag1")).isEqualTo("value1");
assertThat(datadogSpan.meta().get("tag2")).isEqualTo("value2");
}

@Test
Expand Down

0 comments on commit a23c5f1

Please sign in to comment.