From e33893d943c8eb6827e3a5b54c22032413e45365 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Mon, 15 Jul 2024 09:25:40 -0300 Subject: [PATCH] chore: Add telemetry system Added new file 'telemetry.guard.ts' in 'src/api/guards' directory. Modified 'index.router.ts' in 'src/api/routes' directory to integrate the new telemetry system. This change improves the monitoring and data gathering capabilities of the application, allowing for better performance analysis and potential issue detection. --- src/api/guards/telemetry.guard.ts | 32 +++++++++++++++++++++++++++++++ src/api/routes/index.router.ts | 5 +++++ 2 files changed, 37 insertions(+) create mode 100644 src/api/guards/telemetry.guard.ts diff --git a/src/api/guards/telemetry.guard.ts b/src/api/guards/telemetry.guard.ts new file mode 100644 index 000000000..af8645d27 --- /dev/null +++ b/src/api/guards/telemetry.guard.ts @@ -0,0 +1,32 @@ +import axios from 'axios'; +import { NextFunction, Request, Response } from 'express'; +import fs from 'fs'; + +const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')); + +interface TelemetryData { + route: string; + apiVersion: string; + timestamp: Date; +} + +class Telemetry { + public collectTelemetry(req: Request, res: Response, next: NextFunction): void { + const telemetry: TelemetryData = { + route: req.path, + apiVersion: `${packageJson.version}`, + timestamp: new Date(), + }; + + axios + .post('https://log.evolution-api.com/telemetry', telemetry) + .then(() => {}) + .catch((error) => { + console.error('Telemetry error', error); + }); + + next(); + } +} + +export default Telemetry; diff --git a/src/api/routes/index.router.ts b/src/api/routes/index.router.ts index f809345bb..29b6150a1 100644 --- a/src/api/routes/index.router.ts +++ b/src/api/routes/index.router.ts @@ -4,6 +4,7 @@ import fs from 'fs'; import { configService, WaBusiness } from '../../config/env.config'; import { authGuard } from '../guards/auth.guard'; import { instanceExistsGuard, instanceLoggedGuard } from '../guards/instance.guard'; +import Telemetry from '../guards/telemetry.guard'; import { ChatwootRouter } from '../integrations/chatwoot/routes/chatwoot.router'; import { RabbitmqRouter } from '../integrations/rabbitmq/routes/rabbitmq.router'; import { S3Router } from '../integrations/s3/routes/s3.router'; @@ -36,11 +37,15 @@ const router = Router(); const serverConfig = configService.get('SERVER'); const guards = [instanceExistsGuard, instanceLoggedGuard, authGuard['apikey']]; +const telemetry = new Telemetry(); + const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')); if (!serverConfig.DISABLE_MANAGER) router.use('/manager', new ViewsRouter().router); router + .use((req, res, next) => telemetry.collectTelemetry(req, res, next)) + .get('/', (req, res) => { res.status(HttpStatus.OK).json({ status: HttpStatus.OK,