Skip to content

Commit f0e4b8d

Browse files
authored
fix(dbapi): don't assume rowcount exists (backport #5139 to 1.8) (#5201)
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 55525ba commit f0e4b8d

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
@@ -155,7 +155,11 @@ def callproc(self, proc, *args):
155155
return self._trace_method(self.__wrapped__.callproc, self._self_datadog_name, proc, {}, False, proc, *args)
156156

157157
def _set_post_execute_tags(self, span):
158-
row_count = self.__wrapped__.rowcount
158+
# rowcount is in the dbapi specification (https://peps.python.org/pep-0249/#rowcount)
159+
# but some database drivers (cassandra-driver specifically) don't implement it.
160+
row_count = getattr(self.__wrapped__, "rowcount", None)
161+
if row_count is None:
162+
return
159163
span.set_metric("db.rowcount", row_count)
160164
# Necessary for django integration backward compatibility. Django integration used to provide its own
161165
# 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
@@ -159,6 +159,7 @@ resolvers
159159
repo
160160
respawn
161161
rq
162+
rowcount
162163
runnable
163164
runtime
164165
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)