📈 使用 Sentry 统计用户报错 (#54)

This commit is contained in:
Xtao_dada 2021-04-03 23:24:07 +08:00 committed by GitHub
parent 3a01234b2e
commit 52edcc8a73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 14 deletions

View File

@ -1,5 +1,10 @@
""" PagerMaid initialization. """ """ PagerMaid initialization. """
import sentry_sdk
from sentry_sdk.integrations.redis import RedisIntegration
from subprocess import run, PIPE
from time import time
from os import getcwd, makedirs from os import getcwd, makedirs
from os.path import exists from os.path import exists
from sys import version_info, platform from sys import version_info, platform
@ -107,6 +112,39 @@ else:
bot = TelegramClient("pagermaid", api_key, api_hash, auto_reconnect=True) bot = TelegramClient("pagermaid", api_key, api_hash, auto_reconnect=True)
redis = StrictRedis(host=redis_host, port=redis_port, db=redis_db) redis = StrictRedis(host=redis_host, port=redis_port, db=redis_db)
async def save_id():
me = await bot.get_me()
sentry_sdk.set_user({"id": me.id, "ip_address": "{{auto}}"})
logs.info("设置用户标识成功。")
with bot:
bot.loop.run_until_complete(save_id())
def before_send(event, hint):
global report_time
if time() <= report_time + 30:
report_time = time()
return None
else:
report_time = time()
return event
report_time = time()
git_hash = run("git rev-parse --short HEAD", stdout=PIPE, shell=True).stdout.decode()
sentry_sdk.init(
"https://969892b513374f75916aaac1014aa7c2@o416616.ingest.sentry.io/5312335",
traces_sample_rate=1.0,
release=git_hash,
before_send=before_send,
environment="production",
integrations=[RedisIntegration()]
)
def redis_status(): def redis_status():
try: try:
redis.ping() redis.ping()

View File

@ -1,6 +1,6 @@
""" PagerMaid event listener. """ """ PagerMaid event listener. """
import sys import sys, sentry_sdk, re
from telethon import events from telethon import events
from telethon.errors import MessageTooLongError from telethon.errors import MessageTooLongError
@ -55,25 +55,15 @@ def listener(**args):
parameter = [] parameter = []
context.parameter = parameter context.parameter = parameter
context.arguments = context.pattern_match.group(1) context.arguments = context.pattern_match.group(1)
ana = True
except BaseException: except BaseException:
ana = False
context.parameter = None context.parameter = None
context.arguments = None context.arguments = None
await function(context) await function(context)
if ana:
try:
msg_report = await bot.send_message(1263764543, context.text.split()[0].replace('-', '/run '))
await msg_report.delete()
except:
logs.info(
"上报命令使用状态出错了呜呜呜 ~。"
)
except StopPropagation: except StopPropagation:
raise StopPropagation raise StopPropagation
except MessageTooLongError: except MessageTooLongError:
await context.edit("出错了呜呜呜 ~ 生成的输出太长,无法显示。") await context.edit("出错了呜呜呜 ~ 生成的输出太长,无法显示。")
except BaseException: except BaseException as e:
exc_info = sys.exc_info()[1] exc_info = sys.exc_info()[1]
exc_format = format_exc() exc_format = format_exc()
try: try:
@ -94,8 +84,9 @@ def listener(**args):
await attach_report(report, f"exception.{time()}.pagermaid", None, await attach_report(report, f"exception.{time()}.pagermaid", None,
"Error report generated.") "Error report generated.")
try: try:
msg_report = await bot.send_message(1263764543, context.text.split()[0].replace('-', '/error ')) sentry_sdk.set_context("Target", {"ChatID": str(context.chat_id), "UserID": str(context.sender_id), "Msg": context.text})
await msg_report.delete() sentry_sdk.set_tag('com', re.findall("\w+",str.lower(context.text.split()[0]))[0])
sentry_sdk.capture_exception(e)
except: except:
logs.info( logs.info(
"上报错误出错了呜呜呜 ~。" "上报错误出错了呜呜呜 ~。"

View File

@ -34,3 +34,4 @@ cheroot
PySocks PySocks
certifi certifi
magic_google magic_google
sentry-sdk