Skip to content

Commit e6029d5

Browse files
authored
fix(dbapi): don't assume rowcount exists (backport #5139 to 1.9) (#5202)
Some database drivers (namely cassandra-driver) do not implement rowcount on cursor objects and so the dbapi integration fails. I have omitted a test as I believe that the code and inlined comment is sufficient for preventing regressions. Another factor was that it is not trivial to add a test case repro-ing the issue with django + cassandra-driver. (Aside, perhaps having a special `regression` test suite/environment which can be easily configured to test different permutations of libary and version would be a good addition for cases like this)
1 parent 90c89a5 commit e6029d5

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

ddtrace/contrib/dbapi/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ def callproc(self, proc, *args):
149149
return self._trace_method(self.__wrapped__.callproc, self._self_datadog_name, proc, {}, None, proc, *args)
150150

151151
def _set_post_execute_tags(self, span):
152-
row_count = self.__wrapped__.rowcount
152+
# rowcount is in the dbapi specification (https://peps.python.org/pep-0249/#rowcount)
153+
# but some database drivers (cassandra-driver specifically) don't implement it.
154+
row_count = getattr(self.__wrapped__, "rowcount", None)
155+
if row_count is None:
156+
return
153157
span.set_metric(db.ROWCOUNT, row_count)
154158
# Necessary for django integration backward compatibility. Django integration used to provide its own
155159
# implementation of the TracedCursor, which used to store the row count into a tag instead of

docs/spelling_wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ resolvers
160160
repo
161161
respawn
162162
rq
163+
rowcount
163164
runnable
164165
runtime
165166
runtimes
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
dbapi: The dbapi integration no longer assumes that a cursor object will
5+
have a rowcount but not all database drivers implement rowcount.

0 commit comments

Comments
 (0)