Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse 128 bit trace Id retuned by lambda extension #7620

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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 @@ -45,6 +45,7 @@ public class LambdaHandler {
private static final String DATADOG_INVOCATION_ERROR_TYPE = "x-datadog-invocation-error-type";
private static final String DATADOG_INVOCATION_ERROR_STACK = "x-datadog-invocation-error-stack";
private static final String DATADOG_TAGS_KEY = "x-datadog-tags";
private static final String DATADOG_UPPER_64_TRACE_ID_TAG_KEY = "_dd.p.tid";

private static final String START_INVOCATION = "/lambda/start-invocation";
private static final String END_INVOCATION = "/lambda/end-invocation";
Expand Down Expand Up @@ -86,9 +87,20 @@ public static AgentSpan.Context notifyStartInvocation(
.build())
.execute()) {
if (response.isSuccessful()) {
final String traceID = response.headers().get(DATADOG_TRACE_ID);
final String traceIDLower64Long = response.headers().get(DATADOG_TRACE_ID);
final String priority = response.headers().get(DATADOG_SAMPLING_PRIORITY);
if (null != traceID && null != priority) {
final String tags = response.headers().get(DATADOG_TAGS_KEY);
final String traceIDUpper64BitHex = findUpper64BitTraceId(tags);
DDTraceId traceId;
if (null != traceIDUpper64BitHex && null != traceIDLower64Long) {
long lower64Long = Long.parseUnsignedLong(traceIDLower64Long);
String lower64Hex = Long.toHexString(lower64Long);
String full128BitHex = traceIDUpper64BitHex + lower64Hex;
traceId = DDTraceId.fromHex(full128BitHex);
} else {
traceId = DDTraceId.from(traceIDLower64Long);
}
if (null != traceIDLower64Long && null != priority) {
int samplingPriority = PrioritySampling.UNSET;
try {
samplingPriority = Integer.parseInt(priority);
Expand All @@ -97,18 +109,12 @@ public static AgentSpan.Context notifyStartInvocation(
}
log.debug(
"notifyStartInvocation success, found traceID = {} and samplingPriority = {}",
traceID,
traceIDLower64Long,
samplingPriority);
PropagationTags propagationTags =
propagationTagsFactory.fromHeaderValue(
PropagationTags.HeaderType.DATADOG, response.headers().get(DATADOG_TAGS_KEY));
propagationTagsFactory.fromHeaderValue(PropagationTags.HeaderType.DATADOG, tags);
return new ExtractedContext(
DDTraceId.from(traceID),
DDSpanId.ZERO,
samplingPriority,
null,
propagationTags,
DATADOG);
traceId, DDSpanId.ZERO, samplingPriority, null, propagationTags, DATADOG);
} else {
log.debug(
"could not find traceID or sampling priority in notifyStartInvocation, not injecting the context");
Expand All @@ -120,6 +126,21 @@ public static AgentSpan.Context notifyStartInvocation(
return null;
}

private static String findUpper64BitTraceId(String tags)
throws NumberFormatException, IndexOutOfBoundsException {
if (tags == null) {
return null;
}
String[] tagPairs = tags.split(",");
for (String tagPair : tagPairs) {
String[] tag = tagPair.trim().split("=");
if (tag.length == 2 && tag[0].equals(DATADOG_UPPER_64_TRACE_ID_TAG_KEY)) {
return tag[1];
}
}
return null;
}

public static boolean notifyEndInvocation(AgentSpan span, Object result, boolean isError) {

if (null == span || null == span.getSamplingPriority()) {
Expand Down