|
28 | 28 | log = get_logger(__name__) |
29 | 29 |
|
30 | 30 |
|
| 31 | +VERSION = pymongo.version_tuple |
| 32 | + |
| 33 | + |
31 | 34 | class TracedMongoClient(ObjectProxy): |
32 | 35 | def __init__(self, client=None, *args, **kwargs): |
33 | 36 | # To support the former trace_mongo_client interface, we have to keep this old interface |
@@ -114,35 +117,35 @@ def _datadog_trace_operation(self, operation): |
114 | 117 | span.set_tag(ANALYTICS_SAMPLE_RATE_KEY, sample_rate) |
115 | 118 | return span |
116 | 119 |
|
117 | | - # Pymongo >= 3.9 |
118 | | - def run_operation_with_response(self, sock_info, operation, *args, **kwargs): |
119 | | - span = self._datadog_trace_operation(operation) |
120 | | - if not span: |
121 | | - return self.__wrapped__.run_operation_with_response(sock_info, operation, *args, **kwargs) |
| 120 | + # Pymongo >= 3.12 |
| 121 | + if VERSION >= (3, 12, 0): |
122 | 122 |
|
123 | | - try: |
124 | | - result = self.__wrapped__.run_operation_with_response(sock_info, operation, *args, **kwargs) |
| 123 | + def run_operation(self, sock_info, operation, *args, **kwargs): |
| 124 | + with self._datadog_trace_operation(operation) as span: |
| 125 | + result = self.__wrapped__.run_operation(sock_info, operation, *args, **kwargs) |
| 126 | + if result and result.address: |
| 127 | + set_address_tags(span, result.address) |
| 128 | + return result |
125 | 129 |
|
126 | | - if result and result.address: |
127 | | - set_address_tags(span, result.address) |
128 | | - return result |
129 | | - finally: |
130 | | - span.finish() |
| 130 | + # Pymongo >= 3.9, <3.12 |
| 131 | + elif (3, 9, 0) <= VERSION < (3, 12, 0): |
131 | 132 |
|
132 | | - # Pymongo < 3.9 |
133 | | - def send_message_with_response(self, operation, *args, **kwargs): |
134 | | - span = self._datadog_trace_operation(operation) |
135 | | - if not span: |
136 | | - return self.__wrapped__.send_message_with_response(operation, *args, **kwargs) |
| 133 | + def run_operation_with_response(self, sock_info, operation, *args, **kwargs): |
| 134 | + with self._datadog_trace_operation(operation) as span: |
| 135 | + result = self.__wrapped__.run_operation_with_response(sock_info, operation, *args, **kwargs) |
| 136 | + if result and result.address: |
| 137 | + set_address_tags(span, result.address) |
| 138 | + return result |
137 | 139 |
|
138 | | - try: |
139 | | - result = self.__wrapped__.send_message_with_response(operation, *args, **kwargs) |
| 140 | + # Pymongo < 3.9 |
| 141 | + else: |
140 | 142 |
|
141 | | - if result and result.address: |
142 | | - set_address_tags(span, result.address) |
143 | | - return result |
144 | | - finally: |
145 | | - span.finish() |
| 143 | + def send_message_with_response(self, operation, *args, **kwargs): |
| 144 | + with self._datadog_trace_operation(operation) as span: |
| 145 | + result = self.__wrapped__.send_message_with_response(operation, *args, **kwargs) |
| 146 | + if result and result.address: |
| 147 | + set_address_tags(span, result.address) |
| 148 | + return result |
146 | 149 |
|
147 | 150 | @contextlib.contextmanager |
148 | 151 | def get_socket(self, *args, **kwargs): |
|
0 commit comments