From 8040c8e7bd5e78e76be0818b64155f15ea887caf Mon Sep 17 00:00:00 2001 From: Ellis Valentiner Date: Thu, 20 Dec 2018 18:43:48 -0500 Subject: [PATCH 1/4] Fix extract_signature when sql is a bytes object --- elasticapm/instrumentation/packages/dbapi2.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/elasticapm/instrumentation/packages/dbapi2.py b/elasticapm/instrumentation/packages/dbapi2.py index dd75d4e11..0de7aded2 100644 --- a/elasticapm/instrumentation/packages/dbapi2.py +++ b/elasticapm/instrumentation/packages/dbapi2.py @@ -123,6 +123,8 @@ def extract_signature(sql): :param sql: the SQL statement :return: a string representing the signature """ + if isinstance(sql, bytes): + sql = sql.decode() sql = sql.strip() first_space = sql.find(" ") if first_space < 0: From bed5f57a9eaf92fd3fdb53e0354a0a03f2f55471 Mon Sep 17 00:00:00 2001 From: Ellis Valentiner Date: Thu, 20 Dec 2018 18:54:28 -0500 Subject: [PATCH 2/4] Add tests --- tests/instrumentation/dbapi2_tests.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/instrumentation/dbapi2_tests.py b/tests/instrumentation/dbapi2_tests.py index 54d0b10c1..7eb69fd91 100644 --- a/tests/instrumentation/dbapi2_tests.py +++ b/tests/instrumentation/dbapi2_tests.py @@ -1,4 +1,4 @@ -from elasticapm.instrumentation.packages.dbapi2 import Literal, scan, tokenize +from elasticapm.instrumentation.packages.dbapi2 import Literal, scan, tokenize, extract_signature def test_scan_simple(): @@ -39,3 +39,17 @@ def test_scan_double_quotes_at_end(): actual = [t[1] for t in scan(tokens)] expected = ["Hello", "Peter", "Pan", "at", "Disney", Literal("'", "World")] assert actual == expected + + +def test_extract_signature_string(): + sql = "Hello 'Peter Pan' at Disney World" + actual = extract_signature(sql) + expected = "HELLO" + assert actual == expected + + +def test_extract_signature_bytes(): + sql = b"Hello 'Peter Pan' at Disney World" + actual = extract_signature(sql) + expected = "HELLO" + assert actual == expected From c6c617334a83deb5dfc01a2c6958bd60627d3e41 Mon Sep 17 00:00:00 2001 From: Ellis Valentiner Date: Fri, 21 Dec 2018 12:29:07 -0500 Subject: [PATCH 3/4] Change implementation to use elasticapm.utils.encoding.force_text --- elasticapm/instrumentation/packages/dbapi2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/elasticapm/instrumentation/packages/dbapi2.py b/elasticapm/instrumentation/packages/dbapi2.py index 0de7aded2..e1e34d7cb 100644 --- a/elasticapm/instrumentation/packages/dbapi2.py +++ b/elasticapm/instrumentation/packages/dbapi2.py @@ -7,6 +7,7 @@ from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule from elasticapm.traces import capture_span from elasticapm.utils import compat, wrapt +from elasticapm.utils.encoding import force_text class Literal(object): @@ -123,8 +124,7 @@ def extract_signature(sql): :param sql: the SQL statement :return: a string representing the signature """ - if isinstance(sql, bytes): - sql = sql.decode() + sql = force_text(sql) sql = sql.strip() first_space = sql.find(" ") if first_space < 0: From 06b297975d1f5ee70887ec2642d467e5cbc2befc Mon Sep 17 00:00:00 2001 From: Benjamin Wohlwend Date: Mon, 24 Dec 2018 12:32:29 +0100 Subject: [PATCH 4/4] fix test to expect unicode --- elasticapm/instrumentation/packages/dbapi2.py | 2 +- tests/instrumentation/dbapi2_tests.py | 2 +- tests/instrumentation/mysql_tests.py | 4 ++-- tests/instrumentation/psycopg2_tests.py | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/elasticapm/instrumentation/packages/dbapi2.py b/elasticapm/instrumentation/packages/dbapi2.py index e1e34d7cb..059a41380 100644 --- a/elasticapm/instrumentation/packages/dbapi2.py +++ b/elasticapm/instrumentation/packages/dbapi2.py @@ -60,7 +60,7 @@ def _scan_for_table_with_tokens(tokens, keyword): def tokenize(sql): # split on anything that is not a word character, excluding dots - return [t for t in re.split("([^\w.])", sql) if t != ""] + return [t for t in re.split(r"([^\w.])", sql) if t != ""] def scan(tokens): diff --git a/tests/instrumentation/dbapi2_tests.py b/tests/instrumentation/dbapi2_tests.py index 7eb69fd91..3814d612a 100644 --- a/tests/instrumentation/dbapi2_tests.py +++ b/tests/instrumentation/dbapi2_tests.py @@ -1,4 +1,4 @@ -from elasticapm.instrumentation.packages.dbapi2 import Literal, scan, tokenize, extract_signature +from elasticapm.instrumentation.packages.dbapi2 import Literal, extract_signature, scan, tokenize def test_scan_simple(): diff --git a/tests/instrumentation/mysql_tests.py b/tests/instrumentation/mysql_tests.py index 945b2b321..a5a70d800 100644 --- a/tests/instrumentation/mysql_tests.py +++ b/tests/instrumentation/mysql_tests.py @@ -45,10 +45,10 @@ def test_select_with_difficult_values(): def test_select_with_difficult_table_name(): - sql = "SELECT id FROM `myta\n-æøåble` WHERE id = 2323" "" + sql = u"""SELECT id FROM `myta\n-æøåble` WHERE id = 2323""" actual = extract_signature(sql) - assert "SELECT FROM myta\n-æøåble" == actual + assert u"SELECT FROM myta\n-æøåble" == actual def test_select_subselect(): diff --git a/tests/instrumentation/psycopg2_tests.py b/tests/instrumentation/psycopg2_tests.py index a9270a7c4..ab7496e5e 100644 --- a/tests/instrumentation/psycopg2_tests.py +++ b/tests/instrumentation/psycopg2_tests.py @@ -121,10 +121,10 @@ def test_select_with_dollar_quotes_custom_token(): def test_select_with_difficult_table_name(): - sql_statement = 'SELECT id FROM "myta\n-æøåble" WHERE id = 2323' "" + sql_statement = u"""SELECT id FROM "myta\n-æøåble" WHERE id = 2323""" actual = extract_signature(sql_statement) - assert "SELECT FROM myta\n-æøåble" == actual + assert u"SELECT FROM myta\n-æøåble" == actual def test_select_subselect():