Skip to content

Commit f752181

Browse files
ellisvalentinerbeniwohli
authored andcommitted
Fix extract_signature when sql is a bytes object (#366)
closes #366 closes #368
1 parent 3d5c78b commit f752181

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

elasticapm/instrumentation/packages/dbapi2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
88
from elasticapm.traces import capture_span
99
from elasticapm.utils import compat, wrapt
10+
from elasticapm.utils.encoding import force_text
1011

1112

1213
class Literal(object):
@@ -59,7 +60,7 @@ def _scan_for_table_with_tokens(tokens, keyword):
5960

6061
def tokenize(sql):
6162
# split on anything that is not a word character, excluding dots
62-
return [t for t in re.split("([^\w.])", sql) if t != ""]
63+
return [t for t in re.split(r"([^\w.])", sql) if t != ""]
6364

6465

6566
def scan(tokens):
@@ -123,6 +124,7 @@ def extract_signature(sql):
123124
:param sql: the SQL statement
124125
:return: a string representing the signature
125126
"""
127+
sql = force_text(sql)
126128
sql = sql.strip()
127129
first_space = sql.find(" ")
128130
if first_space < 0:

tests/instrumentation/dbapi2_tests.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from elasticapm.instrumentation.packages.dbapi2 import Literal, scan, tokenize
1+
from elasticapm.instrumentation.packages.dbapi2 import Literal, extract_signature, scan, tokenize
22

33

44
def test_scan_simple():
@@ -39,3 +39,17 @@ def test_scan_double_quotes_at_end():
3939
actual = [t[1] for t in scan(tokens)]
4040
expected = ["Hello", "Peter", "Pan", "at", "Disney", Literal("'", "World")]
4141
assert actual == expected
42+
43+
44+
def test_extract_signature_string():
45+
sql = "Hello 'Peter Pan' at Disney World"
46+
actual = extract_signature(sql)
47+
expected = "HELLO"
48+
assert actual == expected
49+
50+
51+
def test_extract_signature_bytes():
52+
sql = b"Hello 'Peter Pan' at Disney World"
53+
actual = extract_signature(sql)
54+
expected = "HELLO"
55+
assert actual == expected

tests/instrumentation/mysql_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ def test_select_with_difficult_values():
4545

4646

4747
def test_select_with_difficult_table_name():
48-
sql = "SELECT id FROM `myta\n-æøåble` WHERE id = 2323" ""
48+
sql = u"""SELECT id FROM `myta\n-æøåble` WHERE id = 2323"""
4949
actual = extract_signature(sql)
5050

51-
assert "SELECT FROM myta\n-æøåble" == actual
51+
assert u"SELECT FROM myta\n-æøåble" == actual
5252

5353

5454
def test_select_subselect():

tests/instrumentation/psycopg2_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ def test_select_with_dollar_quotes_custom_token():
121121

122122

123123
def test_select_with_difficult_table_name():
124-
sql_statement = 'SELECT id FROM "myta\n-æøåble" WHERE id = 2323' ""
124+
sql_statement = u"""SELECT id FROM "myta\n-æøåble" WHERE id = 2323"""
125125
actual = extract_signature(sql_statement)
126126

127-
assert "SELECT FROM myta\n-æøåble" == actual
127+
assert u"SELECT FROM myta\n-æøåble" == actual
128128

129129

130130
def test_select_subselect():

0 commit comments

Comments
 (0)