From 3a0871940d14a113419830c8016986b672576f27 Mon Sep 17 00:00:00 2001 From: AJ Stuyvenberg Date: Thu, 3 Mar 2022 14:02:29 -0600 Subject: [PATCH] feat: Support binary based decoding for SNS message attributes (#269) * feat: Support binary based decoding for SNS message attributes * feat: Bump depencency on dd-trace-js to 2.3.1 * re-bump yarn * feat: bump integrations --- .../logs/process-input-traced_node12.log | 5 +- .../logs/process-input-traced_node14.log | 5 +- .../logs/throw-error-traced_node12.log | 2 +- package.json | 2 +- src/trace/context.spec.ts | 78 +++++++++++++++++++ src/trace/context.ts | 18 ++++- yarn.lock | 38 +++------ 7 files changed, 111 insertions(+), 37 deletions(-) diff --git a/integration_tests/snapshots/logs/process-input-traced_node12.log b/integration_tests/snapshots/logs/process-input-traced_node12.log index 48cf804b..a5943b62 100644 --- a/integration_tests/snapshots/logs/process-input-traced_node12.log +++ b/integration_tests/snapshots/logs/process-input-traced_node12.log @@ -1,14 +1,15 @@ START {"e":XXXX,"m":"aws.lambda.enhanced.invocations","t":["region:sa-east-1","account_id:XXXX","functionname:integration-tests-js-XXXX-process-input-traced_node12","resource:integration-tests-js-XXXX-process-input-traced_node12","cold_start:true","memorysize:1024","datadog_lambda:vX.X.X","runtime:nodejs12.x"],"v":1} {"e":XXXX,"m":"serverless.integration_test.execution","t":["function:process-input-traced","dd_lambda_layer:datadog-nodev12.XX.X"],"v":1} +{"traces":[[{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.apigateway","resource":" ","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"type":"http"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.lambda","resource":"integration-tests-js-XXXX-process-input-traced_node12","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"aws.lambda","type":"serverless"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"getRecordIds","resource":"getRecordIds","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node12"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"getAPIGatewayRequestId","resource":"getAPIGatewayRequestId","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node12"}]]} END Duration: XXXX ms (init: XXXX ms) Memory Used: XXXX MB START {"e":XXXX,"m":"aws.lambda.enhanced.invocations","t":["region:sa-east-1","account_id:XXXX","functionname:integration-tests-js-XXXX-process-input-traced_node12","resource:integration-tests-js-XXXX-process-input-traced_node12","cold_start:false","memorysize:1024","datadog_lambda:vX.X.X","runtime:nodejs12.x"],"v":1} {"e":XXXX,"m":"serverless.integration_test.execution","t":["function:process-input-traced","dd_lambda_layer:datadog-nodev12.XX.X"],"v":1} -{"traces":[[{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"getRecordIds","resource":"getRecordIds","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node12"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.lambda","resource":"integration-tests-js-XXXX-process-input-traced_node12","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"aws.lambda","type":"serverless"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.sns","resource":"sns-lambda","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node12","type":"sns"}]]} +{"traces":[[{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.sns","resource":"sns-lambda","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node12","type":"sns"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.lambda","resource":"integration-tests-js-XXXX-process-input-traced_node12","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"aws.lambda","type":"serverless"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"getRecordIds","resource":"getRecordIds","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node12"}]]} END Duration: XXXX ms Memory Used: XXXX MB START {"e":XXXX,"m":"aws.lambda.enhanced.invocations","t":["region:sa-east-1","account_id:XXXX","functionname:integration-tests-js-XXXX-process-input-traced_node12","resource:integration-tests-js-XXXX-process-input-traced_node12","cold_start:false","memorysize:1024","datadog_lambda:vX.X.X","runtime:nodejs12.x"],"v":1} {"e":XXXX,"m":"serverless.integration_test.execution","t":["function:process-input-traced","dd_lambda_layer:datadog-nodev12.XX.X"],"v":1} -{"traces":[[{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"getRecordIds","resource":"getRecordIds","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node12"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.lambda","resource":"integration-tests-js-XXXX-process-input-traced_node12","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"aws.lambda","type":"serverless"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.sqs","resource":"my-queue","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"my-queue","type":"web"}]]} +{"traces":[[{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.sqs","resource":"my-queue","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"my-queue","type":"web"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.lambda","resource":"integration-tests-js-XXXX-process-input-traced_node12","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"aws.lambda","type":"serverless"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"getRecordIds","resource":"getRecordIds","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node12"}]]} END Duration: XXXX ms Memory Used: XXXX MB diff --git a/integration_tests/snapshots/logs/process-input-traced_node14.log b/integration_tests/snapshots/logs/process-input-traced_node14.log index 90d261bc..d30068f0 100644 --- a/integration_tests/snapshots/logs/process-input-traced_node14.log +++ b/integration_tests/snapshots/logs/process-input-traced_node14.log @@ -1,14 +1,15 @@ START {"e":XXXX,"m":"aws.lambda.enhanced.invocations","t":["region:sa-east-1","account_id:XXXX","functionname:integration-tests-js-XXXX-process-input-traced_node14","resource:integration-tests-js-XXXX-process-input-traced_node14","cold_start:true","memorysize:1024","datadog_lambda:vX.X.X","runtime:nodejs14.x"],"v":1} {"e":XXXX,"m":"serverless.integration_test.execution","t":["function:process-input-traced","dd_lambda_layer:datadog-nodev14.XX.X"],"v":1} +{"traces":[[{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.apigateway","resource":" ","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"type":"http"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.lambda","resource":"integration-tests-js-XXXX-process-input-traced_node14","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"aws.lambda","type":"serverless"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"getRecordIds","resource":"getRecordIds","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node14"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"getAPIGatewayRequestId","resource":"getAPIGatewayRequestId","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node14"}]]} END Duration: XXXX ms (init: XXXX ms) Memory Used: XXXX MB START {"e":XXXX,"m":"aws.lambda.enhanced.invocations","t":["region:sa-east-1","account_id:XXXX","functionname:integration-tests-js-XXXX-process-input-traced_node14","resource:integration-tests-js-XXXX-process-input-traced_node14","cold_start:false","memorysize:1024","datadog_lambda:vX.X.X","runtime:nodejs14.x"],"v":1} {"e":XXXX,"m":"serverless.integration_test.execution","t":["function:process-input-traced","dd_lambda_layer:datadog-nodev14.XX.X"],"v":1} -{"traces":[[{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"getRecordIds","resource":"getRecordIds","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node14"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.lambda","resource":"integration-tests-js-XXXX-process-input-traced_node14","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"aws.lambda","type":"serverless"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.sns","resource":"sns-lambda","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node14","type":"sns"}]]} +{"traces":[[{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.sns","resource":"sns-lambda","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node14","type":"sns"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.lambda","resource":"integration-tests-js-XXXX-process-input-traced_node14","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"aws.lambda","type":"serverless"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"getRecordIds","resource":"getRecordIds","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node14"}]]} END Duration: XXXX ms Memory Used: XXXX MB START {"e":XXXX,"m":"aws.lambda.enhanced.invocations","t":["region:sa-east-1","account_id:XXXX","functionname:integration-tests-js-XXXX-process-input-traced_node14","resource:integration-tests-js-XXXX-process-input-traced_node14","cold_start:false","memorysize:1024","datadog_lambda:vX.X.X","runtime:nodejs14.x"],"v":1} {"e":XXXX,"m":"serverless.integration_test.execution","t":["function:process-input-traced","dd_lambda_layer:datadog-nodev14.XX.X"],"v":1} -{"traces":[[{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"getRecordIds","resource":"getRecordIds","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node14"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.lambda","resource":"integration-tests-js-XXXX-process-input-traced_node14","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"aws.lambda","type":"serverless"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.sqs","resource":"my-queue","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"my-queue","type":"web"}]]} +{"traces":[[{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.sqs","resource":"my-queue","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"my-queue","type":"web"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"aws.lambda","resource":"integration-tests-js-XXXX-process-input-traced_node14","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"aws.lambda","type":"serverless"},{"trace_id":"XXXX","span_id":"XXXX","parent_id":"XXXX","name":"getRecordIds","resource":"getRecordIds","error":0,"meta":{"XXXX": "XXXX"},"metrics":{"XXXX": "XXXX"},"start":XXXX,"duration":XXXX,"service":"integration-tests-js-XXXX-process-input-traced_node14"}]]} END Duration: XXXX ms Memory Used: XXXX MB diff --git a/integration_tests/snapshots/logs/throw-error-traced_node12.log b/integration_tests/snapshots/logs/throw-error-traced_node12.log index 402a01e6..c16974de 100644 --- a/integration_tests/snapshots/logs/throw-error-traced_node12.log +++ b/integration_tests/snapshots/logs/throw-error-traced_node12.log @@ -9,7 +9,7 @@ START XXXX-XX-XX XX:XX:XX.XXX ERROR [dd.trace_id=XXXX dd.span_id=XXXX] Invoke Error {"errorType":"Error","errorMessage":"Hello","stack":["Error: Hello"," at handle (/var/task/throw-error-traced.js:XXX:XXX)"," at /opt/nodejs/node_modules/datadog-lambda-js/utils/handler.js:XXX:XXX"," at /opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX"," at step (/opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX)"," at Object.next (/opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX)"," at /opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX"," at new Promise ()"," at __awaiter (/opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX)"," at /opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX"," at /opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX"]} END Duration: XXXX ms Memory Used: XXXX MB START -XXXX-XX-XX XX:XX:XX.XXX ERROR [dd.trace_id=XXXX dd.span_id=XXXX] Invoke Error {"errorType":"Error","errorMessage":"Hello","stack":["Error: Hello"," at handle (/var/task/throw-error-traced.js:XXX:XXX)"," at /opt/nodejs/node_modules/datadog-lambda-js/utils/handler.js:XXX:XXX"," at /opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX"," at step (/opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX)"," at Object.next (/opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX)"," at /opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX"," at new Promise ()"," at __awaiter (/opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX)"," at /opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX"," at /opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX"]} {"e":XXXX,"m":"aws.lambda.enhanced.invocations","t":["region:sa-east-1","account_id:XXXX","functionname:integration-tests-js-XXXX-throw-error-traced_node12","resource:integration-tests-js-XXXX-throw-error-traced_node12","cold_start:false","memorysize:1024","datadog_lambda:vX.X.X","runtime:nodejs12.x"],"v":1} {"e":XXXX,"m":"aws.lambda.enhanced.errors","t":["region:sa-east-1","account_id:XXXX","functionname:integration-tests-js-XXXX-throw-error-traced_node12","resource:integration-tests-js-XXXX-throw-error-traced_node12","cold_start:false","memorysize:1024","datadog_lambda:vX.X.X","runtime:nodejs12.x"],"v":1} +XXXX-XX-XX XX:XX:XX.XXX ERROR [dd.trace_id=XXXX dd.span_id=XXXX] Invoke Error {"errorType":"Error","errorMessage":"Hello","stack":["Error: Hello"," at handle (/var/task/throw-error-traced.js:XXX:XXX)"," at /opt/nodejs/node_modules/datadog-lambda-js/utils/handler.js:XXX:XXX"," at /opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX"," at step (/opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX)"," at Object.next (/opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX)"," at /opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX"," at new Promise ()"," at __awaiter (/opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX)"," at /opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX"," at /opt/nodejs/node_modules/datadog-lambda-js/index.js:XXX:XXX"]} END Duration: XXXX ms Memory Used: XXXX MB diff --git a/package.json b/package.json index 9580617a..42e66fe8 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "@types/node": "^15.6.1", "@types/promise-retry": "^1.1.3", "@types/shimmer": "^1.0.1", - "dd-trace": "^2.1.1", + "dd-trace": "^2.3.1", "jest": "^27.0.1", "mock-fs": "4.14.0", "nock": "13.0.11", diff --git a/src/trace/context.spec.ts b/src/trace/context.spec.ts index 13ddc650..e0d04d22 100644 --- a/src/trace/context.spec.ts +++ b/src/trace/context.spec.ts @@ -261,6 +261,48 @@ describe("readTraceFromEvent", () => { traceID: "6966585609680374559", }); }); + + it("can parse an SNS message source passing Binary trace context", () => { + const result = readTraceFromEvent({ + Records: [ + { + EventSource: "aws:sns", + EventVersion: "1.0", + EventSubscriptionArn: + "arn:aws:sns:sa-east-1:601427279990:aj-js-library-test-dev-solo-topic:1bd19208-a99a-46d9-8398-f90f8699c641", + Sns: { + Type: "Notification", + MessageId: "f19d39fa-8c61-5df9-8f49-639247b6cece", + TopicArn: "arn:aws:sns:sa-east-1:601427279990:aj-js-library-test-dev-solo-topic", + Subject: null, + Message: '{"hello":"there","ajTimestamp":1643039127879}', + Timestamp: "2022-01-24T15:45:27.968Z", + SignatureVersion: "1", + Signature: + "mzp2Ou0fASw4LYRxY6SSww7qFfofn4luCJBRaTjLpQ5uhwhsAUKdyLz9VPD+/dlRbi1ImsWtIZ7A+wxj1oV7Z2Gyu/N4RpGalae37+jTluDS7AhjgcD7Bs4bgQtFkCfMFEwbhICQfukLLzbwbgczZ4NTPn6zj5o28c5NBKSJMYSnLz82ohw77GgnZ/m26E32ZQNW4+VCEMINg9Ne2rHstwPWRXPr5xGTrx8jH8CNUZnVpFVfhU8o+OSeAdpzm2l99grHIo7qPhekERxANz6QHynMlhdzD3UNSgc3oZkamZban/NEKd4MKJzgNQdNOYVj3Kw6eF2ZweEoBQ5sSFK5fQ==", + SigningCertUrl: + "https://sns.sa-east-1.amazonaws.com/SimpleNotificationService-7ff5318490ec183fbaddaa2a969abfda.pem", + UnsubscribeUrl: + "https://sns.sa-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:sa-east-1:601427279990:aj-js-library-test-dev-solo-topic:1bd19208-a99a-46d9-8398-f90f8699c641", + MessageAttributes: { + _datadog: { + Type: "Binary", + Value: + "eyJ4LWRhdGFkb2ctdHJhY2UtaWQiOiI3MTAyMjkxNjI4NDQzMTM0OTE5IiwieC1kYXRhZG9nLXBhcmVudC1pZCI6IjQyNDc1NTAxMDE2NDg2MTg2MTgiLCJ4LWRhdGFkb2ctc2FtcGxpbmctcHJpb3JpdHkiOiIxIn0=", + }, + }, + }, + }, + ], + }); + expect(result).toEqual({ + parentID: "4247550101648618618", + sampleMode: 1, + source: "event", + traceID: "7102291628443134919", + }); + }); + it("can read from SNS message delivered to SQS queue source", () => { const result = readTraceFromEvent({ Records: [ @@ -283,6 +325,42 @@ describe("readTraceFromEvent", () => { }, ], }); + expect(result).toEqual({ + parentID: "4493917105238181843", + sampleMode: 1, + source: "event", + traceID: "2776434475358637757", + }); + }); + + it("can read from SNS message delivered to SQS queue source with Binary trace context", () => { + const result = readTraceFromEvent({ + Records: [ + { + messageId: "64812b68-4d9b-4dca-b3fb-9b18f255ee51", + receiptHandle: + "AQEBER6aRkfG8092GvkL7FRwCwbQ7LLDW9Tlk/CembqHe+suS2kfFxXiukomvaIN61QoyQMoRgWuV52SDkiQno2u+5hP64BDbmw+e/KR9ayvIfHJ3M6RfyQLaWNWm3hDFBCKTnBMVIxtdx0N9epZZewyokjKcrNYtmCghFgTCvZzsQkowi5rnoHAVHJ3je1c3bDnQ1KLrZFgajDnootYXDwEPuMq5FIxrf4EzTe0S7S+rnRm+GaQfeBLBVAY6dASL9usV3/AFRqDtaI7GKI+0F2NCgLlqj49VlPRz4ldhkGknYlKTZTluAqALWLJS62/J1GQo53Cs3nneJcmu5ajB2zzmhhRXoXINEkLhCD5ujZfcsw9H4xqW69Or4ECvlqx14bUU2rtMIW0QM2p7pEeXnyocymQv6m1te113eYWTVmaJ4I=", + body: '{\n "Type" : "Notification",\n "MessageId" : "0a0ab23e-4861-5447-82b7-e8094ff3e332",\n "TopicArn" : "arn:aws:sns:sa-east-1:601427279990:js-library-test-dev-demoTopic-15WGUVRCBMPAA",\n "Message" : "{\\"hello\\":\\"harv\\",\\"nice of you to join us\\":\\"david\\",\\"anotherThing\\":{\\"foo\\":\\"bar\\",\\"blah\\":null,\\"harv\\":123},\\"vals\\":[{\\"thingOne\\":1},{\\"thingTwo\\":2}],\\"ajTimestamp\\":1639777617957}",\n "Timestamp" : "2021-12-17T21:46:58.040Z",\n "SignatureVersion" : "1",\n "Signature" : "FR35/7E8C3LHEVk/rC4XxXlXwV/5mNkFNPgDhHSnJ2I6hIoSrTROAm7h5xm1PuBkAeFDvq0zofw91ouk9zZyvhdrMLFIIgrjEyNayRmEffmoEAkzLFUsgtQX7MmTl644r4NuWiM0Oiz7jueRvIcKXcZr7Nc6GJcWV1ymec8oOmuHNMisnPMxI07LIQVYSyAfv6P9r2jEWMVIukRoCzwTnRk4bUUYhPSGHI7OC3AsxxXBbv8snqTrLM/4z2rXCf6jHCKNxWeLlm9/45PphCkEyx5BWS4/71KaoMWUWy8+6CCsy+uF3XTCVmvSEYLyEwTSzOY+vCUjazrRW93498i70g==",\n "SigningCertURL" : "https://sns.sa-east-1.amazonaws.com/SimpleNotificationService-7ff5318490ec183fbaddaa2a969abfda.pem",\n "UnsubscribeURL" : "https://sns.sa-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:sa-east-1:601427279990:js-library-test-dev-demoTopic-15WGUVRCBMPAA:1290f550-9a8a-4e8f-a900-8f5f96dcddda",\n "MessageAttributes" : {\n "_datadog" : {"Type":"Binary","Value":"eyJ4LWRhdGFkb2ctdHJhY2UtaWQiOiI3MTAyMjkxNjI4NDQzMTM0OTE5IiwieC1kYXRhZG9nLXBhcmVudC1pZCI6IjQyNDc1NTAxMDE2NDg2MTg2MTgiLCJ4LWRhdGFkb2ctc2FtcGxpbmctcHJpb3JpdHkiOiIxIn0="}\n }\n}', + attributes: { + ApproximateReceiveCount: "1", + SentTimestamp: "1639777618130", + SenderId: "AIDAIOA2GYWSHW4E2VXIO", + ApproximateFirstReceiveTimestamp: "1639777618132", + }, + messageAttributes: {}, + md5OfBody: "ee19d8b1377919239ad3fd5dabc33739", + eventSource: "aws:sqs", + eventSourceARN: "arn:aws:sqs:sa-east-1:601427279990:aj-js-library-test-dev-demo-queue", + awsRegion: "sa-east-1", + }, + ], + }); + expect(result).toEqual({ + parentID: "4247550101648618618", + sampleMode: 1, + source: "event", + traceID: "7102291628443134919", + }); }); it("can read context from EventBridge messages", () => { diff --git a/src/trace/context.ts b/src/trace/context.ts index 49d2cf5a..90d532d8 100644 --- a/src/trace/context.ts +++ b/src/trace/context.ts @@ -246,7 +246,13 @@ export function readTraceFromSNSSQSEvent(event: SQSEvent): TraceContext | undefi parsedBody.MessageAttributes._datadog && parsedBody.MessageAttributes._datadog.Value ) { - const traceData = JSON.parse(parsedBody.MessageAttributes._datadog.Value); + let traceData; + if (parsedBody.MessageAttributes._datadog.Type === "String") { + traceData = JSON.parse(parsedBody.MessageAttributes._datadog.Value); + } else { + const b64Decoded = Buffer.from(parsedBody.MessageAttributes._datadog.Value, "base64").toString("ascii"); + traceData = JSON.parse(b64Decoded); + } const traceID = traceData[traceIDHeader]; const parentID = traceData[parentIDHeader]; const sampledHeader = traceData[samplingPriorityHeader]; @@ -340,7 +346,15 @@ export function readTraceFromEventbridgeEvent(event: EventBridgeEvent) export function readTraceFromSNSEvent(event: SNSEvent): TraceContext | undefined { if (event?.Records?.[0]?.Sns?.MessageAttributes?._datadog?.Value) { try { - const traceData = JSON.parse(event.Records[0].Sns.MessageAttributes._datadog.Value); + let traceData; + if (event.Records[0].Sns.MessageAttributes._datadog.Type === "String") { + traceData = JSON.parse(event.Records[0].Sns.MessageAttributes._datadog.Value); + } else { + const b64Decoded = Buffer.from(event.Records[0].Sns.MessageAttributes._datadog.Value, "base64").toString( + "ascii", + ); + traceData = JSON.parse(b64Decoded); + } const traceID = traceData[traceIDHeader]; const parentID = traceData[parentIDHeader]; const sampledHeader = traceData[samplingPriorityHeader]; diff --git a/yarn.lock b/yarn.lock index cda03229..c4efa6cd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -845,11 +845,6 @@ asynckit@^0.4.0: resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" integrity sha1-x57Zf380y48robyXkLzDZkdLS3k= -atob@^2.1.2: - version "2.1.2" - resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9" - integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg== - aws-sdk@*: version "2.1054.0" resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.1054.0.tgz#70196fcc90b066d892e6f848046542dd1f23a4ca" @@ -1199,10 +1194,10 @@ data-urls@^2.0.0: whatwg-mimetype "^2.3.0" whatwg-url "^8.0.0" -dd-trace@^2.1.1: - version "2.1.1" - resolved "https://registry.yarnpkg.com/dd-trace/-/dd-trace-2.1.1.tgz#42c5d035512d1a65aa9a3f7881dfb3a72f642c0b" - integrity sha512-zV8npDF1fHtnf4GPu/FStV7q7df8NK7FyKAzIrsqtdq/ZXbpGZ1dqm2Pfd4g66vODAjjMIBwEAN6j7IydDZEgQ== +dd-trace@^2.3.1: + version "2.3.1" + resolved "https://registry.yarnpkg.com/dd-trace/-/dd-trace-2.3.1.tgz#bec4d159fcef3ffc63e090581394e8cb597d0c75" + integrity sha512-vCQ7bs9a1WfqZA1JBWEZJsQDRT/rYeN6BVOI9rWGW/KJyIcUfOhP4n0fctvEOCpumJx4+H0vUP2hBog7YFkSAw== dependencies: "@datadog/native-appsec" "^0.8.1" "@datadog/native-metrics" "^1.1.0" @@ -1212,7 +1207,7 @@ dd-trace@^2.1.1: crypto-randomuuid "^1.0.0" diagnostics_channel "^1.1.0" form-data "^3.0.0" - import-in-the-middle "^1.1.2" + import-in-the-middle "^1.2.1" koalas "^1.0.2" limiter "^1.1.4" lodash.kebabcase "^4.1.1" @@ -1227,8 +1222,6 @@ dd-trace@^2.1.1: performance-now "^2.1.0" retry "^0.10.1" semver "^5.5.0" - source-map "^0.7.3" - source-map-resolve "^0.6.0" debug@4, debug@^4.1.0, debug@^4.1.1: version "4.3.3" @@ -1242,11 +1235,6 @@ decimal.js@^10.2.1: resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.3.1.tgz#d8c3a444a9c6774ba60ca6ad7261c3a94fd5e783" integrity sha512-V0pfhfr8suzyPGOx3nmq4aHqabehUZn6Ch9kyFpV79TGDTWFmHqUqXdabR7QHqxzrYolF4+tVmJhUG4OURg5dQ== -decode-uri-component@^0.2.0: - version "0.2.0" - resolved "https://registry.yarnpkg.com/decode-uri-component/-/decode-uri-component-0.2.0.tgz#eb3913333458775cb84cd1a1fae062106bb87545" - integrity sha1-6zkTMzRYd1y4TNGh+uBiEGu4dUU= - dedent@^0.7.0: version "0.7.0" resolved "https://registry.yarnpkg.com/dedent/-/dedent-0.7.0.tgz#2495ddbaf6eb874abb0e1be9df22d2e5a544326c" @@ -1609,10 +1597,10 @@ ieee754@^1.1.4: resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352" integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA== -import-in-the-middle@^1.1.2: - version "1.2.0" - resolved "https://registry.yarnpkg.com/import-in-the-middle/-/import-in-the-middle-1.2.0.tgz#203a910e08e7e3db8a0cb3abb85d919459162cbf" - integrity sha512-i2OrkKW2UH/W4asmzQztA5qjYxMFLh5BaOUEWLv6Ra0MDT+5v7P/7x5/ESZgG9LOaKijYjXnMfupzzy66tfcyA== +import-in-the-middle@^1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/import-in-the-middle/-/import-in-the-middle-1.2.1.tgz#30d4e98be7329eee0d284943dd0df092cc422b3c" + integrity sha512-KdYqCJbJWBOU9740nr9lrmCDhW7htxY1dHmbP4iUEeCaxupj2fKFhyHixsly2WmxMbRIsxzSWSJMfGNEU7el+w== dependencies: module-details-from-path "^1.0.3" @@ -2878,14 +2866,6 @@ slash@^3.0.0: resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634" integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q== -source-map-resolve@^0.6.0: - version "0.6.0" - resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.6.0.tgz#3d9df87e236b53f16d01e58150fc7711138e5ed2" - integrity sha512-KXBr9d/fO/bWo97NXsPIAW1bFSBOuCnjbNTBMO7N59hsv5i9yzRDfcYwwt0l04+VqnKC+EwzvJZIP/qkuMgR/w== - dependencies: - atob "^2.1.2" - decode-uri-component "^0.2.0" - source-map-support@^0.5.6: version "0.5.21" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.21.tgz#04fe7c7f9e1ed2d662233c28cb2b35b9f63f6e4f"