Browse Source

feat(revisions): add aws-xray-sdk

Karol Sójko 1 year ago
parent
commit
126989df82

+ 1 - 0
.pnp.cjs

@@ -6188,6 +6188,7 @@ const RAW_RUNTIME_STATE =
           ["@types/node", "npm:20.5.7"],\
           ["@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"],\
           ["cors", "npm:2.8.5"],\
           ["dotenv", "npm:16.1.3"],\
           ["eslint", "npm:8.41.0"],\

+ 17 - 0
packages/revisions/bin/server.ts

@@ -3,6 +3,7 @@ import 'reflect-metadata'
 import * as cors from 'cors'
 import { urlencoded, json, Request, Response, NextFunction } from 'express'
 import * as winston from 'winston'
+import * as AWSXRay from 'aws-xray-sdk'
 
 import { InversifyExpressServer } from 'inversify-express-utils'
 import TYPES from '../src/Bootstrap/Types'
@@ -11,14 +12,26 @@ import { ContainerConfigLoader } from '../src/Bootstrap/Container'
 
 import '../src/Infra/InversifyExpress/AnnotatedRevisionsController'
 import '../src/Infra/InversifyExpress/AnnotatedHealthCheckController'
+import { ServiceIdentifier } from '@standardnotes/domain-core'
 
 const container = new ContainerConfigLoader()
 void container.load().then((container) => {
   const env: Env = container.get(TYPES.Revisions_Env)
 
+  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.Revisions))
+    }
+
     app.use((_request: Request, response: Response, next: NextFunction) => {
       response.setHeader('X-Revisions-Version', container.get(TYPES.Revisions_VERSION))
       next()
@@ -45,6 +58,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}`)

+ 9 - 0
packages/revisions/bin/worker.ts

@@ -1,6 +1,7 @@
 import 'reflect-metadata'
 
 import { Logger } from 'winston'
+import * as AWSXRay from 'aws-xray-sdk'
 
 import TYPES from '../src/Bootstrap/Types'
 import { Env } from '../src/Bootstrap/Env'
@@ -12,6 +13,14 @@ 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.enableManualMode()
+    AWSXRay.config([AWSXRay.plugins.ECSPlugin])
+  }
+
   const logger: Logger = container.get(TYPES.Revisions_Logger)
 
   logger.info('Starting worker...')

+ 1 - 0
packages/revisions/package.json

@@ -36,6 +36,7 @@
     "@standardnotes/responses": "^1.13.27",
     "@standardnotes/security": "workspace:^",
     "@standardnotes/time": "workspace:^",
+    "aws-xray-sdk": "^3.5.2",
     "cors": "2.8.5",
     "dotenv": "^16.0.1",
     "express": "^4.18.2",

+ 16 - 2
packages/revisions/src/Bootstrap/Container.ts

@@ -3,6 +3,7 @@ import Redis from 'ioredis'
 import { Container, interfaces } from 'inversify'
 import { MongoRepository, Repository } from 'typeorm'
 import * as winston from 'winston'
+import { captureAWSv3Client } from 'aws-xray-sdk'
 import { SNSClient, SNSClientConfig } from '@aws-sdk/client-sns'
 
 import { Revision } from '../Domain/Revision/Revision'
@@ -30,12 +31,12 @@ import {
   DomainEventPublisherInterface,
 } from '@standardnotes/domain-events'
 import {
-  SQSNewRelicEventMessageHandler,
   SQSEventMessageHandler,
   SQSDomainEventSubscriberFactory,
   DirectCallEventMessageHandler,
   DirectCallDomainEventPublisher,
   SNSDomainEventPublisher,
+  SQSXRayEventMessageHandler,
 } from '@standardnotes/domain-events-infra'
 import { DumpRepositoryInterface } from '../Domain/Dump/DumpRepositoryInterface'
 import { AccountDeletionRequestedEventHandler } from '../Domain/Handler/AccountDeletionRequestedEventHandler'
@@ -90,6 +91,7 @@ export class ContainerConfigLoader {
 
     const isConfiguredForHomeServer = env.get('MODE', true) === 'home-server'
     const isConfiguredForSelfHosting = env.get('MODE', true) === 'self-hosted'
+    const isConfiguredForAWSProduction = !isConfiguredForHomeServer && !isConfiguredForSelfHosting
     const isConfiguredForHomeServerOrSelfHosting = isConfiguredForHomeServer || isConfiguredForSelfHosting
     const isSecondaryDatabaseEnabled = env.get('SECONDARY_DB_ENABLED', true) === 'true'
     const isConfiguredForInMemoryCache = env.get('CACHE_TYPE', true) === 'memory'
@@ -173,6 +175,10 @@ export class ContainerConfigLoader {
           }
         }
 
+        if (isConfiguredForAWSProduction) {
+          return captureAWSv3Client(new SNSClient(snsConfig))
+        }
+
         return new SNSClient(snsConfig)
       })
 
@@ -201,6 +207,10 @@ export class ContainerConfigLoader {
           }
         }
 
+        if (isConfiguredForAWSProduction) {
+          return captureAWSv3Client(new SQSClient(sqsConfig))
+        }
+
         return new SQSClient(sqsConfig)
       })
 
@@ -213,6 +223,10 @@ export class ContainerConfigLoader {
             apiVersion: 'latest',
             region: env.get('S3_AWS_REGION', true),
           })
+
+          if (isConfiguredForAWSProduction) {
+            return captureAWSv3Client(s3Client)
+          }
         }
 
         return s3Client
@@ -511,7 +525,7 @@ export class ContainerConfigLoader {
         .bind<DomainEventMessageHandlerInterface>(TYPES.Revisions_DomainEventMessageHandler)
         .toConstantValue(
           env.get('NEW_RELIC_ENABLED', true) === 'true'
-            ? new SQSNewRelicEventMessageHandler(eventHandlers, container.get(TYPES.Revisions_Logger))
+            ? new SQSXRayEventMessageHandler(eventHandlers, container.get(TYPES.Revisions_Logger))
             : new SQSEventMessageHandler(eventHandlers, container.get(TYPES.Revisions_Logger)),
         )
 

+ 1 - 0
yarn.lock

@@ -5079,6 +5079,7 @@ __metadata:
     "@types/node": "npm:^20.5.7"
     "@typescript-eslint/eslint-plugin": "npm:^6.5.0"
     "@typescript-eslint/parser": "npm:^6.5.0"
+    aws-xray-sdk: "npm:^3.5.2"
     cors: "npm:2.8.5"
     dotenv: "npm:^16.0.1"
     eslint: "npm:^8.39.0"