diff --git a/src/bot.ts b/src/bot.ts index e45de5d..3eeee91 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -1,3 +1,4 @@ +import { Sentry } from './instrument' import express from 'express' import asyncHandler from 'express-async-handler' import { @@ -44,9 +45,7 @@ import { run } from '@grammyjs/runner' import { runBotHeartBit } from './monitoring/monitoring' import { type BotPaymentLog } from './database/stats.service' import { TelegramPayments } from './modules/telegram_payment' -import * as Sentry from '@sentry/node' import * as Events from 'events' -import { ProfilingIntegration } from '@sentry/profiling-node' import { ES } from './es' import { hydrateFiles } from '@grammyjs/files' import { VoiceTranslateBot } from './modules/voice-translate' @@ -96,20 +95,17 @@ bot.use( }) ) -Sentry.init({ - dsn: config.sentry.dsn, - release: config.commitHash, - integrations: [ - new ProfilingIntegration() - ], - tracesSampleRate: 1.0, // Performance Monitoring. Should use 0.1 in production - profilesSampleRate: 1.0 // Set sampling rate for profiling - this is relative to tracesSampleRate -}) - -Sentry.setTags({ botName: config.botName }) - ES.init() +try { + console.log('FCO:::: Sentry.isInitialized()', Sentry.isInitialized()) + throw new Error('ERROR') +} catch (e) { + console.log('FCO::::: HERE', e) + Sentry.captureException(e) + console.log('FCO::::: AFTER') +} + bot.use(async (ctx: BotContext, next: NextFunction): Promise => { ctx.transient = { refunded: false, @@ -126,28 +122,45 @@ bot.use(async (ctx: BotContext, next: NextFunction): Promise => { paymentFiatCredits: 0 } } - const transaction = Sentry.startTransaction({ name: 'bot-command' }) - const entities = ctx.entities() + const startTime = now() + const entities = ctx.entities() let command = '' - for (const ent of entities) { - if (ent.type === 'bot_command') { - command = ent.text.substring(1) - const userId = ctx.message?.from?.id - const username = ctx.message?.from?.username - if (userId) { - Sentry.setUser({ id: userId, username }) + + await Sentry.startSpan( + { + name: 'Bot Command Processing', + op: 'bot.command' + }, + async (span) => { + // Process bot commands and set Sentry context + for (const ent of entities) { + if (ent.type === 'bot_command') { + command = ent.text.substring(1) + const userId = ctx.message?.from?.id + const username = ctx.message?.from?.username + if (userId) { + Sentry.setUser({ id: userId, username }) + } + if (command) { + Sentry.setTag('command', command) + span?.setTag('command', command) + } + break + } } - if (command) { - Sentry.setTag('command', command) + + try { + await next() + } catch (error) { + if (span) { + span.setStatus('error') + Sentry.captureException(error) + } + throw error } - // there should be only one bot command - break } - } - - await next() - transaction.finish() + ) if (ctx.transient.analytics.module) { const userId = Number(ctx.message?.from?.id ?? '0') diff --git a/src/instrument.ts b/src/instrument.ts new file mode 100644 index 0000000..f83f060 --- /dev/null +++ b/src/instrument.ts @@ -0,0 +1,17 @@ +import * as Sentry from '@sentry/node' +import config from './config' +import { ProfilingIntegration } from '@sentry/profiling-node' + +Sentry.init({ + dsn: config.sentry.dsn, + release: config.commitHash, + integrations: [ + new ProfilingIntegration() + ], + tracesSampleRate: 0.1, // Performance Monitoring. Should use 0.1 in production + profilesSampleRate: 1.0 // Set sampling rate for profiling - this is relative to tracesSampleRate +}) + +Sentry.setTags({ botName: config.botName }) + +export { Sentry } diff --git a/src/modules/payment/index.ts b/src/modules/payment/index.ts index c0a4656..be0b160 100644 --- a/src/modules/payment/index.ts +++ b/src/modules/payment/index.ts @@ -237,7 +237,7 @@ export class BotPayments { try { const web3 = new Web3(this.rpcURL) web3.eth.accounts.wallet.add(accountFrom) - + this.logger.error(`Transfering funds from ${accountFrom.address} to ${addressTo}`) const gasPrice = await web3.eth.getGasPrice() let nonce @@ -435,7 +435,6 @@ export class BotPayments { const totalPayAmount = await this.getPriceInONE(amountUSD) const { totalCreditsAmount } = await chatService.getUserCredits(accountId) const totalBalanceDelta = totalCreditsAmount.minus(totalPayAmount) - this.logger.info( `[${from.id} @${ from.username diff --git a/src/modules/schedule/harmonyApi.ts b/src/modules/schedule/harmonyApi.ts index 27b3eaa..50c7d82 100644 --- a/src/modules/schedule/harmonyApi.ts +++ b/src/modules/schedule/harmonyApi.ts @@ -4,6 +4,8 @@ import { abbreviateNumber, getPercentDiff } from './utils' const rpcUrl = 'https://rpc.s0.t.hmny.io' +// 'https://api.harmony.one' + const rpcRequest = async (method: string, params: any[] = []): Promise => { const { data } = await axios.post(rpcUrl, { jsonrpc: '2.0', @@ -37,17 +39,13 @@ export const getAddressBalance = async (address: string): Promise => { export const getBotFee = async (address: string, daysCount: number): Promise => { const history = await getAddressHistory(address) - - const startTimestamp = moment().subtract(daysCount, 'days').unix() - + const startTimestamp = moment().subtract(7, 'days').unix() const total = history.reduce((acc, item) => { if (item.timestamp < startTimestamp) { return acc } - return acc + item.value }, 0) - return total / Math.pow(10, 18) }