Skip to content

Commit

Permalink
update and activate sentry configuration + sentry multiple enviroment
Browse files Browse the repository at this point in the history
  • Loading branch information
fegloff committed Dec 11, 2024
1 parent 27e2fb0 commit fadabc3
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 77 deletions.
64 changes: 32 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"description": "Harmony One Telegram Bot",
"main": "dist/bot.js",
"scripts": {
"build": "tsc",
"build": "tsc && npm run sentry:sourcemaps",
"start": "node dist/bot.js",
"dev": "env-cmd ts-node-dev src/bot.ts",
"test": "jest",
"typeorm": "typeorm-ts-node-commonjs -d src/database/datasource.ts",
"lint": "eslint . --ext .ts",
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org hiddenstate --project harmony-ai-bot ./dist && sentry-cli sourcemaps upload --org hiddenstate --project harmony-ai-bot ./dist",
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org harmony-23 --project harmony1bot ./dist && sentry-cli sourcemaps upload --org harmony-23 --project harmony1bot ./dist",
"prepare": "husky install"
},
"repository": {
Expand Down Expand Up @@ -44,7 +44,7 @@
"@babel/core": "^7.22.15",
"@babel/preset-env": "^7.22.15",
"@babel/preset-typescript": "^7.22.15",
"@sentry/cli": "^2.20.7",
"@sentry/cli": "^2.39.1",
"@types/express": "^4.17.14",
"@types/jest": "^29.5.4",
"@types/node": "^18.15.11",
Expand Down Expand Up @@ -125,4 +125,4 @@
"web3-utils": "^4.0.5",
"websocket": "^1.0.34"
}
}
}
11 changes: 1 addition & 10 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Sentry } from './instrument'
import { Sentry } from './monitoring/instrument'
import express from 'express'
import asyncHandler from 'express-async-handler'
import {
Expand Down Expand Up @@ -97,15 +97,6 @@ bot.use(

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 Down
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ export default {
},
betteruptime: { botHeartBitId: process.env.BOT_HEARTBIT_ID ?? '' },
telegramPayments: { token: process.env.TELEGRAM_PAYMENTS_TOKEN ?? '' },
sentry: { dsn: process.env.SENTRY_DSN },
sentry: {
dsn: process.env.SENTRY_DSN,
env: process.env.SENTRY_ENVIRONMENT ?? 'local'
},
es: {
url: process.env.ES_URL ?? '',
username: process.env.ES_USERNAME ?? '',
Expand Down
17 changes: 0 additions & 17 deletions src/instrument.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/modules/1country/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GrammyError, InlineKeyboard } from 'grammy'
import { AxiosError } from 'axios'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { type Logger, pino } from 'pino'

import { chatService } from '../../database/services'
Expand Down
4 changes: 1 addition & 3 deletions src/modules/errorhandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AxiosError } from 'axios'
import * as Sentry from '@sentry/node' // Import Sentry for error capturing
import { Sentry } from '../monitoring/instrument'
import { RequestState, type OnCallBackQueryData, type OnMessageContext } from './types'
import { sleep } from './sd-images/utils'
import { type Logger } from 'pino'
Expand All @@ -9,8 +9,6 @@ import { now } from '../utils/perf'
import OpenAI from 'openai'
import config from '../config'

// const MAX_TRIES = 3 // Define the maximum number of retries

class ErrorHandler {
public maxTries = 3

Expand Down
2 changes: 1 addition & 1 deletion src/modules/hmny/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GrammyError } from 'grammy'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { type Logger, pino } from 'pino'
import {
type OnMessageContext,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/llms/dalleBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
sendMessage
} from './utils/helpers'
import { type LlmCompletion } from './api/llmApi'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { LlmsBase } from './llmsBase'
import config from '../../config'
import { now } from '../../utils/perf'
Expand Down
2 changes: 1 addition & 1 deletion src/modules/llms/llmsBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
splitTelegramMessage
} from './utils/helpers'
import { type LlmCompletion, deleteCollection } from './api/llmApi'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { now } from '../../utils/perf'
import { type ModelParameters, type ChatModel, type LLMModel } from './utils/types'
import { ErrorHandler } from '../errorhandler'
Expand Down
2 changes: 1 addition & 1 deletion src/modules/llms/lumaBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
PRICE_ADJUSTMENT,
sendMessage
} from './utils/helpers'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { LlmsBase } from './llmsBase'
import config from '../../config'
import { now } from '../../utils/perf'
Expand Down
2 changes: 1 addition & 1 deletion src/modules/llms/openaiBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
SupportedCommands
} from './utils/helpers'
import { type LlmCompletion } from './api/llmApi'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { LlmsBase } from './llmsBase'
import config from '../../config'
import { now } from '../../utils/perf'
Expand Down
2 changes: 1 addition & 1 deletion src/modules/payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { LRUCache } from 'lru-cache'
import { freeCreditsFeeCounter } from '../../metrics/prometheus'
import { type BotPaymentLog } from '../../database/stats.service'
import { sendMessage } from '../llms/utils/helpers'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { InlineKeyboard } from 'grammy'
import { Callbacks } from '../types'
import { type InvoiceParams } from '../../database/invoice.service'
Expand Down
2 changes: 1 addition & 1 deletion src/modules/qrcode/QRCodeBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ComfyClient } from './comfy/ComfyClient'
import crypto from 'crypto'
import buildQRWorkflow from './comfy/buildQRWorkflow'
import pino, { type Logger } from 'pino'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { now } from '../../utils/perf'

