Skip to content

Commit 6a4656e

Browse files
stjbeniwohli
andauthored
Shorten long SQL query to prevent oversized event (elastic#1416)
* Shorten long SQL query to prevent oversized event APM server allows events with a max size of 10000 characters. Shorten queries traced in asyncpg to max allowed. See: elastic#842 * Fix copied SQL * fix test * instrument for test * move changelog entry to "Unreleased" and fix PR number Co-authored-by: Benjamin Wohlwend <beni@elastic.co>
1 parent 8b18ddc commit 6a4656e

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGELOG.asciidoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ endif::[]
2929
//[float]
3030
//===== Bug fixes
3131
32+
=== Unreleased
33+
34+
//[float]
35+
//===== Features
36+
37+
38+
[float]
39+
===== Bug fixes
40+
41+
* asyncpg: Limit SQL queries in context data to 10000 characters {pull}1416[#1416]
42+
43+
3244
[[release-notes-6.x]]
3345
=== Python Agent version 6.x
3446

elasticapm/instrumentation/packages/asyncio/asyncpg.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from elasticapm.instrumentation.packages.asyncio.base import AsyncAbstractInstrumentedModule
3333
from elasticapm.instrumentation.packages.dbapi2 import extract_signature
3434
from elasticapm.utils import default_ports
35+
from elasticapm.utils.encoding import shorten
3536

3637

3738
class AsyncPGInstrumentation(AsyncAbstractInstrumentedModule):
@@ -63,7 +64,8 @@ def get_query(self, method, args):
6364
async def call(self, module, method, wrapped, instance, args, kwargs):
6465
query = self.get_query(method, args)
6566
name = extract_signature(query)
66-
context = {"db": {"type": "sql", "statement": query}}
67+
sql_string = shorten(query, string_length=10000)
68+
context = {"db": {"type": "sql", "statement": sql_string}}
6769
action = "query"
6870
destination_info = {
6971
"address": kwargs.get("host", "localhost"),

tests/instrumentation/asyncio_tests/asyncpg_tests.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,17 @@ async def test_fetch_methods(connection, elasticapm_client, method, verify):
139139
assert span["action"] == "query"
140140
assert span["sync"] is False
141141
assert span["name"] == "SELECT FROM test"
142+
143+
144+
@pytest.mark.usefixtures("instrument")
145+
async def test_truncate_long_sql(connection, elasticapm_client):
146+
elasticapm_client.begin_transaction("test")
147+
await connection.execute(f"SELECT id, name FROM test WHERE name = '{'x' * 10010}';")
148+
elasticapm_client.end_transaction("test", "OK")
149+
150+
transactions = elasticapm_client.events[constants.TRANSACTION]
151+
spans = elasticapm_client.spans_for_transaction(transactions[0])
152+
153+
statement = spans[0]["context"]["db"]["statement"]
154+
assert len(statement) == 10000
155+
assert statement.endswith("...")

0 commit comments

Comments
 (0)