Skip to content

Commit

Permalink
Shorten long SQL query to prevent oversized event
Browse files Browse the repository at this point in the history
APM server allows events with a max size of 10000 characters. Shorten
queries traced in asyncpg to max allowed.

See: elastic#842
  • Loading branch information
stj committed Nov 24, 2021
1 parent 8b18ddc commit a640893
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ endif::[]
[[release-notes-6.x]]
=== Python Agent version 6.x
[float]
===== Bug fixes
* asyncpg: Limit SQL queries in context data to 10000 characters {pull}842[#842]
[[release-notes-6.7.0]]
=== 6.7.0 - 2021-11-17
Expand Down
4 changes: 3 additions & 1 deletion elasticapm/instrumentation/packages/asyncio/asyncpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from elasticapm.instrumentation.packages.asyncio.base import AsyncAbstractInstrumentedModule
from elasticapm.instrumentation.packages.dbapi2 import extract_signature
from elasticapm.utils import default_ports
from elasticapm.utils.encoding import shorten


class AsyncPGInstrumentation(AsyncAbstractInstrumentedModule):
Expand Down Expand Up @@ -63,7 +64,8 @@ def get_query(self, method, args):
async def call(self, module, method, wrapped, instance, args, kwargs):
query = self.get_query(method, args)
name = extract_signature(query)
context = {"db": {"type": "sql", "statement": query}}
sql_string = shorten(query, string_length=10000)
context = {"db": {"type": "sql", "statement": sql_string}}
action = "query"
destination_info = {
"address": kwargs.get("host", "localhost"),
Expand Down
14 changes: 14 additions & 0 deletions tests/instrumentation/asyncio_tests/asyncpg_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,17 @@ async def test_fetch_methods(connection, elasticapm_client, method, verify):
assert span["action"] == "query"
assert span["sync"] is False
assert span["name"] == "SELECT FROM test"


async def test_truncate_long_sql(connection, elasticapm_client):
elasticapm_client.begin_transaction("test")
await connection.execute(
f"SELECT id, username FROM test WHERE username = {'x' * 10010};"
)
elasticapm_client.end_transaction("test", "OK")

transactions = elasticapm_client.events[constants.TRANSACTION]
spans = elasticapm_client.spans_for_transaction(transactions[0])

assert len(spans[0]["context"]["db"]["statement"]) == 10000
assert spans[0]["context"]["db"]["statement"].endswith("...")

0 comments on commit a640893

Please sign in to comment.