Ver Fonte

feat(api-gateaway): configure aws-xray-sdk

Karol Sójko há 1 ano atrás
pai
commit
dc76113915
4 ficheiros alterados com 20 adições e 0 exclusões
  1. 1 0
      .pnp.cjs
  2. 17 0
      packages/api-gateway/bin/server.ts
  3. 1 0
      packages/api-gateway/package.json
  4. 1 0
      yarn.lock

+ 1 - 0
.pnp.cjs

@@ -5797,6 +5797,7 @@ const RAW_RUNTIME_STATE =
           ["@types/prettyjson", "npm:0.0.30"],\
           ["@typescript-eslint/eslint-plugin", "virtual:fd909b174d079e30b336c4ce72c38a88c1e447767b1a8dd7655e07719a1e31b97807f0931368724fc78897ff15e6a6d00b83316c0f76d11f85111f342e08bb79#npm:6.5.0"],\
           ["@typescript-eslint/parser", "virtual:fd909b174d079e30b336c4ce72c38a88c1e447767b1a8dd7655e07719a1e31b97807f0931368724fc78897ff15e6a6d00b83316c0f76d11f85111f342e08bb79#npm:6.5.0"],\
+          ["aws-xray-sdk", "npm:3.5.2"],\
           ["axios", "npm:1.4.0"],\
           ["cors", "npm:2.8.5"],\
           ["dotenv", "npm:16.1.3"],\

+ 17 - 0
packages/api-gateway/bin/server.ts

@@ -29,6 +29,7 @@ import helmet from 'helmet'
 import * as cors from 'cors'
 import { text, json, Request, Response, NextFunction } from 'express'
 import * as winston from 'winston'
+import * as AWSXRay from 'aws-xray-sdk'
 // eslint-disable-next-line @typescript-eslint/no-var-requires
 const robots = require('express-robots-txt')
 
@@ -36,15 +37,27 @@ import { InversifyExpressServer } from 'inversify-express-utils'
 import { ContainerConfigLoader } from '../src/Bootstrap/Container'
 import { TYPES } from '../src/Bootstrap/Types'
 import { Env } from '../src/Bootstrap/Env'
+import { ServiceIdentifier } from '@standardnotes/domain-core'
 
 const container = new ContainerConfigLoader()
 void container.load().then((container) => {
   const env: Env = new Env()
   env.load()
 
+  const isConfiguredForAWSProduction =
+    env.get('MODE', true) !== 'home-server' && env.get('MODE', true) !== 'self-hosted'
+
+  if (isConfiguredForAWSProduction) {
+    AWSXRay.config([AWSXRay.plugins.ECSPlugin])
+  }
+
   const server = new InversifyExpressServer(container)
 
   server.setConfig((app) => {
+    if (isConfiguredForAWSProduction) {
+      app.use(AWSXRay.express.openSegment(ServiceIdentifier.NAMES.ApiGateway))
+    }
+
     app.use((_request: Request, response: Response, next: NextFunction) => {
       response.setHeader('X-API-Gateway-Version', container.get(TYPES.ApiGateway_VERSION))
       next()
@@ -104,6 +117,10 @@ void container.load().then((container) => {
 
   const serverInstance = server.build()
 
+  if (isConfiguredForAWSProduction) {
+    serverInstance.use(AWSXRay.express.closeSegment())
+  }
+
   serverInstance.listen(env.get('PORT'))
 
   logger.info(`Server started on port ${process.env.PORT}`)

+ 1 - 0
packages/api-gateway/package.json

@@ -31,6 +31,7 @@
     "@standardnotes/domain-events-infra": "workspace:*",
     "@standardnotes/security": "workspace:*",
     "@standardnotes/time": "workspace:*",
+    "aws-xray-sdk": "^3.5.2",
     "axios": "^1.1.3",
     "cors": "2.8.5",
     "dotenv": "^16.0.1",

+ 1 - 0
yarn.lock

@@ -4667,6 +4667,7 @@ __metadata:
     "@types/prettyjson": "npm:^0.0.30"
     "@typescript-eslint/eslint-plugin": "npm:^6.5.0"
     "@typescript-eslint/parser": "npm:^6.5.0"
+    aws-xray-sdk: "npm:^3.5.2"
     axios: "npm:^1.1.3"
     cors: "npm:2.8.5"
     dotenv: "npm:^16.0.1"