diff --git a/src/main/java/com/hotels/service/tracing/zipkintohaystack/forwarders/datadog/DatadogDomainConverter.java b/src/main/java/com/hotels/service/tracing/zipkintohaystack/forwarders/datadog/DatadogDomainConverter.java index 51fc5a56..8db9db06 100644 --- a/src/main/java/com/hotels/service/tracing/zipkintohaystack/forwarders/datadog/DatadogDomainConverter.java +++ b/src/main/java/com/hotels/service/tracing/zipkintohaystack/forwarders/datadog/DatadogDomainConverter.java @@ -27,6 +27,8 @@ 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; /** @@ -34,7 +36,6 @@ */ public class DatadogDomainConverter { - public static final Long MILLIS_TO_NANOS = 1_000_000L; private static final Integer ERROR = 1; /** @@ -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) { @@ -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, ... diff --git a/src/test/java/com/hotels/service/tracing/zipkintohaystack/forwarders/datadog/DatadogDomainConverterTest.java b/src/test/java/com/hotels/service/tracing/zipkintohaystack/forwarders/datadog/DatadogDomainConverterTest.java index b38db4d1..0a16724c 100644 --- a/src/test/java/com/hotels/service/tracing/zipkintohaystack/forwarders/datadog/DatadogDomainConverterTest.java +++ b/src/test/java/com/hotels/service/tracing/zipkintohaystack/forwarders/datadog/DatadogDomainConverterTest.java @@ -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 { @@ -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"), @@ -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(); @@ -66,9 +67,9 @@ 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) @@ -76,8 +77,8 @@ public void shouldConvertZipkinToDatadog() { .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") @@ -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