Skip to content

Commit 3ac4298

Browse files
feat(dbapi2): include object name in procedure call spans
Include the name of the procedure in the name of the span. Examples: CALL procedure() EXECUTE procedure Issue: #1937
1 parent 05332cd commit 3ac4298

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

elasticapm/instrumentation/packages/dbapi2.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,32 @@ def extract_signature(sql):
170170
keyword = "INTO" if sql_type == "INSERT" else "FROM"
171171
sql_type = sql_type + " " + keyword
172172

173-
table_name = look_for_table(sql, keyword)
173+
object_name = look_for_table(sql, keyword)
174174
elif sql_type in ["CREATE", "DROP"]:
175175
# 2nd word is part of SQL type
176176
sql_type = sql_type + sql[first_space:second_space]
177-
table_name = ""
177+
object_name = ""
178178
elif sql_type == "UPDATE":
179-
table_name = look_for_table(sql, "UPDATE")
179+
object_name = look_for_table(sql, "UPDATE")
180180
elif sql_type == "SELECT":
181181
# Name is first table
182182
try:
183183
sql_type = "SELECT FROM"
184-
table_name = look_for_table(sql, "FROM")
184+
object_name = look_for_table(sql, "FROM")
185185
except Exception:
186-
table_name = ""
186+
object_name = ""
187+
elif sql_type in ["EXEC", "EXECUTE"]:
188+
sql_type = "EXECUTE"
189+
end = second_space if second_space > first_space else len(sql)
190+
object_name = sql[first_space + 1 : end]
191+
elif sql_type == "CALL":
192+
first_paren = sql.find("(", first_space)
193+
object_name = sql[first_space + 1 : first_paren] + "()"
187194
else:
188195
# No name
189-
table_name = ""
196+
object_name = ""
190197

191-
signature = " ".join(filter(bool, [sql_type, table_name]))
198+
signature = " ".join(filter(bool, [sql_type, object_name]))
192199
return signature
193200

194201

tests/instrumentation/dbapi2_tests.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,22 @@ def test_extract_signature_bytes():
114114
actual = extract_signature(sql)
115115
expected = "HELLO"
116116
assert actual == expected
117+
118+
119+
@pytest.mark.parametrize(
120+
["sql", "expected"],
121+
[
122+
(
123+
"EXEC AdventureWorks2022.dbo.uspGetEmployeeManagers 50;",
124+
"EXECUTE AdventureWorks2022.dbo.uspGetEmployeeManagers",
125+
),
126+
("EXECUTE sp_who2", "EXECUTE sp_who2"),
127+
("EXEC sp_updatestats @@all_schemas = 'true'", "EXECUTE sp_updatestats"),
128+
("CALL get_car_stats_by_year(2017, @number, @min, @avg, @max);", "CALL get_car_stats_by_year()"),
129+
("CALL get_car_stats_by_year;", "CALL get_car_stats_by_year()"),
130+
("CALL get_car_stats_by_year();", "CALL get_car_stats_by_year()"),
131+
],
132+
)
133+
def test_extract_signature_for_procedure_call(sql, expected):
134+
actual = extract_signature(sql)
135+
assert actual == expected

0 commit comments

Comments
 (0)