-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support to forward spans to datadog and to accept datadog payloads Co-authored-by: Nikos Katirtzis <[email protected]>
- Loading branch information
1 parent
7f877ab
commit 4fad766
Showing
34 changed files
with
1,222 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
...om/hotels/service/tracing/zipkintohaystack/forwarders/datadog/DatadogDomainConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
* Copyright 2018 Expedia, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.hotels.service.tracing.zipkintohaystack.forwarders.datadog; | ||
|
||
import com.hotels.service.tracing.zipkintohaystack.forwarders.datadog.model.DatadogSpan; | ||
import zipkin2.Annotation; | ||
import zipkin2.Endpoint; | ||
import zipkin2.Span; | ||
|
||
import java.math.BigInteger; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static java.util.Collections.emptyMap; | ||
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; | ||
|
||
/** | ||
* Accepts a span in {@code Zipkin V2} format and returns a span in {@code Datadog} format. | ||
*/ | ||
public static DatadogSpan fromZipkinV2(zipkin2.Span zipkin) { | ||
Integer error = isError(zipkin); | ||
BigInteger parentId = hexadecimalToDecimal(zipkin.parentId()); | ||
var spanId = hexadecimalToDecimal(zipkin.id()); | ||
var traceId = hexadecimalToDecimal(zipkin.traceId()); | ||
|
||
return new DatadogSpan( | ||
traceId, | ||
spanId, | ||
parentId, | ||
toNanos(zipkin.timestamp()), | ||
toNanos(zipkin.duration()), | ||
error, | ||
tags(zipkin.tags(), zipkin.annotations(), zipkin.kind()), | ||
emptyMap(), | ||
valueOrDefault(zipkin.name(), "span"), | ||
valueOrDefault(zipkin.name(), "resource"), // TODO: maybe derive resource from tags? http.method + http.path? | ||
zipkin.localServiceName(), | ||
"web" | ||
); | ||
} | ||
|
||
private static Map<String, String> tags(Map<String, String> tags, List<Annotation> annotations, Span.Kind kind) { | ||
Map<String, String> collected = new HashMap<>(); | ||
|
||
if (kind != null) { | ||
collected.put("span.kind", kind.name()); | ||
} | ||
|
||
tags.forEach(collected::put); | ||
|
||
return collected; | ||
} | ||
|
||
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 toMillis(Long timestamp) { | ||
return timestamp != null ? timestamp / MILLIS_TO_NANOS : null; | ||
} | ||
|
||
private static BigInteger hexadecimalToDecimal(String input) { | ||
// B3 accepts ids with different sizes (64 or 128 bits). | ||
// To make this work we truncate larger than 64 bit ones (16 chars). | ||
if (input == null) { | ||
return null; | ||
} else if (input.length() > 16) { | ||
return new BigInteger(input.substring(16), 16); | ||
} else { | ||
return new BigInteger(input, 16); | ||
} | ||
} | ||
|
||
private static Integer isError(Span zipkin) { | ||
return zipkin.tags().containsKey("error") ? ERROR : null; | ||
} | ||
|
||
private static boolean isError(DatadogSpan datadog) { | ||
return ERROR.equals(datadog.error()); | ||
} | ||
|
||
public static Span toZipkin(DatadogSpan datadogSpan) { | ||
var builder = Span.newBuilder(); | ||
|
||
if (hasText(datadogSpan.name())) { | ||
builder.localEndpoint(Endpoint.newBuilder() | ||
.serviceName(datadogSpan.service()) | ||
.build()); | ||
} | ||
|
||
builder.name(datadogSpan.name()); | ||
|
||
if (isError(datadogSpan)) { | ||
builder.putTag("error", "error"); | ||
} | ||
if (datadogSpan.meta() != null) { | ||
datadogSpan.meta().forEach(builder::putTag); | ||
} | ||
|
||
if (datadogSpan.type() != null) { | ||
builder.putTag("type", datadogSpan.type()); | ||
} | ||
if (datadogSpan.traceId() != null) { | ||
builder.traceId(decimalToHexadecimal(datadogSpan.traceId())); | ||
} | ||
if (datadogSpan.spanId() != null) { | ||
builder.id(decimalToHexadecimal(datadogSpan.spanId())); | ||
} | ||
if (datadogSpan.parentId() != null) { | ||
builder.parentId(decimalToHexadecimal(datadogSpan.parentId())); | ||
} | ||
if (datadogSpan.start() != null) { | ||
builder.timestamp(toMillis(datadogSpan.start())); | ||
} | ||
if (datadogSpan.duration() != null) { | ||
builder.duration(toMillis(datadogSpan.duration())); | ||
} | ||
// TODO: annotations, resource_name, ... | ||
|
||
return builder.build(); | ||
} | ||
|
||
/** | ||
* Zipkin trace ids are 64 or 128 bits represented as 16 or 32 hex characters with '0' left padding | ||
* We always use 64 bits (16 chars) to maintain compatibility with Datadog. | ||
*/ | ||
private static String decimalToHexadecimal(BigInteger id) { | ||
return String.format("%016x", id); | ||
} | ||
} |
Oops, something went wrong.