1+ import { Sentry } from './monitoring/instrument'
12import express from 'express'
23import asyncHandler from 'express-async-handler'
34import {
@@ -44,9 +45,7 @@ import { run } from '@grammyjs/runner'
4445import { runBotHeartBit } from './monitoring/monitoring'
4546import { type BotPaymentLog } from './database/stats.service'
4647import { TelegramPayments } from './modules/telegram_payment'
47- import * as Sentry from '@sentry/node'
4848import * as Events from 'events'
49- import { ProfilingIntegration } from '@sentry/profiling-node'
5049import { ES } from './es'
5150import { hydrateFiles } from '@grammyjs/files'
5251import { VoiceTranslateBot } from './modules/voice-translate'
@@ -96,18 +95,6 @@ bot.use(
9695 } )
9796)
9897
99- Sentry . init ( {
100- dsn : config . sentry . dsn ,
101- release : config . commitHash ,
102- integrations : [
103- new ProfilingIntegration ( )
104- ] ,
105- tracesSampleRate : 1.0 , // Performance Monitoring. Should use 0.1 in production
106- profilesSampleRate : 1.0 // Set sampling rate for profiling - this is relative to tracesSampleRate
107- } )
108-
109- Sentry . setTags ( { botName : config . botName } )
110-
11198ES . init ( )
11299
113100bot . use ( async ( ctx : BotContext , next : NextFunction ) : Promise < void > => {
@@ -126,28 +113,45 @@ bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
126113 paymentFiatCredits : 0
127114 }
128115 }
129- const transaction = Sentry . startTransaction ( { name : 'bot-command' } )
130- const entities = ctx . entities ( )
116+
131117 const startTime = now ( )
118+ const entities = ctx . entities ( )
132119 let command = ''
133- for ( const ent of entities ) {
134- if ( ent . type === 'bot_command' ) {
135- command = ent . text . substring ( 1 )
136- const userId = ctx . message ?. from ?. id
137- const username = ctx . message ?. from ?. username
138- if ( userId ) {
139- Sentry . setUser ( { id : userId , username } )
120+
121+ await Sentry . startSpan (
122+ {
123+ name : 'Bot Command Processing' ,
124+ op : 'bot.command'
125+ } ,
126+ async ( span ) => {
127+ // Process bot commands and set Sentry context
128+ for ( const ent of entities ) {
129+ if ( ent . type === 'bot_command' ) {
130+ command = ent . text . substring ( 1 )
131+ const userId = ctx . message ?. from ?. id
132+ const username = ctx . message ?. from ?. username
133+ if ( userId ) {
134+ Sentry . setUser ( { id : userId , username } )
135+ }
136+ if ( command ) {
137+ Sentry . setTag ( 'command' , command )
138+ span ?. setTag ( 'command' , command )
139+ }
140+ break
141+ }
140142 }
141- if ( command ) {
142- Sentry . setTag ( 'command' , command )
143+
144+ try {
145+ await next ( )
146+ } catch ( error ) {
147+ if ( span ) {
148+ span . setStatus ( 'error' )
149+ Sentry . captureException ( error )
150+ }
151+ throw error
143152 }
144- // there should be only one bot command
145- break
146153 }
147- }
148-
149- await next ( )
150- transaction . finish ( )
154+ )
151155
152156 if ( ctx . transient . analytics . module ) {
153157 const userId = Number ( ctx . message ?. from ?. id ?? '0' )
0 commit comments