Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,12 @@ private static class ExtractionCache<C>
private final List<String> keysAndValues;

/**
* The parent span identifier from {@link DatadogHttpCodec#SPAN_ID_KEY} header formatted as 16
* hexadecimal characters, {@code null} if absent or invalid.
* The parent span identifier from {@link DatadogHttpCodec#SPAN_ID_KEY} header, valid only if
* {@link #hasDatadogSpanId}. Kept unformatted since its hex form is rarely read.
*/
private String datadogSpanIdHex;
private long datadogSpanId;

private boolean hasDatadogSpanId;

public ExtractionCache(C carrier, AgentPropagation.ContextVisitor<C> getter) {
this.keysAndValues = new ArrayList<>(32);
Expand All @@ -346,15 +348,19 @@ public boolean accept(String key, String value) {
private void cacheDatadogSpanId(String key, String value) {
if (SPAN_ID_KEY.equalsIgnoreCase(key)) {
try {
// Parse numeric header value to format it as 16 hexadecimal character format
this.datadogSpanIdHex = DDSpanId.toHexStringPadded(DDSpanId.from(value));
this.datadogSpanId = DDSpanId.from(value);
this.hasDatadogSpanId = true;
} catch (NumberFormatException ignored) {
}
}
}

/**
* Returns the {@link DatadogHttpCodec#SPAN_ID_KEY} header formatted as 16 hexadecimal
* characters, {@code null} if absent or invalid.
*/
private String getDatadogSpanIdHex() {
return this.datadogSpanIdHex;
return this.hasDatadogSpanId ? DDSpanId.toHexStringPadded(this.datadogSpanId) : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,25 +368,17 @@ private void parseTraceParent(String tp, boolean buildTraceId) {
} else if (version == 0 && length > TRACE_PARENT_LENGTH) {
throw new IllegalStateException("The length of traceparent '" + tp + "' is too long");
}
DD128bTraceId traceId = null;
if (buildTraceId) {
traceId = DD128bTraceId.fromHex(tp, TRACE_PARENT_TID_START, 32, true);
this.traceIdHigh = traceId.toHighOrderLong();
this.traceIdLow = traceId.toLong();
} else {
// as DD128bTraceId.fromHex(tp, TRACE_PARENT_TID_START, 32, true) parses them
this.traceIdHigh =
LongStringUtils.parseUnsignedLongHex(tp, TRACE_PARENT_TID_START, 16, true);
this.traceIdLow =
LongStringUtils.parseUnsignedLongHex(tp, TRACE_PARENT_TID_START + 16, 16, true);
}
this.traceIdHigh = LongStringUtils.parseUnsignedLongHex(tp, TRACE_PARENT_TID_START, 16, true);
this.traceIdLow =
LongStringUtils.parseUnsignedLongHex(tp, TRACE_PARENT_TID_START + 16, 16, true);
if (traceIdLow == 0) {
throw new IllegalStateException(
"Illegal all zero 64 bit trace id "
+ tp.substring(TRACE_PARENT_TID_START, TRACE_PARENT_TID_END));
}
if (traceId != null) {
this.traceId = traceId;
if (buildTraceId) {
// Built from its parts, so it formats its hex form only if read, not per extraction
this.traceId = DD128bTraceId.from(traceIdHigh, traceIdLow);
}
this.spanId = DDSpanId.fromHex(tp, TRACE_PARENT_SID_START, 16, true);
if (this.spanId == 0) {
Expand Down
Loading