Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling logfire.warn with a Django object raises exception #867

Open
joshbrooks opened this issue Feb 17, 2025 · 1 comment
Open

Calling logfire.warn with a Django object raises exception #867

joshbrooks opened this issue Feb 17, 2025 · 1 comment

Comments

@joshbrooks
Copy link

Description

I'm calling logfire.warn on a Django "User" object with logfire.warn(request.user). What I'd expect is that I'd get the equivalent of calling str(request.user) in the log

In [2]: User.objects.first()
Out[2]: <User: eskola>

In [3]: str(User.objects.first())
Out[3]: 'eskola'

Instead, I get an exception as the serialization of the Django object is not supported

Feb 17 11:01:20 Caught an internal error in Logfire. Your code should still be running fine, just with less telemetry. This is just logging the internal error.
Feb 17 11:01:20 Traceback (most recent call last):
Feb 17 11:01:20   File "/var/www/education/env/lib/python3.12/site-packages/django/views/generic/base.py", line 143, in dispatch
Feb 17 11:01:20     return handler(request, *args, **kwargs)
Feb 17 11:01:20            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 17 11:01:20   File "/var/www/education/source/gather/gather/surveydata/views.py", line 603, in put
Feb 17 11:01:20     couchdoc = self.create_couchdoc(request, rid=rid, uid=uid)
Feb 17 11:01:20                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 17 11:01:20   File "/var/www/education/source/gather/gather/surveydata/views.py", line 564, in create_couchdoc
Feb 17 11:01:20     logfire.warn(request.user)
Feb 17 11:01:20   File "/var/www/education/env/lib/python3.12/site-packages/logfire/_internal/main.py", line 423, in warning
Feb 17 11:01:20     self.log('warn', msg_template, attributes, tags=_tags, exc_info=_exc_info)
Feb 17 11:01:20   File "/var/www/education/env/lib/python3.12/site-packages/logfire/_internal/main.py", line 681, in log
Feb 17 11:01:20     msg, extra_attrs, msg_template = logfire_format_with_magic(
Feb 17 11:01:20                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 17 11:01:20   File "/var/www/education/env/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 330, in logfire_format_with_magic
Feb 17 11:01:20     chunks, extra_attrs, new_template = chunks_formatter.chunks(
Feb 17 11:01:20                                         ^^^^^^^^^^^^^^^^^^^^^^^^
Feb 17 11:01:20   File "/var/www/education/env/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 55, in chunks
Feb 17 11:01:20     chunks, extra_attrs = self._vformat_chunks(
Feb 17 11:01:20                           ^^^^^^^^^^^^^^^^^^^^^
Feb 17 11:01:20   File "/var/www/education/env/lib/python3.12/site-packages/logfire/_internal/formatter.py", line 228, in _vformat_chunks
Feb 17 11:01:20     for literal_text, field_name, format_spec, conversion in self.parse(format_string):
Feb 17 11:01:20                                                              ^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 17 11:01:20   File "/usr/lib/python3.12/string.py", line 288, in parse
Feb 17 11:01:20     return _string.formatter_parser(format_string)
Feb 17 11:01:20            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 17 11:01:20 TypeError: expected str, got SimpleLazyObject
Feb 17 11:01:20 Invalid type SimpleLazyObject for attribute 'logfire.msg_template' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
Feb 17 11:01:20 Invalid type SimpleLazyObject for attribute 'logfire.msg' value. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or a sequence of those types
Feb 17 11:01:20 Exception while exporting Span.
Feb 17 11:01:20 Traceback (most recent call last):
Feb 17 11:01:20   File "/var/www/education/env/lib/python3.12/site-packages/opentelemetry/sdk/trace/export/__init__.py", line 113, in on_end
Feb 17 11:01:20     self.span_exporter.export((span,))
Feb 17 11:01:20   File "/var/www/education/env/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 94, in export
Feb 17 11:01:20     self._log_span(span)
Feb 17 11:01:20   File "/var/www/education/env/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 373, in _log_span
Feb 17 11:01:20     self._print_span(span)
Feb 17 11:01:20   File "/var/www/education/env/lib/python3.12/site-packages/logfire/_internal/exporters/console.py", line 123, in _print_span
Feb 17 11:01:20     print(''.join(text for text, _style in parts), file=self._output)
Feb 17 11:01:20           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Feb 17 11:01:20 TypeError: sequence item 3: expected str instance, SimpleLazyObject found

Python, Logfire & OS Versions, related packages (not required)

logfire="3.5.3"
platform="Linux-6.8.0-52-generic-x86_64-with-glibc2.39"
python="3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0]"
[related_packages]
requests="2.31.0"
requests="2.32.3"
protobuf="4.21.12"
protobuf="5.29.3"
rich="13.7.1"
executing="2.2.0"
opentelemetry-api="1.30.0"
opentelemetry-exporter-otlp-proto-common="1.30.0"
opentelemetry-exporter-otlp-proto-http="1.30.0"
opentelemetry-instrumentation="0.51b0"
opentelemetry-instrumentation-django="0.51b0"
opentelemetry-instrumentation-wsgi="0.51b0"
opentelemetry-proto="1.30.0"
opentelemetry-sdk="1.30.0"
opentelemetry-semantic-conventions="0.51b0"
opentelemetry-util-http="0.51b0"
@alexmojaki
Copy link
Contributor

The object can be serialized, but the first argument is always supposed to be a string with the message/template, e.g. logfire.warn(f'Problem with {request.user}') . Even logfire.warn(123) gives the same error.

We should at least produce a clearer error. Not sure if we should try to make this 'just work'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants