Skip to content

Fix asyncpg tests to make it compatible with asyncpg>=0.29 #1935

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

Merged
merged 4 commits into from
Nov 27, 2023
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
1 change: 1 addition & 0 deletions .ci/.matrix_framework_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ FRAMEWORK:
- aiohttp-3.0
- aiohttp-newest
- aiopg-newest
- asyncpg-0.28
- asyncpg-newest
- tornado-newest
- starlette-0.13
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:
language_version: python3
exclude: "(tests/utils/stacks/linenos.py|tests/utils/stacks/linenos2.py|tests/contrib/grpc/grpc_app/.*pb2.*.py)"
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
rev: 6.1.0
hooks:
- id: flake8
exclude: "(tests/utils/stacks/linenos.py|tests/utils/stacks/linenos2.py|tests/contrib/grpc/grpc_app/.*pb2.*.py)"
Expand Down
4 changes: 3 additions & 1 deletion elasticapm/contrib/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ async def __call__(self, scope: "Scope", receive: "ASGIReceiveCallable", send: "
body = str(body_raw, errors="ignore")

# Dispatch to the ASGI callable
async def wrapped_receive():
async def new_wrapped_receive():
if messages:
return messages.pop(0)

# Once that's done we can just await any other messages.
return await receive()

wrapped_receive = new_wrapped_receive

await set_context(lambda: self.get_data_from_request(scope, constants.TRANSACTION, body), "request")

try:
Expand Down
9 changes: 6 additions & 3 deletions elasticapm/instrumentation/packages/asyncio/asyncpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ class AsyncPGInstrumentation(AsyncAbstractInstrumentedModule):
("asyncpg.protocol.protocol", "Protocol.copy_out"),
]

def get_query(self, method, args):
def get_query(self, method, args, kwargs=None):
if method in ["Protocol.query", "Protocol.copy_in", "Protocol.copy_out"]:
return args[0]
else:
elif args:
return args[0].query
else:
# asyncpg>=0.29 pass data as kwargs
return kwargs["state"].query

async def call(self, module, method, wrapped, instance, args, kwargs):
query = self.get_query(method, args)
query = self.get_query(method, args, kwargs)
name = extract_signature(query)
sql_string = shorten(query, string_length=10000)
context = {"db": {"type": "sql", "statement": sql_string}}
Expand Down
2 changes: 2 additions & 0 deletions tests/requirements/reqs-asyncpg-0.28.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
asyncpg==0.28
-r reqs-base.txt