Skip to content

Commit c3a8376

Browse files
authored
Fix sqlite connection executemany instrumentation. (#180)
1 parent d9c23f3 commit c3a8376

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

newrelic/hooks/database_dbapi2.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def __init__(self, cursor, dbapi2_module, connect_params, cursor_params):
3030
self._nr_cursor_params = cursor_params
3131

3232
def execute(self, sql, parameters=DEFAULT, *args, **kwargs):
33-
transaction = current_transaction()
3433
if parameters is not DEFAULT:
3534
with DatabaseTrace(sql, self._nr_dbapi2_module,
3635
self._nr_connect_params, self._nr_cursor_params,
@@ -44,8 +43,8 @@ def execute(self, sql, parameters=DEFAULT, *args, **kwargs):
4443
return self.__wrapped__.execute(sql, **kwargs)
4544

4645
def executemany(self, sql, seq_of_parameters):
47-
transaction = current_transaction()
4846
try:
47+
seq_of_parameters = list(seq_of_parameters)
4948
parameters = seq_of_parameters[0]
5049
except (TypeError, IndexError):
5150
parameters = DEFAULT
@@ -60,7 +59,6 @@ def executemany(self, sql, seq_of_parameters):
6059
return self.__wrapped__.executemany(sql, seq_of_parameters)
6160

6261
def callproc(self, procname, parameters=DEFAULT):
63-
transaction = current_transaction()
6462
with DatabaseTrace('CALL %s' % procname,
6563
self._nr_dbapi2_module, self._nr_connect_params):
6664
if parameters is not DEFAULT:
@@ -83,13 +81,11 @@ def cursor(self, *args, **kwargs):
8381
self._nr_connect_params, (args, kwargs))
8482

8583
def commit(self):
86-
transaction = current_transaction()
8784
with DatabaseTrace('COMMIT', self._nr_dbapi2_module,
8885
self._nr_connect_params):
8986
return self.__wrapped__.commit()
9087

9188
def rollback(self):
92-
transaction = current_transaction()
9389
with DatabaseTrace('ROLLBACK', self._nr_dbapi2_module,
9490
self._nr_connect_params):
9591
return self.__wrapped__.rollback()
@@ -103,10 +99,6 @@ def __init__(self, connect, dbapi2_module):
10399
self._nr_dbapi2_module = dbapi2_module
104100

105101
def __call__(self, *args, **kwargs):
106-
transaction = current_transaction()
107-
108-
settings = global_settings()
109-
110102
rollup = []
111103
rollup.append('Datastore/all')
112104
rollup.append('Datastore/%s/all' %

newrelic/hooks/database_sqlite.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,20 @@ def execute(self, sql, parameters=DEFAULT):
7373
return self.__wrapped__.execute(sql)
7474

7575
def executemany(self, sql, seq_of_parameters):
76-
with DatabaseTrace(sql, self._nr_dbapi2_module,
77-
self._nr_connect_params, None, list(seq_of_parameters)[0]):
78-
return self.__wrapped__.executemany(sql, seq_of_parameters)
76+
try:
77+
seq_of_parameters = list(seq_of_parameters)
78+
parameters = seq_of_parameters[0]
79+
except (TypeError, IndexError):
80+
parameters = DEFAULT
81+
if parameters is not DEFAULT:
82+
with DatabaseTrace(sql, self._nr_dbapi2_module,
83+
self._nr_connect_params, None,
84+
parameters):
85+
return self.__wrapped__.executemany(sql, seq_of_parameters)
86+
else:
87+
with DatabaseTrace(sql, self._nr_dbapi2_module,
88+
self._nr_connect_params, None):
89+
return self.__wrapped__.executemany(sql, seq_of_parameters)
7990

8091
def executescript(self, sql_script):
8192
with DatabaseTrace(sql_script, self._nr_dbapi2_module,

tests/datastore_sqlite/test_database.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
_test_execute_via_cursor_scoped_metrics = [
3030
('Function/_sqlite3:connect', 1),
3131
('Datastore/statement/SQLite/datastore_sqlite/select', 1),
32-
('Datastore/statement/SQLite/datastore_sqlite/insert', 1),
32+
('Datastore/statement/SQLite/datastore_sqlite/insert', 2),
3333
('Datastore/statement/SQLite/datastore_sqlite/update', 1),
3434
('Datastore/statement/SQLite/datastore_sqlite/delete', 1),
3535
('Datastore/operation/SQLite/drop', 1),
@@ -38,14 +38,14 @@
3838
('Datastore/operation/SQLite/rollback', 1)]
3939

4040
_test_execute_via_cursor_rollup_metrics = [
41-
('Datastore/all', 11),
42-
('Datastore/allOther', 11),
43-
('Datastore/SQLite/all', 11),
44-
('Datastore/SQLite/allOther', 11),
41+
('Datastore/all', 12),
42+
('Datastore/allOther', 12),
43+
('Datastore/SQLite/all', 12),
44+
('Datastore/SQLite/allOther', 12),
4545
('Datastore/operation/SQLite/select', 1),
4646
('Datastore/statement/SQLite/datastore_sqlite/select', 1),
47-
('Datastore/operation/SQLite/insert', 1),
48-
('Datastore/statement/SQLite/datastore_sqlite/insert', 1),
47+
('Datastore/operation/SQLite/insert', 2),
48+
('Datastore/statement/SQLite/datastore_sqlite/insert', 2),
4949
('Datastore/operation/SQLite/update', 1),
5050
('Datastore/statement/SQLite/datastore_sqlite/update', 1),
5151
('Datastore/operation/SQLite/delete', 1),
@@ -83,9 +83,14 @@ def test_execute_via_cursor():
8383
cursor.executemany("""insert into datastore_sqlite values (?, ?, ?)""",
8484
[(1, 1.0, '1.0'), (2, 2.2, '2.2'), (3, 3.3, '3.3')])
8585

86+
test_data = [(4, 4.0, '4.0'), (5, 5.5, '5.5'), (6, 6.6, '6.6')]
87+
88+
cursor.executemany("""insert into datastore_sqlite values (?, ?, ?)""",
89+
((value) for value in test_data))
90+
8691
cursor.execute("""select * from datastore_sqlite""")
8792

88-
for row in cursor: pass
93+
assert len(list(cursor)) == 6
8994

9095
cursor.execute("""update datastore_sqlite set a=?, b=?, """
9196
"""c=? where a=?""", (4, 4.0, '4.0', 1))
@@ -100,7 +105,7 @@ def test_execute_via_cursor():
100105
_test_execute_via_connection_scoped_metrics = [
101106
('Function/_sqlite3:connect', 1),
102107
('Datastore/statement/SQLite/datastore_sqlite/select', 1),
103-
('Datastore/statement/SQLite/datastore_sqlite/insert', 1),
108+
('Datastore/statement/SQLite/datastore_sqlite/insert', 2),
104109
('Datastore/statement/SQLite/datastore_sqlite/update', 1),
105110
('Datastore/statement/SQLite/datastore_sqlite/delete', 1),
106111
('Datastore/operation/SQLite/drop', 1),
@@ -109,14 +114,14 @@ def test_execute_via_cursor():
109114
('Datastore/operation/SQLite/rollback', 1)]
110115

111116
_test_execute_via_connection_rollup_metrics = [
112-
('Datastore/all', 11),
113-
('Datastore/allOther', 11),
114-
('Datastore/SQLite/all', 11),
115-
('Datastore/SQLite/allOther', 11),
117+
('Datastore/all', 12),
118+
('Datastore/allOther', 12),
119+
('Datastore/SQLite/all', 12),
120+
('Datastore/SQLite/allOther', 12),
116121
('Datastore/operation/SQLite/select', 1),
117122
('Datastore/statement/SQLite/datastore_sqlite/select', 1),
118-
('Datastore/operation/SQLite/insert', 1),
119-
('Datastore/statement/SQLite/datastore_sqlite/insert', 1),
123+
('Datastore/operation/SQLite/insert', 2),
124+
('Datastore/statement/SQLite/datastore_sqlite/insert', 2),
120125
('Datastore/operation/SQLite/update', 1),
121126
('Datastore/statement/SQLite/datastore_sqlite/update', 1),
122127
('Datastore/operation/SQLite/delete', 1),
@@ -157,7 +162,14 @@ def test_execute_via_connection():
157162
"""(?, ?, ?)""", [(1, 1.0, '1.0'), (2, 2.2, '2.2'),
158163
(3, 3.3, '3.3')])
159164

160-
connection.execute("""select * from datastore_sqlite""")
165+
test_data = [(4, 4.0, '4.0'), (5, 5.5, '5.5'), (6, 6.6, '6.6')]
166+
167+
connection.executemany("""insert into datastore_sqlite values (?, ?, ?)""",
168+
((value) for value in test_data))
169+
170+
cursor = connection.execute("""select * from datastore_sqlite""")
171+
172+
assert len(list(cursor)) == 6
161173

162174
connection.execute("""update datastore_sqlite set a=?, b=?, """
163175
"""c=? where a=?""", (4, 4.0, '4.0', 1))

0 commit comments

Comments
 (0)