Skip to content
Merged
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
28 changes: 27 additions & 1 deletion src/trace/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ describe("readTraceFromSQSEvent", () => {
});

describe("readTraceFromLambdaContext", () => {
it("can read from lambda context source", () => {
it("can read from lambda context source, legacy style", () => {
const result = readTraceFromLambdaContext({
clientContext: {
custom: {
Expand All @@ -413,6 +413,24 @@ describe("readTraceFromLambdaContext", () => {
source: Source.Event,
});
});
it("can read from lambda context source, new style", () => {
const result = readTraceFromLambdaContext({
clientContext: {
custom: {
"x-datadog-trace-id": "666",
"x-datadog-parent-id": "777",
"x-datadog-sampled": "1",
"x-datadog-sampling-priority": "1",
},
},
});
expect(result).toEqual({
parentID: "777",
sampleMode: SampleMode.AUTO_KEEP,
traceID: "666",
source: Source.Event,
});
});
it("can handle no `custom` key", () => {
const result = readTraceFromLambdaContext({
clientContext: {
Expand All @@ -421,6 +439,14 @@ describe("readTraceFromLambdaContext", () => {
});
expect(result).toBeUndefined();
});
it("can handle a string `custom` key", () => {
const result = readTraceFromLambdaContext({
clientContext: {
custom: "bar",
},
});
expect(result).toBeUndefined();
});
it("can handle no context", () => {
const result = readTraceFromLambdaContext(undefined);
expect(result).toBeUndefined();
Expand Down
23 changes: 21 additions & 2 deletions src/trace/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,28 @@ export function readTraceFromLambdaContext(context: any): TraceContext | undefin
return;
}

const traceData = context.clientContext?.custom?._datadog;
const custom = context.clientContext?.custom;

if (!traceData || typeof traceData !== "object") {
if (!custom || typeof custom !== "object") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a test to validate this condition could be useful ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return;
}
let traceData = null;

if (
custom.hasOwnProperty("_datadog") &&
typeof custom._datadog === "object" &&
custom._datadog.hasOwnProperty(traceIDHeader) &&
custom._datadog.hasOwnProperty(parentIDHeader) &&
custom._datadog.hasOwnProperty(samplingPriorityHeader)
) {
traceData = custom._datadog;
} else if (
custom.hasOwnProperty(traceIDHeader) &&
custom.hasOwnProperty(parentIDHeader) &&
custom.hasOwnProperty(samplingPriorityHeader)
) {
traceData = custom;
} else {
return;
}

Expand Down