-
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
Changes from 12 commits
71e3f64
80577e3
32b8d92
952887b
a146e03
00ca76d
7daaa7e
46cc9a9
71b9447
276a115
8e76feb
4ce1838
7e28e29
175830d
a813a29
ec428cd
04c3816
6a2a2c3
3a2dc8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { TracerWrapper } from "../../tracer-wrapper"; | ||
| import { EventBridgeEventTraceExtractor } from "./event-bridge"; | ||
| import { StepFunctionContextService } from "../../step-function-service"; | ||
|
|
||
| let mockSpanContext: any = null; | ||
|
|
||
|
|
@@ -106,5 +107,66 @@ describe("EventBridgeEventTraceExtractor", () => { | |
| const traceContext = extractor.extract(payload); | ||
| expect(traceContext).toBeNull(); | ||
| }); | ||
|
|
||
| it("extracts trace context from Step Function EventBridge event", () => { | ||
| // Reset StepFunctionContextService instance | ||
| StepFunctionContextService["_instance"] = undefined as any; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be done before every step?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reset is only required for tests that use the Step Function Context Service. It could be added in a BeforeEach for all tests, but I don't think it would add value. |
||
|
|
||
| const tracerWrapper = new TracerWrapper(); | ||
|
|
||
| const payload = { | ||
| version: "0", | ||
| id: "af718b2a-b987-e8c0-7a2b-a188fad2661a", | ||
| "detail-type": "ProcessEvent", | ||
| source: "demo.stepfunction", | ||
| account: "123456123456", | ||
| time: "2025-07-11T14:59:35Z", | ||
| region: "us-east-2", | ||
| resources: [ | ||
| "arn:aws:states:us-east-2:123456123456:stateMachine:rstrat-sfn-evb-demo-dev-state-machine", | ||
| "arn:aws:states:us-east-2:123456123456:execution:rstrat-sfn-evb-demo-dev-state-machine:6c190e7b-eb77-46db-af26-9066d353b105", | ||
| ], | ||
| detail: { | ||
| message: "Event from Step Functions", | ||
| timestamp: "2025-07-11T14:59:35.830Z", | ||
| executionName: "6c190e7b-eb77-46db-af26-9066d353b105", | ||
| stateMachineName: "rstrat-sfn-evb-demo-dev-state-machine", | ||
| input: { | ||
| testData: "Hello with Datadog tracing", | ||
| }, | ||
| _datadog: { | ||
| Execution: { | ||
| Id: "arn:aws:states:us-east-2:123456123456:execution:rstrat-sfn-evb-demo-dev-state-machine:6c190e7b-eb77-46db-af26-9066d353b105", | ||
| StartTime: "2025-07-11T14:59:35.806Z", | ||
| Name: "6c190e7b-eb77-46db-af26-9066d353b105", | ||
| RoleArn: "arn:aws:iam::123456123456:role/rstrat-sfn-evb-demo-dev-StepFunctionsExecutionRole-8maJHu01fhZZ", | ||
| RedriveCount: 0, | ||
| }, | ||
| StateMachine: { | ||
| Id: "arn:aws:states:us-east-2:123456123456:stateMachine:rstrat-sfn-evb-demo-dev-state-machine", | ||
| Name: "rstrat-sfn-evb-demo-dev-state-machine", | ||
| }, | ||
| State: { | ||
| Name: "PublishToEventBridge", | ||
| EnteredTime: "2025-07-11T14:59:35.830Z", | ||
| RetryCount: 0, | ||
| }, | ||
| RootExecutionId: | ||
| "arn:aws:states:us-east-2:123456123456:execution:rstrat-sfn-evb-demo-dev-state-machine:6c190e7b-eb77-46db-af26-9066d353b105", | ||
| "serverless-version": "v1", | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const extractor = new EventBridgeEventTraceExtractor(tracerWrapper); | ||
|
|
||
| const traceContext = extractor.extract(payload); | ||
| expect(traceContext).not.toBeNull(); | ||
|
|
||
| expect(traceContext?.toTraceId()).toBe("1503104665848096006"); | ||
| expect(traceContext?.toSpanId()).toBe("159267866761498620"); | ||
| expect(traceContext?.sampleMode()).toBe("1"); | ||
| expect(traceContext?.source).toBe("event"); | ||
| }); | ||
| }); | ||
| }); | ||
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