Skip to content

Commit 6876ad8

Browse files
authored
Refactor TracedConnectionProxy (#1097)
* Refactor TracedConnectionProxy Fixes #1077 * Remove unecessary methods
1 parent 8afbce7 commit 6876ad8

File tree

4 files changed

+74
-42
lines changed

4 files changed

+74
-42
lines changed

instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ def uninstrument_connection(connection):
214214
Returns:
215215
An uninstrumented connection.
216216
"""
217-
if isinstance(connection, wrapt.ObjectProxy):
218-
return connection.__wrapped__
217+
if isinstance(connection, _TracedConnectionProxy):
218+
return connection._connection
219219

220220
_logger.warning("Connection is not instrumented")
221221
return connection
@@ -300,28 +300,35 @@ def get_connection_attributes(self, connection):
300300
self.span_attributes[SpanAttributes.NET_PEER_PORT] = port
301301

302302

303+
class _TracedConnectionProxy:
304+
pass
305+
306+
303307
def get_traced_connection_proxy(
304308
connection, db_api_integration, *args, **kwargs
305309
):
306310
# pylint: disable=abstract-method
307-
class TracedConnectionProxy(wrapt.ObjectProxy):
308-
# pylint: disable=unused-argument
309-
def __init__(self, connection, *args, **kwargs):
310-
wrapt.ObjectProxy.__init__(self, connection)
311+
class TracedConnectionProxy(type(connection), _TracedConnectionProxy):
312+
def __init__(self, connection):
313+
self._connection = connection
314+
315+
def __getattr__(self, name):
316+
return object.__getattribute__(
317+
object.__getattribute__(self, "_connection"), name
318+
)
311319

312320
def cursor(self, *args, **kwargs):
313321
return get_traced_cursor_proxy(
314-
self.__wrapped__.cursor(*args, **kwargs), db_api_integration
322+
self._connection.cursor(*args, **kwargs), db_api_integration
315323
)
316324

317-
def __enter__(self):
318-
self.__wrapped__.__enter__()
319-
return self
320-
321-
def __exit__(self, *args, **kwargs):
322-
self.__wrapped__.__exit__(*args, **kwargs)
325+
# For some reason this is necessary as trying to access the close
326+
# method of self._connection via __getattr__ leads to unexplained
327+
# errors.
328+
def close(self):
329+
self._connection.close()
323330

324-
return TracedConnectionProxy(connection, *args, **kwargs)
331+
return TracedConnectionProxy(connection)
325332

326333

327334
class CursorTracer:

instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,14 @@ def test_callproc(self):
262262

263263
@mock.patch("opentelemetry.instrumentation.dbapi")
264264
def test_wrap_connect(self, mock_dbapi):
265-
dbapi.wrap_connect(self.tracer, mock_dbapi, "connect", "-")
265+
dbapi.wrap_connect(self.tracer, MockConnectionEmpty(), "connect", "-")
266266
connection = mock_dbapi.connect()
267267
self.assertEqual(mock_dbapi.connect.call_count, 1)
268-
self.assertIsInstance(connection.__wrapped__, mock.Mock)
268+
self.assertIsInstance(connection._connection, mock.Mock)
269269

270270
@mock.patch("opentelemetry.instrumentation.dbapi")
271271
def test_unwrap_connect(self, mock_dbapi):
272-
dbapi.wrap_connect(self.tracer, mock_dbapi, "connect", "-")
272+
dbapi.wrap_connect(self.tracer, MockConnectionEmpty(), "connect", "-")
273273
connection = mock_dbapi.connect()
274274
self.assertEqual(mock_dbapi.connect.call_count, 1)
275275

@@ -279,19 +279,21 @@ def test_unwrap_connect(self, mock_dbapi):
279279
self.assertIsInstance(connection, mock.Mock)
280280

281281
def test_instrument_connection(self):
282-
connection = mock.Mock()
282+
connection = MockConnectionEmpty()
283283
# Avoid get_attributes failing because can't concatenate mock
284+
# pylint: disable=attribute-defined-outside-init
284285
connection.database = "-"
285286
connection2 = dbapi.instrument_connection(self.tracer, connection, "-")
286-
self.assertIs(connection2.__wrapped__, connection)
287+
self.assertIs(connection2._connection, connection)
287288

288289
def test_uninstrument_connection(self):
289-
connection = mock.Mock()
290+
connection = MockConnectionEmpty()
290291
# Set connection.database to avoid a failure because mock can't
291292
# be concatenated
293+
# pylint: disable=attribute-defined-outside-init
292294
connection.database = "-"
293295
connection2 = dbapi.instrument_connection(self.tracer, connection, "-")
294-
self.assertIs(connection2.__wrapped__, connection)
296+
self.assertIs(connection2._connection, connection)
295297

296298
connection3 = dbapi.uninstrument_connection(connection2)
297299
self.assertIs(connection3, connection)
@@ -307,10 +309,12 @@ def mock_connect(*args, **kwargs):
307309
server_host = kwargs.get("server_host")
308310
server_port = kwargs.get("server_port")
309311
user = kwargs.get("user")
310-
return MockConnection(database, server_port, server_host, user)
312+
return MockConnectionWithAttributes(
313+
database, server_port, server_host, user
314+
)
311315

312316

313-
class MockConnection:
317+
class MockConnectionWithAttributes:
314318
def __init__(self, database, server_port, server_host, user):
315319
self.database = database
316320
self.server_port = server_port
@@ -343,3 +347,7 @@ def executemany(self, query, params=None, throw_exception=False):
343347
def callproc(self, query, params=None, throw_exception=False):
344348
if throw_exception:
345349
raise Exception("Test Exception")
350+
351+
352+
class MockConnectionEmpty:
353+
pass

instrumentation/opentelemetry-instrumentation-mysql/tests/test_mysql_integration.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from unittest import mock
15+
from unittest.mock import Mock, patch
1616

1717
import mysql.connector
1818

@@ -22,15 +22,24 @@
2222
from opentelemetry.test.test_base import TestBase
2323

2424

25+
def mock_connect(*args, **kwargs):
26+
class MockConnection:
27+
def cursor(self):
28+
# pylint: disable=no-self-use
29+
return Mock()
30+
31+
return MockConnection()
32+
33+
2534
class TestMysqlIntegration(TestBase):
2635
def tearDown(self):
2736
super().tearDown()
2837
with self.disable_logging():
2938
MySQLInstrumentor().uninstrument()
3039

31-
@mock.patch("mysql.connector.connect")
40+
@patch("mysql.connector.connect", new=mock_connect)
3241
# pylint: disable=unused-argument
33-
def test_instrumentor(self, mock_connect):
42+
def test_instrumentor(self):
3443
MySQLInstrumentor().instrument()
3544

3645
cnx = mysql.connector.connect(database="test")
@@ -58,9 +67,8 @@ def test_instrumentor(self, mock_connect):
5867
spans_list = self.memory_exporter.get_finished_spans()
5968
self.assertEqual(len(spans_list), 1)
6069

61-
@mock.patch("mysql.connector.connect")
62-
# pylint: disable=unused-argument
63-
def test_custom_tracer_provider(self, mock_connect):
70+
@patch("mysql.connector.connect", new=mock_connect)
71+
def test_custom_tracer_provider(self):
6472
resource = resources.Resource.create({})
6573
result = self.create_tracer_provider(resource=resource)
6674
tracer_provider, exporter = result
@@ -77,9 +85,9 @@ def test_custom_tracer_provider(self, mock_connect):
7785

7886
self.assertIs(span.resource, resource)
7987

80-
@mock.patch("mysql.connector.connect")
88+
@patch("mysql.connector.connect", new=mock_connect)
8189
# pylint: disable=unused-argument
82-
def test_instrument_connection(self, mock_connect):
90+
def test_instrument_connection(self):
8391
cnx = mysql.connector.connect(database="test")
8492
query = "SELECT * FROM test"
8593
cursor = cnx.cursor()
@@ -95,9 +103,9 @@ def test_instrument_connection(self, mock_connect):
95103
spans_list = self.memory_exporter.get_finished_spans()
96104
self.assertEqual(len(spans_list), 1)
97105

98-
@mock.patch("mysql.connector.connect")
106+
@patch("mysql.connector.connect", new=mock_connect)
99107
# pylint: disable=unused-argument
100-
def test_uninstrument_connection(self, mock_connect):
108+
def test_uninstrument_connection(self):
101109
MySQLInstrumentor().instrument()
102110
cnx = mysql.connector.connect(database="test")
103111
query = "SELECT * FROM test"

instrumentation/opentelemetry-instrumentation-pymysql/tests/test_pymysql_integration.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from unittest import mock
15+
from unittest.mock import Mock, patch
1616

1717
import pymysql
1818

@@ -22,15 +22,24 @@
2222
from opentelemetry.test.test_base import TestBase
2323

2424

25+
def mock_connect(*args, **kwargs):
26+
class MockConnection:
27+
def cursor(self):
28+
# pylint: disable=no-self-use
29+
return Mock()
30+
31+
return MockConnection()
32+
33+
2534
class TestPyMysqlIntegration(TestBase):
2635
def tearDown(self):
2736
super().tearDown()
2837
with self.disable_logging():
2938
PyMySQLInstrumentor().uninstrument()
3039

31-
@mock.patch("pymysql.connect")
40+
@patch("pymysql.connect", new=mock_connect)
3241
# pylint: disable=unused-argument
33-
def test_instrumentor(self, mock_connect):
42+
def test_instrumentor(self):
3443
PyMySQLInstrumentor().instrument()
3544

3645
cnx = pymysql.connect(database="test")
@@ -58,9 +67,9 @@ def test_instrumentor(self, mock_connect):
5867
spans_list = self.memory_exporter.get_finished_spans()
5968
self.assertEqual(len(spans_list), 1)
6069

61-
@mock.patch("pymysql.connect")
70+
@patch("pymysql.connect", new=mock_connect)
6271
# pylint: disable=unused-argument
63-
def test_custom_tracer_provider(self, mock_connect):
72+
def test_custom_tracer_provider(self):
6473
resource = resources.Resource.create({})
6574
result = self.create_tracer_provider(resource=resource)
6675
tracer_provider, exporter = result
@@ -78,9 +87,9 @@ def test_custom_tracer_provider(self, mock_connect):
7887

7988
self.assertIs(span.resource, resource)
8089

81-
@mock.patch("pymysql.connect")
90+
@patch("pymysql.connect", new=mock_connect)
8291
# pylint: disable=unused-argument
83-
def test_instrument_connection(self, mock_connect):
92+
def test_instrument_connection(self):
8493
cnx = pymysql.connect(database="test")
8594
query = "SELECT * FROM test"
8695
cursor = cnx.cursor()
@@ -96,9 +105,9 @@ def test_instrument_connection(self, mock_connect):
96105
spans_list = self.memory_exporter.get_finished_spans()
97106
self.assertEqual(len(spans_list), 1)
98107

99-
@mock.patch("pymysql.connect")
108+
@patch("pymysql.connect", new=mock_connect)
100109
# pylint: disable=unused-argument
101-
def test_uninstrument_connection(self, mock_connect):
110+
def test_uninstrument_connection(self):
102111
PyMySQLInstrumentor().instrument()
103112
cnx = pymysql.connect(database="test")
104113
query = "SELECT * FROM test"

0 commit comments

Comments
 (0)