enum SupportedCommands {
Expand Down
2 changes: 2 additions & 0 deletions src/modules/schedule/bridgeAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from 'axios'
import moment from 'moment'
import { Sentry } from '../../monitoring/instrument'
import { abbreviateNumber, getPercentDiff } from './utils'
import { BigNumber } from 'bignumber.js'
import pino from 'pino'
Expand Down Expand Up @@ -182,6 +183,7 @@ export const getAvgStakes = async (): Promise<number> => {
}, BigNumber(0)).div(values.length).div(10 ** 18).toNumber()
} catch (e) {
logger.error(e)
Sentry.captureException(e)
return 0
}
}
2 changes: 1 addition & 1 deletion src/modules/sd-images/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SDImagesBotBase } from './SDImagesBotBase'
import { COMMAND, type IOperation, parseCtx, promptHasBadWords } from './helpers'
import { getModelByParam, MODELS_CONFIGS } from './api'
import { sendMessage } from '../llms/utils/helpers'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { OPERATION_STATUS, completeOperation } from './balancer'
import { now } from '../../utils/perf'

Expand Down
2 changes: 1 addition & 1 deletion src/modules/voice-memo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Kagi } from './kagi'
import MessageMediaDocument = Api.MessageMediaDocument
import { InputFile } from 'grammy'
import { bot } from '../../bot'
import * as Sentry from '@sentry/node'
import { Sentry } from '../../monitoring/instrument'
import { now } from '../../utils/perf'
import { isAdmin } from '../llms/utils/context'
import { VOICE_MEMO_FORWARDING } from '../../constants'
Expand Down
19 changes: 19 additions & 0 deletions src/monitoring/environments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import config from '../config'

export type Environment = 'local' | 'test' | 'production'

export function getEnvironment (): Environment {
// Use SENTRY_ENVIRONMENT if available
const sentryEnv = config.sentry.env.toLowerCase()
if (sentryEnv) {
// Validate that it's one of our expected environments
if (['local', 'test', 'production'].includes(sentryEnv)) {
return sentryEnv as Environment
}
// If invalid value, log warning and default to production
console.warn(`Invalid SENTRY_ENVIRONMENT value: ${sentryEnv}. Defaulting to production`)
return 'production'
}
// If no SENTRY_ENVIRONMENT set, assume local development
return 'local'
}
37 changes: 37 additions & 0 deletions src/monitoring/instrument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as Sentry from '@sentry/node'
import config from '../config'
import { ProfilingIntegration } from '@sentry/profiling-node'
import { getEnvironment } from './environments'

const env = getEnvironment()

// Configure sample rates based on environment
const sampleRates = {
local: {
traces: 1.0, // Higher sampling in local for testing
profiles: 1.0
},
test: {
traces: 0.5, // Medium sampling in test
profiles: 1.0
},
production: {
traces: 0.1, // Lower sampling in production for performance
profiles: 1.0
}
}

Sentry.init({
dsn: config.sentry.dsn,
release: config.commitHash,
environment: env,
integrations: [
new ProfilingIntegration()
],
tracesSampleRate: sampleRates[env].traces,
profilesSampleRate: sampleRates[env].profiles
})

Sentry.setTags({ botName: config.botName })

export { Sentry }

0 comments on commit fadabc3

Please sign in to comment.