Skip to content

Commit 797f647

Browse files
committed
Disable analytics for fetch* and connections
1 parent 8f865a5 commit 797f647

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

ddtrace/contrib/dbapi/__init__.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ def _trace_method(self, method, name, resource, extra_tags, *args, **kwargs):
5050
s.set_tags(pin.tags)
5151
s.set_tags(extra_tags)
5252

53-
# set analytics sample rate if enabled
54-
s.set_tag(
55-
ANALYTICS_SAMPLE_RATE_KEY,
56-
config.dbapi2.get_analytics_sample_rate()
57-
)
53+
# set analytics sample rate if enabled but only for non-FetchTracedCursor
54+
if type(self) is not FetchTracedCursor:
55+
s.set_tag(
56+
ANALYTICS_SAMPLE_RATE_KEY,
57+
config.dbapi2.get_analytics_sample_rate()
58+
)
5859

5960
try:
6061
return method(*args, **kwargs)
@@ -170,12 +171,6 @@ def _trace_method(self, method, name, extra_tags, *args, **kwargs):
170171
s.set_tags(pin.tags)
171172
s.set_tags(extra_tags)
172173

173-
# set analytics sample rate if enabled
174-
s.set_tag(
175-
ANALYTICS_SAMPLE_RATE_KEY,
176-
config.dbapi2.get_analytics_sample_rate()
177-
)
178-
179174
return method(*args, **kwargs)
180175

181176
def cursor(self, *args, **kwargs):

tests/contrib/dbapi/test_unit.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,41 @@ def method():
415415
assert span.get_metric('db.rowcount') == 123, 'Row count is set as a metric'
416416
assert span.get_tag('sql.rows') == '123', 'Row count is set as a tag (for legacy django cursor replacement)'
417417

418+
def test_fetch_analytics_off(self):
419+
with self.override_config(
420+
'dbapi2',
421+
dict(analytics_enabled=True)
422+
):
423+
cursor = self.cursor
424+
cursor.rowcount = 0
425+
cursor.fetchone.return_value = '__result__'
426+
pin = Pin('pin_name', tracer=self.tracer)
427+
traced_cursor = FetchTracedCursor(cursor, pin)
428+
assert '__result__' == traced_cursor.fetchone('arg_1', kwarg1='kwarg1')
429+
430+
span = self.tracer.writer.pop()[0]
431+
self.assertIsNone(span.get_metric(ANALYTICS_SAMPLE_RATE_KEY))
432+
433+
cursor = self.cursor
434+
cursor.rowcount = 0
435+
cursor.fetchall.return_value = '__result__'
436+
pin = Pin('pin_name', tracer=self.tracer)
437+
traced_cursor = FetchTracedCursor(cursor, pin)
438+
assert '__result__' == traced_cursor.fetchall('arg_1', kwarg1='kwarg1')
439+
440+
span = self.tracer.writer.pop()[0]
441+
self.assertIsNone(span.get_metric(ANALYTICS_SAMPLE_RATE_KEY))
442+
443+
cursor = self.cursor
444+
cursor.rowcount = 0
445+
cursor.fetchmany.return_value = '__result__'
446+
pin = Pin('pin_name', tracer=self.tracer)
447+
traced_cursor = FetchTracedCursor(cursor, pin)
448+
assert '__result__' == traced_cursor.fetchmany('arg_1', kwarg1='kwarg1')
449+
450+
span = self.tracer.writer.pop()[0]
451+
self.assertIsNone(span.get_metric(ANALYTICS_SAMPLE_RATE_KEY))
452+
418453

419454
class TestTracedConnection(BaseTracerTestCase):
420455
def setUp(self):
@@ -458,7 +493,7 @@ def test_rollback_is_traced(self):
458493
assert tracer.writer.pop()[0].name == 'mock.connection.rollback'
459494
connection.rollback.assert_called_with()
460495

461-
def test_cursor_analytics_with_rate(self):
496+
def test_connection_analytics_with_rate(self):
462497
with self.override_config(
463498
'dbapi2',
464499
dict(analytics_enabled=True, analytics_sample_rate=0.5)
@@ -470,4 +505,4 @@ def test_cursor_analytics_with_rate(self):
470505
traced_connection = TracedConnection(connection, pin)
471506
traced_connection.commit()
472507
span = tracer.writer.pop()[0]
473-
self.assertEqual(span.get_metric(ANALYTICS_SAMPLE_RATE_KEY), 0.5)
508+
self.assertIsNone(span.get_metric(ANALYTICS_SAMPLE_RATE_KEY))

0 commit comments

Comments
 (0)