Skip to content

Commit 4bd91b0

Browse files
feat(dbapi2): use span action exec for procedure calls
Set span action to exec instead of query for procedure calls not using callproc(). Issue: #1937
1 parent 62a4996 commit 4bd91b0

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

elasticapm/instrumentation/packages/dbapi2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ def extract_signature(sql):
203203

204204
QUERY_ACTION = "query"
205205
EXEC_ACTION = "exec"
206+
PROCEDURE_STATEMENTS = ["EXEC", "EXECUTE", "CALL"]
207+
208+
209+
def extract_action_from_signature(signature, default):
210+
if signature.split(" ")[0] in PROCEDURE_STATEMENTS:
211+
return EXEC_ACTION
212+
return default
206213

207214

208215
class CursorProxy(wrapt.ObjectProxy):
@@ -235,6 +242,7 @@ def _trace_sql(self, method, sql, params, action=QUERY_ACTION):
235242
signature = sql_string + "()"
236243
else:
237244
signature = self.extract_signature(sql_string)
245+
action = extract_action_from_signature(signature, action)
238246

239247
# Truncate sql_string to 10000 characters to prevent large queries from
240248
# causing an error to APM server.

tests/instrumentation/dbapi2_tests.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@
2929
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
import pytest
3131

32-
from elasticapm.instrumentation.packages.dbapi2 import Literal, extract_signature, scan, tokenize
32+
from elasticapm.instrumentation.packages.dbapi2 import (
33+
Literal,
34+
extract_action_from_signature,
35+
extract_signature,
36+
scan,
37+
tokenize,
38+
)
3339

3440

3541
def test_scan_simple():
@@ -134,3 +140,17 @@ def test_extract_signature_bytes():
134140
def test_extract_signature_for_procedure_call(sql, expected):
135141
actual = extract_signature(sql)
136142
assert actual == expected
143+
144+
145+
@pytest.mark.parametrize(
146+
["sql", "expected"],
147+
[
148+
("SELECT FROM table", "query"),
149+
("EXEC sp_who", "exec"),
150+
("EXECUTE sp_updatestats", "exec"),
151+
("CALL me_maybe", "exec"),
152+
],
153+
)
154+
def test_extract_action_from_signature(sql, expected):
155+
actual = extract_action_from_signature(sql, "query")
156+
assert actual == expected

0 commit comments

Comments
 (0)