From 5975a4a1357b56d09faf4628dba824087e10ecc3 Mon Sep 17 00:00:00 2001 From: Kristina Date: Fri, 9 Aug 2024 08:46:30 +0400 Subject: [PATCH] Add logs on notify request (#6295) Signed-off-by: Kristina Fefelova --- .../pod-telegram-bot/package.json | 2 + .../pod-telegram-bot/src/config.ts | 6 ++- .../pod-telegram-bot/src/server.ts | 37 +++++++++++-------- .../pod-telegram-bot/src/start.ts | 23 ++++++++++-- 4 files changed, 46 insertions(+), 22 deletions(-) diff --git a/services/telegram-bot/pod-telegram-bot/package.json b/services/telegram-bot/pod-telegram-bot/package.json index 2f3858f397..5697bde6d0 100644 --- a/services/telegram-bot/pod-telegram-bot/package.json +++ b/services/telegram-bot/pod-telegram-bot/package.json @@ -55,6 +55,8 @@ }, "dependencies": { "@hcengineering/activity": "^0.6.0", + "@hcengineering/analytics": "^0.6.0", + "@hcengineering/analytics-service": "^0.6.0", "@hcengineering/chunter": "^0.6.20", "@hcengineering/client": "^0.6.18", "@hcengineering/client-resources": "^0.6.27", diff --git a/services/telegram-bot/pod-telegram-bot/src/config.ts b/services/telegram-bot/pod-telegram-bot/src/config.ts index 40c92e4d21..49d3b6a640 100644 --- a/services/telegram-bot/pod-telegram-bot/src/config.ts +++ b/services/telegram-bot/pod-telegram-bot/src/config.ts @@ -26,6 +26,7 @@ export interface Config { OtpTimeToLiveSec: number OtpRetryDelaySec: number AccountsUrl: string + SentryDSN: string } const parseNumber = (str: string | undefined): number | undefined => (str !== undefined ? Number(str) : undefined) @@ -39,12 +40,13 @@ const config: Config = (() => { AccountsUrl: process.env.ACCOUNTS_URL, ServiceId: process.env.SERVICE_ID, Secret: process.env.SECRET, - Domain: process.env.DOMAIN, + Domain: process.env.DOMAIN ?? '', BotPort: parseNumber(process.env.BOT_PORT) ?? 8443, // TODO: later we should get this title from branding map App: process.env.APP ?? 'Huly', OtpTimeToLiveSec: parseNumber(process.env.OTP_TIME_TO_LIVE_SEC) ?? 60, - OtpRetryDelaySec: parseNumber(process.env.OTP_RETRY_DELAY_SEC) ?? 60 + OtpRetryDelaySec: parseNumber(process.env.OTP_RETRY_DELAY_SEC) ?? 60, + SentryDSN: process.env.SENTRY_DSN ?? '' } const missingEnv = (Object.keys(params) as Array).filter((key) => params[key] === undefined) diff --git a/services/telegram-bot/pod-telegram-bot/src/server.ts b/services/telegram-bot/pod-telegram-bot/src/server.ts index 66fe435471..86f4efcd51 100644 --- a/services/telegram-bot/pod-telegram-bot/src/server.ts +++ b/services/telegram-bot/pod-telegram-bot/src/server.ts @@ -86,6 +86,7 @@ const handleRequest = async ( const token = extractToken(req.headers) await fn(req, res, token, next) } catch (err: unknown) { + console.error('Error during extract token', err) next(err) } } @@ -94,7 +95,7 @@ const wrapRequest = (fn: AsyncRequestHandler) => (req: Request, res: Response, n void handleRequest(fn, req, res, next) } -export function createServer (bot: Telegraf, worker: PlatformWorker): Express { +export function createServer (bot: Telegraf, worker: PlatformWorker, ctx: MeasureContext): Express { const limiter = new Limiter() const app = express() @@ -174,28 +175,32 @@ export function createServer (bot: Telegraf, worker: PlatformWorker): Express { app.post( '/notify', wrapRequest(async (req, res, token) => { + ctx.info('Received notification', { email: token.email }) if (req.body == null || !Array.isArray(req.body)) { + ctx.error('Invalid request body', { body: req.body, email: token.email }) throw new ApiError(400) } const notificationRecords = req.body as TelegramNotificationRecord[] - const usersRecords = await worker.getUsersRecords() + const userRecord = await worker.getUserRecordByEmail(token.email) + + if (userRecord === undefined) { + ctx.error('User not found', { email: token.email }) + throw new ApiError(404) + } for (const notificationRecord of notificationRecords) { - const userRecord = usersRecords.find((record) => record.email === token.email) - if (userRecord !== undefined) { - void limiter.add(userRecord.telegramId, async () => { - const formattedMessage = toTelegramHtml(notificationRecord) - const message = await bot.telegram.sendMessage(userRecord.telegramId, formattedMessage, { - parse_mode: 'HTML' - }) - await worker.addNotificationRecord({ - notificationId: notificationRecord.notificationId, - email: userRecord.email, - workspace: notificationRecord.workspace, - telegramId: message.message_id - }) + void limiter.add(userRecord.telegramId, async () => { + const formattedMessage = toTelegramHtml(notificationRecord) + const message = await bot.telegram.sendMessage(userRecord.telegramId, formattedMessage, { + parse_mode: 'HTML' }) - } + await worker.addNotificationRecord({ + notificationId: notificationRecord.notificationId, + email: userRecord.email, + workspace: notificationRecord.workspace, + telegramId: message.message_id + }) + }) } res.status(200) diff --git a/services/telegram-bot/pod-telegram-bot/src/start.ts b/services/telegram-bot/pod-telegram-bot/src/start.ts index ffdaab9d05..855c84fe75 100644 --- a/services/telegram-bot/pod-telegram-bot/src/start.ts +++ b/services/telegram-bot/pod-telegram-bot/src/start.ts @@ -13,10 +13,13 @@ // limitations under the License. // -import { MeasureMetricsContext } from '@hcengineering/core' +import { MeasureMetricsContext, newMetrics } from '@hcengineering/core' import { setMetadata } from '@hcengineering/platform' import serverToken from '@hcengineering/server-token' import serverClient from '@hcengineering/server-client' +import { SplitLogger, configureAnalytics } from '@hcengineering/analytics-service' +import { Analytics } from '@hcengineering/analytics' +import { join } from 'path' import config from './config' import { createServer, listen } from './server' @@ -24,17 +27,29 @@ import { setUpBot } from './bot' import { PlatformWorker } from './worker' import { registerLoaders } from './loaders' +const ctx = new MeasureMetricsContext( + 'telegram-bot-service', + {}, + {}, + newMetrics(), + new SplitLogger('telegram-bot-service', { + root: join(process.cwd(), 'logs'), + enableConsole: (process.env.ENABLE_CONSOLE ?? 'true') === 'true' + }) +) + +configureAnalytics(config.SentryDSN, config) +Analytics.setTag('application', 'telegram-bot-service') + export const start = async (): Promise => { setMetadata(serverToken.metadata.Secret, config.Secret) setMetadata(serverClient.metadata.Endpoint, config.AccountsUrl) setMetadata(serverClient.metadata.UserAgent, config.ServiceId) registerLoaders() - const ctx = new MeasureMetricsContext('telegram-bot', {}) - const worker = await PlatformWorker.create() const bot = await setUpBot(worker) - const app = createServer(bot, worker) + const app = createServer(bot, worker, ctx) if (config.Domain === '') { void bot.launch({ dropPendingUpdates: true })