getsentry / getsentry/sentry-python

Debug logs duplication due to incorrect configuration

Đang mở
#3,944 4 bình luận 0 reaction 0 người được giao Xem trên GitHub
Improvement Python
Ngôn ngữ chính
Python
Star
2.2k
Fork
669
Merge trung bình
1 ngày 40 phút
Pull request đã merge (30 ngày)
212

Mô tả

### How do you use Sentry?

Self-hosted/on-premise

### Version

2.19.2

### Steps to Reproduce

Sentry duplicates log records when you set debug mode via `sentry_sdk.init()` and configure handler in root logger only.

In my project I use `structlog` so full code example with it

```python
import logging
import sys

import structlog

LOG_LEVEL = logging.DEBUG

def remove_processors_meta(
_,
__,
event_dict,
):
del event_dict["_record"]
del event_dict["_from_structlog"]
event_dict.pop("positional_args", None)
return event_dict

structlog.configure_once(
processors=[
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
formatter = structlog.stdlib.ProcessorFormatter(
pass_foreign_args=True,
processors=[
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S", utc=True),
structlog.dev.set_exc_info,
remove_processors_meta,
structlog.dev.ConsoleRenderer(
event_key="message",
exception_formatter=structlog.dev.better_traceback,
),
],
)

handler = logging.StreamHandler(stream=sys.stdout)
handler.setFormatter(formatter)

root_logger = logging.getLogger()
root_logger.addHandler(handler)
root_logger.setLevel(LOG_LEVEL)

l = logging.getLogger(__name__)
l.info("test1", extra={"extra": "one"})

import sentry_sdk # must be after configuration of logging and structlog
from sentry_sdk.utils import logger

sentry_sdk.init("https://123@localhost/1", debug=True)
```

### Expected Result

Sentry does not write log records via own stream handler and use parent (root) logger configuration

Output:
```
2025-01-18 06:44:33 [info ] [__main__] event=test1
2025-01-18 06:44:33 [debug ] [sentry_sdk.errors] event=Setting up integrations (with default = True)
2025-01-18 06:44:33 [debug ] [sentry_sdk.errors] event=Did not import default integration sentry_sdk.integrations.anthropic.AnthropicIntegration: Anthropic not installed
```

Image

### Actual Result

Output:
```
[sentry] DEBUG: Setting up integrations (with default = True)
2025-01-18 06:11:54 [debug ] Setting up integrations (with default = True) [sentry_sdk.errors] func_name=setup_integrations module=__init__ pathname=/<...>/.venv/lib/python3.11/site-packages/sentry_sdk/integrations/__init__.py process=69509 process_name=MainProcess thread=8643448640 thread_name=MainThread
```

Image

### Possible fix

In `debug.py` we should change configuration check and use special logger method to check parent handlers too

from this
```python

def init_debug_support():
# type: () -> None
if not logger.handlers:
configure_logger()
```

to this

```python
def init_debug_support():
# type: () -> None

if not logger.hasHandlers(): # check parent handlers
configure_logger()

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu trong debug.py tại init_debug_support và so sánh việc kiểm tra logger handler với cấu hình root logger được báo cáo. Tái hiện ví dụ với debug=True và xác minh rằng các bản ghi debug của Sentry xuất hiện một lần thông qua parent handler đã cấu hình, không có phần đầu ra bổ sung của Sentry stream-handler.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
observability
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.