DataDog / dd-trace-py

Datadog Python APM Client
https://ddtrace.readthedocs.io/
Other
512 stars 404 forks source link

ddtrace.patch breaks serializing loguru logger #8939

Open ikseek opened 4 months ago

ikseek commented 4 months ago

Summary of problem

Patching loguru logger in serializing mode breaks it when certain named arguments (for example file) are passed to log entry.

Which version of dd-trace-py are you using?

ddtrace 2.8.0

Which version of pip are you using?

pip 24.0

Which libraries and their versions are you using?

`pip freeze` ``` attrs==23.2.0 bytecode==0.15.1 cattrs==23.2.3 ddsketch==3.0.1 ddtrace==2.8.0 Deprecated==1.2.14 envier==0.5.1 importlib-metadata==7.0.0 loguru==0.7.2 opentelemetry-api==1.24.0 protobuf==5.26.1 setuptools==69.2.0 six==1.16.0 sqlparse==0.4.4 typing_extensions==4.11.0 wrapt==1.16.0 xmltodict==0.13.0 zipp==3.18.1 ```

How can we reproduce your problem?

from os import environ
environ['LOGURU_SERIALIZE'] = '1'

import ddtrace
import loguru

ddtrace.patch(loguru=True)

loguru.logger.error("Message with file attribute: {file}", file="example")

What is the result that you get?

--- Logging error in Loguru Handler #0 ---
Record was: {'elapsed': datetime.timedelta(microseconds=11578), 'exception': None, 'extra': {'file': 'example', 'dd.trace_id': '0', 'dd.span_id': '0', 'dd.env': '', 'dd.service': '', 'dd.version': ''}, 'file': 'example', 'function': '<module>', 'level': (name='ERROR', no=40, icon='❌'), 'line': 9, 'message': 'Message with file attribute: example', 'module': 'ddbug', 'name': '__main__', 'process': (id=79946, name='MainProcess'), 'thread': (id=8214018752, name='MainThread'), 'time': datetime(2024, 4, 10, 15, 39, 12, 448643, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=61200), 'PDT')), 'dd.trace_id': '0', 'dd.span_id': '0', 'dd.env': '', 'dd.service': '', 'dd.version': ''}
Traceback (most recent call last):
  File "venv/lib/python3.12/site-packages/loguru/_handler.py", line 195, in emit
    formatted = self._serialize_record(formatted, record)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/loguru/_handler.py", line 271, in _serialize_record
    "file": {"name": record["file"].name, "path": record["file"].path},
                     ^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'name'
--- End of logging error ---

What is the result that you expected?

{"text": "2024-04-10 15:39:28.534 | ERROR    | __main__:<module>:9 - Message with file attribute: example\n", "record": {"elapsed": {"repr": "0:00:00.002795", "seconds": 0.002795}, "exception": null, "extra": {"file": "example"}, "file": {"name": "ddbug.py", "path": "ddbug.py"}, "function": "<module>", "level": {"icon": "❌", "name": "ERROR", "no": 40}, "line": 9, "message": "Message with file attribute: example", "module": "ddbug", "name": "__main__", "process": {"id": 79970, "name": "MainProcess"}, "thread": {"id": 8214018752, "name": "MainThread"}, "time": {"repr": "2024-04-10 15:39:28.534128-07:00", "timestamp": 1712788768.534128}}}
emmettbutler commented 4 months ago

Thank you for reporting this problem, @ikseek. We'll dig into it and see what we can do.

cc @mabdinur who has worked on the loguru integration before