-
Notifications
You must be signed in to change notification settings - Fork 38
Propagate Step Function Trace Context through Managed Services #667
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
71e3f64
Add SFN -> EventBridge -> Lambda use case
ryanstrat 80577e3
Simplify code
ryanstrat 32b8d92
Remove additional classes
ryanstrat 952887b
Add implementation and test for SFN -> EventBridge -> SQS -> Lambda case
ryanstrat a146e03
Add support for Eventbridge -> SNS -> Lambda pattern for both header …
ryanstrat 00ca76d
Revert "Add support for Eventbridge -> SNS -> Lambda pattern for both…
ryanstrat 7daaa7e
Add implementation and test for SFN -> SQS -> Lambda
ryanstrat 46cc9a9
Add implementation and test for SFN -> SNS -> Lambda
ryanstrat 71b9447
Add implementation and test for SFN -> SNS -> SQS -> Lambda
ryanstrat 276a115
Refactor common extraction logic
ryanstrat 8e76feb
Remove unrequired changes to SQS binary format handling
ryanstrat 4ce1838
wip
ryanstrat 7e28e29
wip
ryanstrat 175830d
Merge branch 'ryan.strat/sfn-managed-services' of github.com:DataDog/…
ryanstrat a813a29
revent yarn.lock changes
ryanstrat ec428cd
simplify extractor-util logic
ryanstrat 04c3816
Revert changes to layer publishing script
ryanstrat 6a2a2c3
Merge branch 'main' into ryan.strat/sfn-managed-services
ryanstrat 3a2dc8a
Fixes test failure from merging main
ryanstrat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,164 @@ | ||
| import { TracerWrapper } from "../tracer-wrapper"; | ||
| import { extractTraceContext, extractFromAWSTraceHeader } from "./extractor-utils"; | ||
| import { StepFunctionContextService } from "../step-function-service"; | ||
|
|
||
| describe("extractor-utils", () => { | ||
| beforeEach(() => { | ||
| StepFunctionContextService["_instance"] = undefined as any; | ||
| }); | ||
| describe("extractTraceContext", () => { | ||
| it("returns span context when tracer wrapper successfully extracts from headers", () => { | ||
| const legacyStepFunctionEvent = { | ||
| Execution: { | ||
| Id: "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf", | ||
| Input: { | ||
| MyInput: "MyValue", | ||
| }, | ||
| Name: "85a9933e-9e11-83dc-6a61-b92367b6c3be", | ||
| RoleArn: "arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03", | ||
| RedriveCount: 0, | ||
| StartTime: "2022-12-08T21:08:17.924Z", | ||
| }, | ||
| State: { | ||
| Name: "step-one", | ||
| EnteredTime: "2022-12-08T21:08:19.224Z", | ||
| RetryCount: 2, | ||
| }, | ||
| StateMachine: { | ||
| Id: "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential", | ||
| Name: "my-state-machine", | ||
| }, | ||
| }; | ||
|
|
||
| const tracerWrapper = new TracerWrapper(); | ||
| const result = extractTraceContext(legacyStepFunctionEvent, tracerWrapper); | ||
|
|
||
| // Should return a span context from Step Function context since headers extraction fails | ||
| expect(result).not.toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null when no trace context can be extracted", () => { | ||
| const emptyEvent = { | ||
| someOtherProperty: "value", | ||
| }; | ||
|
|
||
| const tracerWrapper = new TracerWrapper(); | ||
| const result = extractTraceContext(emptyEvent, tracerWrapper); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it("extracts context from LambdaRootStepFunctionContext", () => { | ||
| const lambdaRootStepFunctionEvent = { | ||
| _datadog: { | ||
| Execution: { | ||
| Id: "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf", | ||
| Input: { | ||
| MyInput: "MyValue", | ||
| }, | ||
| Name: "85a9933e-9e11-83dc-6a61-b92367b6c3be", | ||
| RoleArn: | ||
| "arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03", | ||
| RedriveCount: 0, | ||
| StartTime: "2022-12-08T21:08:17.924Z", | ||
| }, | ||
| State: { | ||
| Name: "step-one", | ||
| EnteredTime: "2022-12-08T21:08:19.224Z", | ||
| RetryCount: 2, | ||
| }, | ||
| StateMachine: { | ||
| Id: "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential", | ||
| Name: "my-state-machine", | ||
| }, | ||
| "x-datadog-trace-id": "10593586103637578129", | ||
| "x-datadog-tags": "_dd.p.dm=-0,_dd.p.tid=6734e7c300000000", | ||
| "serverless-version": "v1", | ||
| }, | ||
| }; | ||
|
|
||
| const tracerWrapper = new TracerWrapper(); | ||
| const result = extractTraceContext(lambdaRootStepFunctionEvent, tracerWrapper); | ||
|
|
||
| expect(result).not.toBeNull(); | ||
| }); | ||
|
|
||
| it("extracts context from NestedStepFunctionContext", () => { | ||
| const nestedStepFunctionEvent = { | ||
| _datadog: { | ||
| Execution: { | ||
| Id: "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf", | ||
| Input: { | ||
| MyInput: "MyValue", | ||
| }, | ||
| Name: "85a9933e-9e11-83dc-6a61-b92367b6c3be", | ||
| RoleArn: | ||
| "arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03", | ||
| RedriveCount: 0, | ||
| StartTime: "2022-12-08T21:08:17.924Z", | ||
| }, | ||
| State: { | ||
| Name: "step-one", | ||
| EnteredTime: "2022-12-08T21:08:19.224Z", | ||
| RetryCount: 2, | ||
| }, | ||
| StateMachine: { | ||
| Id: "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential", | ||
| Name: "my-state-machine", | ||
| }, | ||
| RootExecutionId: | ||
| "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:a1b2c3d4-e5f6-7890-1234-56789abcdef0:9f8e7d6c-5b4a-3c2d-1e0f-123456789abc", | ||
| "serverless-version": "v1", | ||
| }, | ||
| }; | ||
|
|
||
| const tracerWrapper = new TracerWrapper(); | ||
| const result = extractTraceContext(nestedStepFunctionEvent, tracerWrapper); | ||
|
|
||
| expect(result).not.toBeNull(); | ||
| }); | ||
|
|
||
| it("extracts context from legacy lambda StepFunctionContext", () => { | ||
| const event = { | ||
| Payload: { | ||
| Execution: { | ||
| Id: "arn:aws:states:sa-east-1:425362996713:express:logs-to-traces-sequential:85a9933e-9e11-83dc-6a61-b92367b6c3be:3f7ef5c7-c8b8-4c88-90a1-d54aa7e7e2bf", | ||
| Input: { | ||
| MyInput: "MyValue", | ||
| }, | ||
| Name: "85a9933e-9e11-83dc-6a61-b92367b6c3be", | ||
| RoleArn: | ||
| "arn:aws:iam::425362996713:role/service-role/StepFunctions-logs-to-traces-sequential-role-ccd69c03", | ||
| RedriveCount: 0, | ||
| StartTime: "2022-12-08T21:08:17.924Z", | ||
| }, | ||
| State: { | ||
| Name: "step-one", | ||
| EnteredTime: "2022-12-08T21:08:19.224Z", | ||
| RetryCount: 2, | ||
| }, | ||
| StateMachine: { | ||
| Id: "arn:aws:states:sa-east-1:425362996713:stateMachine:logs-to-traces-sequential", | ||
| Name: "my-state-machine", | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const tracerWrapper = new TracerWrapper(); | ||
| const result = extractTraceContext(event, tracerWrapper); | ||
|
|
||
| expect(result).not.toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("extractFromAWSTraceHeader", () => { | ||
| it("returns null when AWS trace header is invalid", () => { | ||
| const invalidHeader = "invalid-header"; | ||
| const eventType = "SQS"; | ||
|
|
||
| const result = extractFromAWSTraceHeader(invalidHeader, eventType); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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,64 @@ | ||
| import { logDebug } from "../../utils"; | ||
| import { StepFunctionContextService } from "../step-function-service"; | ||
| import { SpanContextWrapper } from "../span-context-wrapper"; | ||
| import { TracerWrapper } from "../tracer-wrapper"; | ||
| import { XrayService } from "../xray-service"; | ||
|
|
||
| /** | ||
| * Common utility functions for trace context extraction | ||
| */ | ||
|
|
||
| /** | ||
| * Attempts to extract trace context from headers, falling back to Step Function context if needed | ||
| * @param headers The headers object to extract from | ||
| * @param tracerWrapper The tracer wrapper instance | ||
| * @returns SpanContextWrapper or null | ||
| */ | ||
| export function extractTraceContext(headers: any, tracerWrapper: TracerWrapper): SpanContextWrapper | null { | ||
| // First try to extract as regular trace headers | ||
| const traceContext = tracerWrapper.extract(headers); | ||
| if (traceContext) { | ||
| return traceContext; | ||
| } | ||
|
|
||
| // If that fails, check if this is a Step Function context | ||
| const stepFunctionInstance = StepFunctionContextService.instance(headers); | ||
| const stepFunctionContext = stepFunctionInstance.context; | ||
|
|
||
| if (stepFunctionContext !== undefined) { | ||
| const spanContext = stepFunctionInstance.spanContext; | ||
| if (spanContext !== null) { | ||
| return spanContext; | ||
| } | ||
| } | ||
ryanstrat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts trace context from AWS Trace Header | ||
| * @param awsTraceHeader The AWS trace header string | ||
| * @param eventType The type of event (for logging) | ||
| * @returns SpanContextWrapper or null | ||
| */ | ||
| export function extractFromAWSTraceHeader(awsTraceHeader: string, eventType: string): SpanContextWrapper | null { | ||
| const traceContext = XrayService.extraceDDContextFromAWSTraceHeader(awsTraceHeader); | ||
| if (traceContext) { | ||
| logDebug(`Extracted trace context from ${eventType} event attributes AWSTraceHeader`); | ||
| return traceContext; | ||
| } else { | ||
| logDebug(`No Datadog trace context found from ${eventType} event attributes AWSTraceHeader`); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Common error handler for extraction operations | ||
| * @param error The error that occurred | ||
| * @param eventType The type of event (for logging) | ||
| */ | ||
| export function handleExtractionError(error: unknown, eventType: string): void { | ||
| if (error instanceof Error) { | ||
| logDebug(`Unable to extract trace context from ${eventType} event`, error); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is the variable LAYER_NAME_SUFFIX set? is this mainly for testing in development?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not, I added it as a local config option to publish private versions. This is similar to an option available in the python layer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mmm, not sure about this change, how are we sure this is not going to affect gitlab pipelines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this edit based on feedback from @duncanista