dd-trace-py icon indicating copy to clipboard operation
dd-trace-py copied to clipboard

ddtrace.patch breaks serializing loguru logger

Open ikseek opened this issue 1 year ago • 1 comments

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}}}

ikseek avatar Apr 10 '24 22:04 ikseek

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

emmettbutler avatar Apr 11 '24 13:04 emmettbutler

This issue has been automatically closed after a period of inactivity. If it's a feature request, it has been added to the maintainers' internal backlog and will be included in an upcoming round of feature prioritization. Please comment or reopen if you think this issue was closed in error.

github-actions[bot] avatar Sep 09 '24 00:09 github-actions[bot]

Any news on fix for this?

sky-code avatar Sep 09 '24 17:09 sky-code