|
1 | 1 | import { SNSEvent } from "aws-lambda"; |
2 | 2 | import { TracerWrapper } from "../../tracer-wrapper"; |
3 | 3 | import { SNSEventTraceExtractor } from "./sns"; |
| 4 | +import { StepFunctionContextService } from "../../step-function-service"; |
4 | 5 |
|
5 | 6 | let mockSpanContext: any = null; |
6 | 7 |
|
@@ -256,5 +257,51 @@ describe("SNSEventTraceExtractor", () => { |
256 | 257 | expect(traceContext?.sampleMode()).toBe("1"); |
257 | 258 | expect(traceContext?.source).toBe("event"); |
258 | 259 | }); |
| 260 | + |
| 261 | + it("extracts trace context from Step Function SNS event", () => { |
| 262 | + // Reset StepFunctionContextService instance |
| 263 | + StepFunctionContextService["_instance"] = undefined as any; |
| 264 | + |
| 265 | + const tracerWrapper = new TracerWrapper(); |
| 266 | + |
| 267 | + const payload: SNSEvent = { |
| 268 | + Records: [ |
| 269 | + { |
| 270 | + EventSource: "aws:sns", |
| 271 | + EventVersion: "1.0", |
| 272 | + EventSubscriptionArn: "arn:aws:sns:sa-east-1:123456123456:rstrat-sfn-sns-demo-dev-process-event-topic:f18241f8-a4f7-4586-80db-97bd1939a557", |
| 273 | + Sns: { |
| 274 | + Type: "Notification", |
| 275 | + MessageId: "46d2665c-7ee2-50ba-a4cd-06acf35f5d5f", |
| 276 | + TopicArn: "arn:aws:sns:sa-east-1:123456123456:rstrat-sfn-sns-demo-dev-process-event-topic", |
| 277 | + Message: "{\"source\":\"demo.stepfunction\",\"detailType\":\"ProcessEvent\",\"message\":\"Test event from Step Functions\",\"customData\":{\"userId\":\"12345\",\"action\":\"test\"}}", |
| 278 | + Timestamp: "2025-07-15T17:10:21.503Z", |
| 279 | + SignatureVersion: "1", |
| 280 | + Signature: "fHeJta0GWCs/lHhI6wesXiT+66i1SZ+XH58pyd8mKHKD8bepXsnWvfQdDsOkO2AVv2CqPBF58sAWQae6yob2aMawe/vo8eeahJCaguK8a/3HLj7kP+nXGjgSGvzQm4CdYEyAUco453/mfE/BSf0SkdctxW0rjMs27T2l964Lt2Y/vJeiXVibs/AqEIu3ImekbM8+EIfNMOLBdRBVE47650vawazMkcpPtg5o/8LCA/jNUNj9VCTJrvzep8/vVJEcuHbZ3pcmajA9UJmP3000G0+to0cXwZ5YaakOxQTv81I+cfC99yQJoogLklbgiu+4bqEeNWbwW9KdQz1U+79NgA==", |
| 281 | + SigningCertUrl: "https://sns.sa-east-1.amazonaws.com/SimpleNotificationService-9c6465fa7f48f5cacd23014631ec1136.pem", |
| 282 | + Subject: "Event from Step Functions", |
| 283 | + UnsubscribeUrl: "https://sns.sa-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:sa-east-1:123456123456:rstrat-sfn-sns-demo-dev-process-event-topic:f18241f8-a4f7-4586-80db-97bd1939a557", |
| 284 | + MessageAttributes: { |
| 285 | + _datadog: { |
| 286 | + Type: "String", |
| 287 | + Value: "{\"Execution\":{\"Id\":\"arn:aws:states:sa-east-1:123456123456:execution:rstrat-sfn-sns-demo-dev-state-machine:79049e80-5cc6-49da-9dc0-f19ba2921772\",\"StartTime\":\"2025-07-15T17:10:21.328Z\",\"Name\":\"79049e80-5cc6-49da-9dc0-f19ba2921772\",\"RoleArn\":\"arn:aws:iam::123456123456:role/rstrat-sfn-sns-demo-dev-StepFunctionsExecutionRole-LrsdDm6wMmBh\",\"RedriveCount\":0},\"StateMachine\":{\"Id\":\"arn:aws:states:sa-east-1:123456123456:stateMachine:rstrat-sfn-sns-demo-dev-state-machine\",\"Name\":\"rstrat-sfn-sns-demo-dev-state-machine\"},\"State\":{\"Name\":\"PublishToSNS\",\"EnteredTime\":\"2025-07-15T17:10:21.354Z\",\"RetryCount\":0},\"RootExecutionId\":\"arn:aws:states:sa-east-1:123456123456:execution:rstrat-sfn-sns-demo-dev-state-machine:79049e80-5cc6-49da-9dc0-f19ba2921772\",\"serverless-version\":\"v1\"}" |
| 288 | + } |
| 289 | + } |
| 290 | + } |
| 291 | + } |
| 292 | + ] |
| 293 | + }; |
| 294 | + |
| 295 | + const extractor = new SNSEventTraceExtractor(tracerWrapper); |
| 296 | + |
| 297 | + const traceContext = extractor.extract(payload); |
| 298 | + expect(traceContext).not.toBeNull(); |
| 299 | + |
| 300 | + // The StepFunctionContextService generates deterministic trace IDs |
| 301 | + expect(traceContext?.toTraceId()).toBe("3995810302240690842"); |
| 302 | + expect(traceContext?.toSpanId()).toBe("8347071195300897803"); |
| 303 | + expect(traceContext?.sampleMode()).toBe("1"); |
| 304 | + expect(traceContext?.source).toBe("event"); |
| 305 | + }); |
259 | 306 | }); |
260 | 307 | }); |
0 commit comments