pydantic / logfire

Uncomplicated Observability for Python and beyond! 🪵🔥
https://docs.pydantic.dev/logfire/
MIT License
1.64k stars 45 forks source link

Failed to introspect calling code #276

Closed imnotdev25 closed 3 days ago

imnotdev25 commented 1 week ago

Description

Failed to introspect calling code. Please report this issue to Logfire. Falling back to normal message formatting which may result in loss of information if using an f-string. Set inspect_arguments=False in logfire.configure() to suppress this warning. The problem was: executing failed to find a node.

I tried to set parameter but still same.

Traceback:

Traceback (most recent call last): File "/root/PycharmProjects/backend-user-form-api/.venv/lib/python3.11/site-packages/logfire/_internal/utils.py", line 236, in handle_internal_errors yield File "/usr/lib/python3.11/contextlib.py", line 81, in inner return func(*args, kwds) ^^^^^^^^^^^^^^^^^^^ File "/root/PycharmProjects/backend-user-form-api/.venv/lib/python3.11/site-packages/logfire/_internal/main.py", line 1613, in set_attribute self._json_schema_properties[key] = create_json_schema(value, set()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/PycharmProjects/backend-user-form-api/.venv/lib/python3.11/site-packages/logfire/_internal/json_schema.py", line 118, in create_json_schema return _mapping_schema(obj, seen) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/PycharmProjects/backend-user-form-api/.venv/lib/python3.11/site-packages/logfire/_internal/json_schema.py", line 212, in _mapping_schema _properties({(k if isinstance(k, str) else safe_repr(k)): v for k, v in obj.items()}, seen), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/PycharmProjects/backend-user-form-api/.venv/lib/python3.11/site-packages/logfire/_internal/json_schema.py", line 337, in _properties if (value_schema := create_json_schema(value, seen)) not in PLAIN_SCHEMAS: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/PycharmProjects/backend-user-form-api/.venv/lib/python3.11/site-packages/logfire/_internal/json_schema.py", line 120, in create_json_schema return _sqlalchemy_schema(obj, seen) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/PycharmProjects/backend-user-form-api/.venv/lib/python3.11/site-packages/logfire/_internal/json_schema.py", line 329, in _sqlalchemy_schema state = sa_inspect(obj) ^^^^^^^^^^^^^^^ File "/root/PycharmProjects/backend-user-form-api/.venv/lib/python3.11/site-packages/sqlalchemy/inspection.py", line 147, in inspect raise exc.NoInspectionAvailable( sqlalchemy.exc.NoInspectionAvailable: No inspection system is available for object of type <class 'form_api.schema.user_details.UserFormCreate'>

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

logfire info
logfire="0.43.0"
platform="Linux-5.15.0-112-generic-x86_64-with-glibc2.35"
python="3.11.9 (main, Apr  6 2024, 17:59:24) [GCC 11.4.0]"
[related_packages]
requests="2.32.3"
pydantic="2.7.4"
fastapi="0.110.0"
protobuf="4.25.3"
rich="13.7.1"
executing="2.0.1"
opentelemetry-api="1.25.0"
opentelemetry-exporter-otlp-proto-common="1.25.0"
opentelemetry-exporter-otlp-proto-http="1.25.0"
opentelemetry-instrumentation="0.46b0"
opentelemetry-instrumentation-asgi="0.46b0"
opentelemetry-instrumentation-asyncpg="0.46b0"
opentelemetry-instrumentation-fastapi="0.46b0"
opentelemetry-instrumentation-redis="0.46b0"
opentelemetry-instrumentation-sqlalchemy="0.46b0"
opentelemetry-instrumentation-system-metrics="0.46b0"
opentelemetry-proto="1.25.0"
opentelemetry-sdk="1.25.0"
opentelemetry-semantic-conventions="0.46b0"
opentelemetry-util-http="0.46b0"
alexmojaki commented 1 week ago

Hi @imnotdev25, thanks for the report. The two messages refer to very different and unrelated types of inspection, so this is actually two bugs. Please can you share some of the code and/or other details? What version of sqlalchemy is this? What is the UserFormCreate class? Where was the original warning raised from?

joaoflaviosantos commented 3 days ago

Hi @imnotdev25, thanks for the report. The two messages refer to very different and unrelated types of inspection, so this is actually two bugs. Please can you share some of the code and/or other details? What version of sqlalchemy is this? What is the UserFormCreate class? Where was the original warning raised from?

Hello @alexmojaki, I'm having the same issue. In an SQLModel project based on my boilerplate FastAPI Async SQLModel Boilerplate. Here is how I am implementing it:

backend/src/core/utils/observability.py

# Third-Party Dependencies
import logfire

# Local Dependencies
from src.core.config import settings

# Set up observability on FastAPI application
if settings.LOGFIRE_TOKEN:
    logfire.configure(
        project_name=settings.PROJECT_NAME,
        service_name=f"{settings.PROJECT_NAME} ({str(settings.ENVIRONMENT.value).upper()})",
        token=settings.LOGFIRE_TOKEN,
        pydantic_plugin=logfire.PydanticPlugin(record="all" if settings.PYDANTIC_TRACE else "off"),
        console=False,
    )

backend/src/main.py

# Local Dependencies
from src.core.setup import create_application
from src.core.config import settings
from src.core.api import router

# Set up FastAPI application
app = create_application(router=router, settings=settings)

# Set up observability on FastAPI application
if settings.LOGFIRE_TOKEN and settings.ASGI_TRACE:
    from src.core.utils.observability import logfire

    logfire.instrument_fastapi(app)

However, I receive the following exception in the console during the data deserialization process (one for each record to be deserialized by SQLModel):

INFO:     127.0.0.1:46430 - "GET /api/v1/system/users?page=1&items_per_page=10 HTTP/1.1" 200 OK
2024-06-29 01:23:33,609 - logfire - ERROR - Internal error in Logfire
Traceback (most recent call last):
  File "/home/ubuntu/Example/Server/backend/.venv/lib/python3.11/site-packages/logfire/_internal/utils.py", line 236, in handle_internal_errors
    yield
  File "/usr/lib/python3.11/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/Example/Server/backend/.venv/lib/python3.11/site-packages/logfire/_internal/main.py", line 1613, in set_attribute
    self._json_schema_properties[key] = create_json_schema(value, set())
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/Example/Server/backend/.venv/lib/python3.11/site-packages/logfire/_internal/json_schema.py", line 120, in create_json_schema
    return _sqlalchemy_schema(obj, seen)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/Example/Server/backend/.venv/lib/python3.11/site-packages/logfire/_internal/json_schema.py", line 329, in _sqlalchemy_schema
    state = sa_inspect(obj)
            ^^^^^^^^^^^^^^^
  File "/home/ubuntu/Example/Server/backend/.venv/lib/python3.11/site-packages/sqlalchemy/inspection.py", line 147, in inspect
    raise exc.NoInspectionAvailable(
sqlalchemy.exc.NoInspectionAvailable: No inspection system is available for object of type <class 'src.apps.shared.system.users.schemas.UserRead'>

The strangest thing is that the Pydantic tracing is being exported correctly, appearing in the Web UI as 'Pydantic UserRead validate_python succeeded'.

Below are the current dependencies:

logfire = {extras = ["fastapi", "sqlalchemy", "system-metrics"], version = "^0.44.0"}

I am using SQLModel with Logfire because on the page 'https://docs.pydantic.dev/logfire/integrations/sqlalchemy/#usage' you included a note that this is possible: 'If you use SQLModel, you can use the same SQLAlchemyInstrumentor to instrument it.'

Thank you very much, and this tool is becoming amazing!! 🪵🔥🚀"

alexmojaki commented 3 days ago

Released a fix, please pip install -U logfire and try again!

For the Failed to introspect calling code warning, if you see it again please open a new issue with more details.

joaoflaviosantos commented 3 days ago

Released a fix, please pip install -U logfire and try again!

For the Failed to introspect calling code warning, if you see it again please open a new issue with more details.

Hello again @alexmojaki!

I tested it here, and now it works correctly.

I am impressed with the speed at which this bug/issue was resolved. I am confident that Pydantic Logfire will soon become a reference tool for Python in terms of observability.

Thank you for the fix! I wish you all success!