diff --git a/package.json b/package.json index 434a231..0e880f1 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "license": "MIT", "dependencies": { "@godaddy/terminus": "^4.12.1", + "aws-xray-sdk": "^3.10.2", "cls-hooked": "^4.2.2", "dataloader": "^2.0.0", "express": "^5.0.1", diff --git a/src/app.ts b/src/app.ts index 39c596a..2aede11 100644 --- a/src/app.ts +++ b/src/app.ts @@ -5,6 +5,7 @@ import cors from './middlewares/cors'; import trace from './middlewares/trace'; import context from './context'; import schema from './schema'; +import AWSXRay from './logger/AWSXRay'; const app = express(); @@ -15,9 +16,11 @@ app.use(trace); app.use(responseTime); app.use(cors); +if (process.env.AWS_XRAY_ENABLED === 'true') app.use(AWSXRay.express.openSegment('express-graphql-example')); app.all('/graphql', createHandler({ schema, context: context as any, })); +if (process.env.AWS_XRAY_ENABLED === 'true') app.use(AWSXRay.express.closeSegment()); export default app; diff --git a/src/logger/AWSXRay.ts b/src/logger/AWSXRay.ts new file mode 100644 index 0000000..ee04221 --- /dev/null +++ b/src/logger/AWSXRay.ts @@ -0,0 +1,15 @@ +import AWSXRay from 'aws-xray-sdk'; +// import http from 'http'; +// import https from 'https'; + +AWSXRay.config([AWSXRay.plugins.ECSPlugin]); + +if (typeof process.env.AWS_XRAY_DAEMON_ADDRESS !== 'undefined') { + AWSXRay.setDaemonAddress(process.env.AWS_XRAY_DAEMON_ADDRESS); +} + +// AWSXRay.captureHTTPsGlobal(http); +// AWSXRay.captureHTTPsGlobal(https); +// AWSXRay.capturePromise(); + +export default AWSXRay; diff --git a/src/middlewares/trace.ts b/src/middlewares/trace.ts index 7c469e9..984975f 100644 --- a/src/middlewares/trace.ts +++ b/src/middlewares/trace.ts @@ -8,8 +8,8 @@ export default function trace(req: Request, res: Response, next: NextFunction): return; } - namespace.bind(req); - namespace.bind(res); + namespace.bind(req); + namespace.bind(res); const traceId = uuidv7(); diff --git a/yarn.lock b/yarn.lock index 895f5b2..8fdc1ad 100644 --- a/yarn.lock +++ b/yarn.lock @@ -15,6 +15,16 @@ __metadata: languageName: node linkType: hard +"@aws-sdk/types@npm:^3.4.1": + version: 3.723.0 + resolution: "@aws-sdk/types@npm:3.723.0" + dependencies: + "@smithy/types": "npm:^4.0.0" + tslib: "npm:^2.6.2" + checksum: 10c0/b13f2ef66a0de96df9a6ff227531579483b0d7a735ca3a936ba881d528ccae8b36d568f69914c343c972c0b84057366947980ed2ab60c642443564c2ad3739fe + languageName: node + linkType: hard + "@babel/code-frame@npm:^7.0.0, @babel/code-frame@npm:^7.12.13, @babel/code-frame@npm:^7.25.9, @babel/code-frame@npm:^7.26.0": version: 7.26.2 resolution: "@babel/code-frame@npm:7.26.2" @@ -874,6 +884,33 @@ __metadata: languageName: node linkType: hard +"@smithy/service-error-classification@npm:^2.0.4": + version: 2.1.5 + resolution: "@smithy/service-error-classification@npm:2.1.5" + dependencies: + "@smithy/types": "npm:^2.12.0" + checksum: 10c0/e3fb24af5a3a60bf6479bc057bc832f89a0b427650ea2f262220a9627d60d2ab455ad766a2fcceb55fba1b15eddb82b25775fb781c254d2031768f5f8e131e15 + languageName: node + linkType: hard + +"@smithy/types@npm:^2.12.0": + version: 2.12.0 + resolution: "@smithy/types@npm:2.12.0" + dependencies: + tslib: "npm:^2.6.2" + checksum: 10c0/3530ba5b4f4e52a4028679f73e133af928cf6ea22a16d29669b8c67ea540ed46ab15dc6d391598fbdfd476884cdc57881c480168e2dbe7c5bb007f5afad01531 + languageName: node + linkType: hard + +"@smithy/types@npm:^4.0.0": + version: 4.1.0 + resolution: "@smithy/types@npm:4.1.0" + dependencies: + tslib: "npm:^2.6.2" + checksum: 10c0/d8817145ea043c5b29783df747ed47c3a1c584fd9d02bbdb609d38b7cb4dded1197ac214ae112744c86abe0537a314dae0edbc0e752bb639ef2d9fb84c67a9d9 + languageName: node + linkType: hard + "@types/babel__core@npm:^7.1.14": version: 7.20.5 resolution: "@types/babel__core@npm:7.20.5" @@ -925,6 +962,15 @@ __metadata: languageName: node linkType: hard +"@types/cls-hooked@npm:^4.3.3": + version: 4.3.9 + resolution: "@types/cls-hooked@npm:4.3.9" + dependencies: + "@types/node": "npm:*" + checksum: 10c0/fd531903b2cd7fa76d36ec81142ac55f0ea8702f6d76238a1ca5c773b8e35889cf528198a8a83b5e551f928f47bdfad947e29d9ebbe0df712cbf14510f7ae713 + languageName: node + linkType: hard + "@types/connect@npm:*": version: 3.4.38 resolution: "@types/connect@npm:3.4.38" @@ -946,7 +992,7 @@ __metadata: languageName: node linkType: hard -"@types/express@npm:^5.0.0": +"@types/express@npm:*, @types/express@npm:^5.0.0": version: 5.0.0 resolution: "@types/express@npm:5.0.0" dependencies: @@ -1023,6 +1069,15 @@ __metadata: languageName: node linkType: hard +"@types/mysql@npm:*": + version: 2.15.26 + resolution: "@types/mysql@npm:2.15.26" + dependencies: + "@types/node": "npm:*" + checksum: 10c0/3cf279e7db05d56c0544532a4380b9079f579092379a04c8138bd5cf88dda5b31208ac2d23ce7dbf4e3a3f43aaeed44e72f9f19f726518f308efe95a7435619a + languageName: node + linkType: hard + "@types/node@npm:*, @types/node@npm:^22.5.0": version: 22.8.7 resolution: "@types/node@npm:22.8.7" @@ -1032,6 +1087,17 @@ __metadata: languageName: node linkType: hard +"@types/pg@npm:*": + version: 8.11.10 + resolution: "@types/pg@npm:8.11.10" + dependencies: + "@types/node": "npm:*" + pg-protocol: "npm:*" + pg-types: "npm:^4.0.1" + checksum: 10c0/c8800d0ab2c6424308e6c6b40c73f19583ee1aed758462bd07694844b0a551b5841442205a4ee05207b80109ba502f33f20241b1bd9b4902e713611fb9e08f6c + languageName: node + linkType: hard + "@types/qs@npm:*": version: 6.9.16 resolution: "@types/qs@npm:6.9.16" @@ -1395,6 +1461,13 @@ __metadata: languageName: node linkType: hard +"atomic-batcher@npm:^1.0.2": + version: 1.0.2 + resolution: "atomic-batcher@npm:1.0.2" + checksum: 10c0/6511385b61eca9ba1feedd85b78ba601818aba6fffc65ba3ba1b361d32f4a36b2e735c4d65a316568163beeb76bae7a21d3b043c1a1e5e2b5fa116048f5f7d2b + languageName: node + linkType: hard + "aws-ssl-profiles@npm:^1.1.1": version: 1.1.2 resolution: "aws-ssl-profiles@npm:1.1.2" @@ -1402,6 +1475,65 @@ __metadata: languageName: node linkType: hard +"aws-xray-sdk-core@npm:3.10.2": + version: 3.10.2 + resolution: "aws-xray-sdk-core@npm:3.10.2" + dependencies: + "@aws-sdk/types": "npm:^3.4.1" + "@smithy/service-error-classification": "npm:^2.0.4" + "@types/cls-hooked": "npm:^4.3.3" + atomic-batcher: "npm:^1.0.2" + cls-hooked: "npm:^4.2.2" + semver: "npm:^7.5.3" + checksum: 10c0/0d47b13687f841092e927c919085d0570aca67ad9a12251f1d8395c9628f6fcc738d962897032b993c3cf1a94e11cdbe64d23cfbd6106adde732bb46495d5c9e + languageName: node + linkType: hard + +"aws-xray-sdk-express@npm:3.10.2": + version: 3.10.2 + resolution: "aws-xray-sdk-express@npm:3.10.2" + dependencies: + "@types/express": "npm:*" + peerDependencies: + aws-xray-sdk-core: ^3.10.2 + checksum: 10c0/8bc5846394eadf9ea2f89e5d28e2219cde88d9390a8b83b23ad4983efcd85d037f17deb9d627b1784205aae25d54a759532c17f027ef0cd492f4e927ccfc5379 + languageName: node + linkType: hard + +"aws-xray-sdk-mysql@npm:3.10.2": + version: 3.10.2 + resolution: "aws-xray-sdk-mysql@npm:3.10.2" + dependencies: + "@types/mysql": "npm:*" + peerDependencies: + aws-xray-sdk-core: ^3.10.2 + checksum: 10c0/b612825a3140a8b9c28262758dfe5184e15c6d0652fe46254d64728ed1183883059e16e506baac19d109e170e65b683acc1a4a5bbc5fd69ea3329691c086cc14 + languageName: node + linkType: hard + +"aws-xray-sdk-postgres@npm:3.10.2": + version: 3.10.2 + resolution: "aws-xray-sdk-postgres@npm:3.10.2" + dependencies: + "@types/pg": "npm:*" + peerDependencies: + aws-xray-sdk-core: ^3.10.2 + checksum: 10c0/190dde93c458f712fd42946c172ac0dca1290ff3a5ce781273d22bb0a5992d5689b08bd96f0c541ca2870bf79a7e900b883399cc2ab1d08daac114b3ae3c6618 + languageName: node + linkType: hard + +"aws-xray-sdk@npm:^3.10.2": + version: 3.10.2 + resolution: "aws-xray-sdk@npm:3.10.2" + dependencies: + aws-xray-sdk-core: "npm:3.10.2" + aws-xray-sdk-express: "npm:3.10.2" + aws-xray-sdk-mysql: "npm:3.10.2" + aws-xray-sdk-postgres: "npm:3.10.2" + checksum: 10c0/7bd956d893ac23d48b7a569117c7c9aa62ed55d538ec288cbf22ea588829e0f5cffefe59a9e865b20ac2c8ad54711fc940291c0baa58138c541bec461992276b + languageName: node + linkType: hard + "babel-jest@npm:^29.7.0": version: 29.7.0 resolution: "babel-jest@npm:29.7.0" @@ -2469,6 +2601,7 @@ __metadata: "@types/node": "npm:^22.5.0" "@typescript-eslint/eslint-plugin": "npm:^5.9.0" "@typescript-eslint/parser": "npm:^5.9.0" + aws-xray-sdk: "npm:^3.10.2" cls-hooked: "npm:^4.2.2" dataloader: "npm:^2.0.0" eslint: "npm:^8.6.0" @@ -4504,6 +4637,13 @@ __metadata: languageName: node linkType: hard +"obuf@npm:~1.1.2": + version: 1.1.2 + resolution: "obuf@npm:1.1.2" + checksum: 10c0/520aaac7ea701618eacf000fc96ae458e20e13b0569845800fc582f81b386731ab22d55354b4915d58171db00e79cfcd09c1638c02f89577ef092b38c65b7d81 + languageName: node + linkType: hard + "on-finished@npm:2.4.1, on-finished@npm:^2.4.1": version: 2.4.1 resolution: "on-finished@npm:2.4.1" @@ -4698,6 +4838,42 @@ __metadata: languageName: node linkType: hard +"pg-int8@npm:1.0.1": + version: 1.0.1 + resolution: "pg-int8@npm:1.0.1" + checksum: 10c0/be6a02d851fc2a4ae3e9de81710d861de3ba35ac927268973eb3cb618873a05b9424656df464dd43bd7dc3fc5295c3f5b3c8349494f87c7af50ec59ef14e0b98 + languageName: node + linkType: hard + +"pg-numeric@npm:1.0.2": + version: 1.0.2 + resolution: "pg-numeric@npm:1.0.2" + checksum: 10c0/43dd9884e7b52c79ddc28d2d282d7475fce8bba13452d33c04ceb2e0a65f561edf6699694e8e1c832ff9093770496363183c950dd29608e1bdd98f344b25bca9 + languageName: node + linkType: hard + +"pg-protocol@npm:*": + version: 1.7.0 + resolution: "pg-protocol@npm:1.7.0" + checksum: 10c0/c4af854d9b843c808231c0040fed89f2b9101006157df8da2bb2f62a7dde702de748d852228dc22df41cc7ffddfb526af3bcb34b278b581e9f76a060789186c1 + languageName: node + linkType: hard + +"pg-types@npm:^4.0.1": + version: 4.0.2 + resolution: "pg-types@npm:4.0.2" + dependencies: + pg-int8: "npm:1.0.1" + pg-numeric: "npm:1.0.2" + postgres-array: "npm:~3.0.1" + postgres-bytea: "npm:~3.0.0" + postgres-date: "npm:~2.1.0" + postgres-interval: "npm:^3.0.0" + postgres-range: "npm:^1.1.1" + checksum: 10c0/780fccda2f3fa2a34e85a72e8e7dadb7d88fbe71ce88f126cb3313f333ad836d02488ec4ff3d94d0c1e5846f735d6e6c6281f8059e6b8919d2180429acaec3e2 + languageName: node + linkType: hard + "picocolors@npm:^1.0.0, picocolors@npm:^1.1.1": version: 1.1.1 resolution: "picocolors@npm:1.1.1" @@ -4728,6 +4904,43 @@ __metadata: languageName: node linkType: hard +"postgres-array@npm:~3.0.1": + version: 3.0.2 + resolution: "postgres-array@npm:3.0.2" + checksum: 10c0/644aa071f67a66a59f641f8e623887d2b915bc102a32643e2aa8b54c11acd343c5ad97831ea444dd37bd4b921ba35add4aa2cb0c6b76700a8252c2324aeba5b4 + languageName: node + linkType: hard + +"postgres-bytea@npm:~3.0.0": + version: 3.0.0 + resolution: "postgres-bytea@npm:3.0.0" + dependencies: + obuf: "npm:~1.1.2" + checksum: 10c0/41c79cc48aa730c5ba3eda6ab989a940034f07a1f57b8f2777dce56f1b8cca16c5870582932b5b10cc605048aef9b6157e06253c871b4717cafc6d00f55376aa + languageName: node + linkType: hard + +"postgres-date@npm:~2.1.0": + version: 2.1.0 + resolution: "postgres-date@npm:2.1.0" + checksum: 10c0/00a7472c10788f6b0d08d24108bf1eb80858de1bd6317740198a564918ea4a69b80c98148167b92ae688abd606483020d0de0dd3a36f3ea9a3e26bbeef3464f4 + languageName: node + linkType: hard + +"postgres-interval@npm:^3.0.0": + version: 3.0.0 + resolution: "postgres-interval@npm:3.0.0" + checksum: 10c0/8b570b30ea37c685e26d136d34460f246f98935a1533defc4b53bb05ee23ae3dc7475b718ec7ea607a57894d8c6b4f1adf67ca9cc83a75bdacffd427d5c68de8 + languageName: node + linkType: hard + +"postgres-range@npm:^1.1.1": + version: 1.1.4 + resolution: "postgres-range@npm:1.1.4" + checksum: 10c0/254494ef81df208e0adeae6b66ce394aba37914ea14c7ece55a45fb6691b7db04bee74c825380a47c887a9f87158fd3d86f758f9cc60b76d3a38ce5aca7912e8 + languageName: node + linkType: hard + "prelude-ls@npm:^1.2.1": version: 1.2.1 resolution: "prelude-ls@npm:1.2.1" @@ -5673,6 +5886,13 @@ __metadata: languageName: node linkType: hard +"tslib@npm:^2.6.2": + version: 2.8.1 + resolution: "tslib@npm:2.8.1" + checksum: 10c0/9c4759110a19c53f992d9aae23aac5ced636e99887b51b9e61def52611732872ff7668757d4e4c61f19691e36f4da981cd9485e869b4a7408d689f6bf1f14e62 + languageName: node + linkType: hard + "tsutils@npm:^3.21.0": version: 3.21.0 resolution: "tsutils@npm:3.21.0"