Skip to content

Commit

Permalink
create instrument.ts file refactor sentry configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
fegloff committed Dec 10, 2024
1 parent 1542715 commit 27e2fb0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 38 deletions.
75 changes: 44 additions & 31 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Sentry } from './instrument'
import express from 'express'
import asyncHandler from 'express-async-handler'
import {
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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<void> => {
ctx.transient = {
refunded: false,
Expand All @@ -126,28 +122,45 @@ bot.use(async (ctx: BotContext, next: NextFunction): Promise<void> => {
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')
Expand Down
17 changes: 17 additions & 0 deletions src/instrument.ts
Original file line number Diff line number Diff line change
@@ -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 }
3 changes: 1 addition & 2 deletions src/modules/payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/modules/schedule/harmonyApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> => {
const { data } = await axios.post(rpcUrl, {
jsonrpc: '2.0',
Expand Down Expand Up @@ -37,17 +39,13 @@ export const getAddressBalance = async (address: string): Promise<number> => {

export const getBotFee = async (address: string, daysCount: number): Promise<number> => {
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)
}

Expand Down

0 comments on commit 27e2fb0

Please sign in to comment.