diff --git a/pagermaid/__init__.py b/pagermaid/__init__.py index a56d7e3..717579a 100644 --- a/pagermaid/__init__.py +++ b/pagermaid/__init__.py @@ -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() diff --git a/pagermaid/listener.py b/pagermaid/listener.py index ec485b6..e8f43b0 100644 --- a/pagermaid/listener.py +++ b/pagermaid/listener.py @@ -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( "上报错误出错了呜呜呜 ~。" diff --git a/requirements.txt b/requirements.txt index f121e97..890d8e4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -34,3 +34,4 @@ cheroot PySocks certifi magic_google +sentry-sdk