MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.99k stars 404 forks source link

IndexError when use `elastic-apm` #1100

Open aryadovoy opened 1 year ago

aryadovoy commented 1 year ago

It was OK on 0.28.0, but now I have this error. Think, it's regarding to #1043. There is no args, so it cannot get query.

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.venv/lib/python3.11/site-packages/httpx/_client.py:1848: in post
    return await self.request(
.venv/lib/python3.11/site-packages/httpx/_client.py:1530: in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
.venv/lib/python3.11/site-packages/httpx/_client.py:1617: in send
    response = await self._send_handling_auth(
.venv/lib/python3.11/site-packages/httpx/_client.py:1645: in _send_handling_auth
    response = await self._send_handling_redirects(
.venv/lib/python3.11/site-packages/httpx/_client.py:1682: in _send_handling_redirects
    response = await self._send_single_request(request)
.venv/lib/python3.11/site-packages/httpx/_client.py:1719: in _send_single_request
    response = await transport.handle_async_request(request)
.venv/lib/python3.11/site-packages/httpx/_transports/asgi.py:162: in handle_async_request
    await self.app(scope, receive, send)
.venv/lib/python3.11/site-packages/fastapi/applications.py:292: in __call__
    await super().__call__(scope, receive, send)
.venv/lib/python3.11/site-packages/starlette/applications.py:122: in __call__
    await self.middleware_stack(scope, receive, send)
.venv/lib/python3.11/site-packages/elasticapm/instrumentation/packages/asyncio/starlette.py:48: in call
    return await wrapped(*args, **kwargs)
.venv/lib/python3.11/site-packages/starlette/middleware/errors.py:184: in __call__
    raise exc
.venv/lib/python3.11/site-packages/starlette/middleware/errors.py:162: in __call__
    await self.app(scope, receive, _send)
.venv/lib/python3.11/site-packages/elasticapm/contrib/starlette/__init__.py:193: in __call__
    await self.app(scope, _request_receive or receive, wrapped_send)
.venv/lib/python3.11/site-packages/starlette/middleware/sessions.py:86: in __call__
    await self.app(scope, receive, send_wrapper)
.venv/lib/python3.11/site-packages/starlette/middleware/cors.py:83: in __call__
    await self.app(scope, receive, send)
.venv/lib/python3.11/site-packages/starlette/middleware/exceptions.py:79: in __call__
    raise exc
.venv/lib/python3.11/site-packages/starlette/middleware/exceptions.py:68: in __call__
    await self.app(scope, receive, sender)
.venv/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py:20: in __call__
    raise e
.venv/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py:17: in __call__
    await self.app(scope, receive, send)
.venv/lib/python3.11/site-packages/starlette/routing.py:718: in __call__
    await route.handle(scope, receive, send)
.venv/lib/python3.11/site-packages/starlette/routing.py:276: in handle
    await self.app(scope, receive, send)
.venv/lib/python3.11/site-packages/starlette/routing.py:66: in app
    response = await func(request)
.venv/lib/python3.11/site-packages/fastapi/routing.py:273: in app
    raw_response = await run_endpoint_function(
.venv/lib/python3.11/site-packages/fastapi/routing.py:190: in run_endpoint_function
    return await dependant.call(**values)
src/app/presentation/api/consumers.py:39: in send_otp
    consumer, is_new = await ConsumerService.send_otp(uow=uow, data=data)
src/app/core/services/consumers.py:29: in send_otp
    consumer := await uow.consumers.get_by_phone(
src/app/infrastructure/db/dao/consumers.py:24: in get_by_phone
    query = await self.session.execute(
.venv/lib/python3.11/site-packages/sqlalchemy/ext/asyncio/session.py:454: in execute
    result = await greenlet_spawn(
.venv/lib/python3.11/site-packages/sqlalchemy/util/_concurrency_py3k.py:190: in greenlet_spawn
    result = context.throw(*sys.exc_info())
.venv/lib/python3.11/site-packages/sqlalchemy/orm/session.py:2262: in execute
    return self._execute_internal(
.venv/lib/python3.11/site-packages/sqlalchemy/orm/session.py:2134: in _execute_internal
    conn = self._connection_for_bind(bind)
.venv/lib/python3.11/site-packages/sqlalchemy/orm/session.py:2001: in _connection_for_bind
    return trans._connection_for_bind(engine, execution_options)
<string>:2: in _connection_for_bind
    ???
.venv/lib/python3.11/site-packages/sqlalchemy/orm/state_changes.py:139: in _go
    ret_value = fn(self, *arg, **kw)
.venv/lib/python3.11/site-packages/sqlalchemy/orm/session.py:1126: in _connection_for_bind
    conn = bind.connect()
.venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py:3264: in connect
    return self._connection_cls(self)
.venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py:145: in __init__
    self._dbapi_connection = engine.raw_connection()
.venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py:3288: in raw_connection
    return self.pool.connect()
.venv/lib/python3.11/site-packages/sqlalchemy/pool/base.py:452: in connect
    return _ConnectionFairy._checkout(self)
.venv/lib/python3.11/site-packages/sqlalchemy/pool/base.py:1267: in _checkout
    fairy = _ConnectionRecord.checkout(pool)
.venv/lib/python3.11/site-packages/sqlalchemy/pool/base.py:716: in checkout
    rec = pool._do_get()
.venv/lib/python3.11/site-packages/sqlalchemy/pool/impl.py:169: in _do_get
    with util.safe_reraise():
.venv/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py:146: in __exit__
    raise exc_value.with_traceback(exc_tb)
.venv/lib/python3.11/site-packages/sqlalchemy/pool/impl.py:167: in _do_get
    return self._create_connection()
.venv/lib/python3.11/site-packages/sqlalchemy/pool/base.py:393: in _create_connection
    return _ConnectionRecord(self)
.venv/lib/python3.11/site-packages/sqlalchemy/pool/base.py:678: in __init__
    self.__connect()
.venv/lib/python3.11/site-packages/sqlalchemy/pool/base.py:916: in __connect
    )._exec_w_sync_on_first_run(self.dbapi_connection, self)
.venv/lib/python3.11/site-packages/sqlalchemy/event/attr.py:473: in _exec_w_sync_on_first_run
    self(*args, **kw)
.venv/lib/python3.11/site-packages/sqlalchemy/event/attr.py:487: in __call__
    fn(*args, **kw)
.venv/lib/python3.11/site-packages/sqlalchemy/engine/create.py:704: in on_connect
    do_on_connect(dbapi_connection)
.venv/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:1240: in connect
    conn.await_(self.setup_asyncpg_json_codec(conn))
.venv/lib/python3.11/site-packages/sqlalchemy/util/_concurrency_py3k.py:125: in await_only
    return current.driver.switch(awaitable)  # type: ignore[no-any-return]
.venv/lib/python3.11/site-packages/sqlalchemy/util/_concurrency_py3k.py:185: in greenlet_spawn
    value = await result
.venv/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py:1166: in setup_asyncpg_json_codec
    await asyncpg_connection.set_type_codec(
.venv/lib/python3.11/site-packages/asyncpg/connection.py:1339: in set_type_codec
    typeinfo = await self._introspect_type(typename, schema)
.venv/lib/python3.11/site-packages/asyncpg/connection.py:543: in _introspect_type
    rows = await self._execute(
.venv/lib/python3.11/site-packages/asyncpg/connection.py:1794: in _execute
    result, _ = await self.__execute(
.venv/lib/python3.11/site-packages/asyncpg/connection.py:1892: in __execute
    result, stmt = await self._do_execute(
.venv/lib/python3.11/site-packages/asyncpg/connection.py:1945: in _do_execute
    result = await executor(stmt, None)
.venv/lib/python3.11/site-packages/elasticapm/instrumentation/packages/asyncio/asyncpg.py:65: in call
    query = self.get_query(method, args)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <elasticapm.instrumentation.packages.asyncio.asyncpg.AsyncPGInstrumentation object at 0x7fd19d5e4b90>, method = 'Protocol.bind_execute', args = ()

    def get_query(self, method, args):
        if method in ["Protocol.query", "Protocol.copy_in", "Protocol.copy_out"]:
            return args[0]
        else:
>           return args[0].query
E           IndexError: tuple index out of range

.venv/lib/python3.11/site-packages/elasticapm/instrumentation/packages/asyncio/asyncpg.py:62: IndexError
fritzdj commented 11 months ago

Related issue: https://github.com/elastic/apm-agent-python/issues/1933. It looks like there is an APM agent fix coming for this.