Skip to content

Commit

Permalink
🐛 fix duplicate sentry handler
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Oct 18, 2023
1 parent 6c8dd08 commit d7557e2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
8 changes: 5 additions & 3 deletions src/plugins/nonebot_plugin_sentry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
* @Author : yanyongyu
* @Date : 2020-11-23 20:23:12
* @LastEditors : yanyongyu
* @LastEditTime : 2023-10-02 16:44:50
* @LastEditTime : 2023-10-18 10:29:34
* @Description : None
* @GitHub : https://github.com/yanyongyu
-->

<!-- markdownlint-disable MD033 MD036 MD041 -->

<p align="center">
<a href="https://nonebot.dev/">
<img src="https://nonebot.dev/logo.png" height="100" alt="nonebot">
Expand Down Expand Up @@ -40,6 +42,6 @@ _✨ 在 Sentry.io 上进行 NoneBot 服务日志查看、错误处理 ✨_

## 配置项

配置项需要添加前缀 `sentry_`,所有参数以及具体含义参考: [Sentry Docs](https://docs.sentry.io/platforms/python/configuration/options/)
配置项需要添加前缀 `SENTRY_`,所有参数以及具体含义参考: [Sentry Docs](https://docs.sentry.io/platforms/python/configuration/options/)

所有以 `sentry_` 开头的配置项将会被自动读取。
所有以 `SENTRY_` 开头的配置项将会被自动读取。
14 changes: 3 additions & 11 deletions src/plugins/nonebot_plugin_sentry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@Author : yanyongyu
@Date : 2020-11-23 18:44:25
@LastEditors : yanyongyu
@LastEditTime : 2023-10-02 17:07:39
@LastEditTime : 2023-10-18 10:32:17
@Description : Sentry plugin
@GitHub : https://github.com/yanyongyu
"""
Expand All @@ -11,7 +11,6 @@
import sentry_sdk
from nonebot import logger, get_driver
from nonebot.plugin import PluginMetadata
from sentry_sdk.integrations.logging import EventHandler, BreadcrumbHandler

from .config import Config

Expand All @@ -34,15 +33,8 @@ def init_sentry(config: Config):
sentry_config = {key[7:]: value for key, value in config.dict().items()}
sentry_sdk.init(**sentry_config)

logger.add(
EventHandler("ERROR"),
filter=lambda r: r["level"].no >= logger.level("ERROR").no,
)
logger.add(
BreadcrumbHandler("INFO"),
filter=lambda r: r["level"].no >= logger.level("INFO").no,
)


if config.sentry_dsn:
init_sentry(config)
else:
logger.warning("Sentry DSN not provided! Sentry plugin disabled!")
10 changes: 2 additions & 8 deletions src/plugins/nonebot_plugin_sentry/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
@Author : yanyongyu
@Date : 2020-11-23 18:44:18
@LastEditors : yanyongyu
@LastEditTime : 2023-10-11 11:24:03
@LastEditTime : 2023-10-18 10:32:28
@Description : Config for Sentry plugin
@GitHub : https://github.com/yanyongyu
"""
__author__ = "yanyongyu"

from typing import Any, Dict, List, Optional

from nonebot import logger, get_driver
from nonebot import get_driver
from sentry_sdk.integrations import Integration
from sentry_sdk.integrations.loguru import LoguruIntegration
from pydantic import Extra, Field, BaseModel, validator, root_validator
Expand All @@ -36,12 +36,6 @@ def filter_sentry_configs(cls, values: Dict[str, Any]):
key: value for key, value in values.items() if key.startswith("sentry_")
}

@validator("sentry_dsn", allow_reuse=True)
def validate_dsn(cls, v: Optional[str]):
if not v:
logger.warning("Sentry DSN not provided! Sentry plugin disabled!")
return v

@validator("sentry_integrations", allow_reuse=True)
def validate_integrations(cls, v: List[Integration]):
ids = {i.identifier for i in v}
Expand Down

0 comments on commit d7557e2

Please sign in to comment.