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

Catch more errors when checking for sqlalchemy objects #854

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions logfire/_internal/json_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,17 @@ def logfire_json_dumps(obj: Any) -> str:


def is_sqlalchemy(obj: Any) -> bool:
if not hasattr(obj, '__mapper__'):
# A SQLModel without `table=True` will pass `isinstance(obj.__class__, DeclarativeMeta)` (I don't know how)
# but will fail when retrieving data, specifically when calling `sqlalchemy.inspect`
# or when getting the `__mapper__` attribute.
return False

try:
if not hasattr(obj, '__mapper__'):
# A SQLModel without `table=True` will pass `isinstance(obj.__class__, DeclarativeMeta)` (I don't know how)
# but will fail when retrieving data, specifically when calling `sqlalchemy.inspect`
# or when getting the `__mapper__` attribute.
return False

from sqlalchemy.orm import DeclarativeBase, DeclarativeMeta

return isinstance(obj, DeclarativeBase) or isinstance(obj.__class__, DeclarativeMeta)
except ImportError: # pragma: no cover
except Exception:
return False


Expand Down
35 changes: 35 additions & 0 deletions tests/test_json_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,3 +1315,38 @@ def test_numpy_array_truncation(exporter: TestExporter):
}
]
)


def test_bad_getattr(exporter: TestExporter, caplog: pytest.LogCaptureFixture):
class A:
def __getattr__(self, item: str):
raise RuntimeError

def __repr__(self):
return 'A()'

logfire.info('hello', a=A())

assert not caplog.messages
assert exporter.exported_spans_as_dict() == snapshot(
[
{
'name': 'hello',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 1000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 9,
'logfire.msg_template': 'hello',
'logfire.msg': 'hello',
'code.filepath': 'test_json_args.py',
'code.function': 'test_bad_getattr',
'code.lineno': 123,
'a': '"A()"',
'logfire.json_schema': '{"type":"object","properties":{"a":{"type":"object","x-python-datatype":"unknown"}}}',
},
}
]
)