📈 使用 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. """
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.path import exists
from sys import version_info, platform
@ -107,6 +112,39 @@ else:
bot = TelegramClient("pagermaid", api_key, api_hash, auto_reconnect=True)
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():
try:
redis.ping()

View File

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

View File

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