Skip to content

Commit 7f65cbc

Browse files
fix: Pymongo raises an AttributeError when the tracer is disabled (#3157) (#3169)
Currently we attempt to wrap pymongo operations in a span when tracer.enabled == False. This raises an exception since spans are not generated on a disabled tracer. An example of this error is linked below: Fix: #3140 (cherry picked from commit af5784e) Co-authored-by: Munir Abdinur <[email protected]>
1 parent dbbb396 commit 7f65cbc

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

ddtrace/contrib/pymongo/client.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ def _datadog_trace_operation(self, operation):
121121
if VERSION >= (3, 12, 0):
122122

123123
def run_operation(self, sock_info, operation, *args, **kwargs):
124-
with self._datadog_trace_operation(operation) as span:
124+
span = self._datadog_trace_operation(operation)
125+
if span is None:
126+
return self.__wrapped__.run_operation(sock_info, operation, *args, **kwargs)
127+
with span:
125128
result = self.__wrapped__.run_operation(sock_info, operation, *args, **kwargs)
126129
if result and result.address:
127130
set_address_tags(span, result.address)
@@ -131,7 +134,10 @@ def run_operation(self, sock_info, operation, *args, **kwargs):
131134
elif (3, 9, 0) <= VERSION < (3, 12, 0):
132135

133136
def run_operation_with_response(self, sock_info, operation, *args, **kwargs):
134-
with self._datadog_trace_operation(operation) as span:
137+
span = self._datadog_trace_operation(operation)
138+
if span is None:
139+
return self.__wrapped__.run_operation_with_response(sock_info, operation, *args, **kwargs)
140+
with span:
135141
result = self.__wrapped__.run_operation_with_response(sock_info, operation, *args, **kwargs)
136142
if result and result.address:
137143
set_address_tags(span, result.address)
@@ -141,7 +147,10 @@ def run_operation_with_response(self, sock_info, operation, *args, **kwargs):
141147
else:
142148

143149
def send_message_with_response(self, operation, *args, **kwargs):
144-
with self._datadog_trace_operation(operation) as span:
150+
span = self._datadog_trace_operation(operation)
151+
if span is None:
152+
return self.__wrapped__.send_message_with_response(operation, *args, **kwargs)
153+
with span:
145154
result = self.__wrapped__.send_message_with_response(operation, *args, **kwargs)
146155
if result and result.address:
147156
set_address_tags(span, result.address)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fixes:
2+
- |
3+
Pymongo instrumentation raises an AttributeError when ``tracer.enabled == False``

tests/contrib/pymongo/test.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,64 @@ def test_user_specified_service(self):
484484
assert len(spans) == 1
485485
assert spans[0].service != "mysvc"
486486

487+
def test_patch_with_disabled_tracer(self):
488+
tracer, client = self.get_tracer_and_client()
489+
tracer.configure(enabled=False)
490+
491+
db = client.testdb
492+
db.drop_collection("teams")
493+
teams = [
494+
{
495+
"name": "Toronto Maple Leafs",
496+
"established": 1917,
497+
},
498+
{
499+
"name": "Montreal Canadiens",
500+
"established": 1910,
501+
},
502+
{
503+
"name": "New York Rangers",
504+
"established": 1926,
505+
},
506+
{
507+
"name": "Boston Knuckleheads",
508+
"established": 1998,
509+
},
510+
]
511+
512+
# create records (exercising both ways of inserting)
513+
db.teams.insert_one(teams[0])
514+
db.teams.insert_many(teams[1:])
515+
# delete record
516+
db.teams.delete_one({"name": "Boston Knuckleheads"})
517+
518+
# assert one team was deleted
519+
cursor = db["teams"].find()
520+
count = 0
521+
for row in cursor:
522+
count += 1
523+
assert count == len(teams) - 1
524+
525+
# query record
526+
q = {"name": "Toronto Maple Leafs"}
527+
queried = list(db.teams.find(q))
528+
assert len(queried) == 1
529+
assert queried[0]["name"] == "Toronto Maple Leafs"
530+
assert queried[0]["established"] == 1917
531+
532+
# update record
533+
result = db.teams.update_many(
534+
{"name": "Toronto Maple Leafs"},
535+
{"$set": {"established": 2021}},
536+
)
537+
assert result.matched_count == 1
538+
queried = list(db.teams.find(q))
539+
assert queried[0]["name"] == "Toronto Maple Leafs"
540+
assert queried[0]["established"] == 2021
541+
542+
# assert no spans were created
543+
assert tracer.pop() == []
544+
487545

488546
class TestPymongoSocketTracing(TracerTestCase):
489547
"""

0 commit comments

Comments
 (0)