mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-31 06:03:21 +08:00
Several issues have arisen from this bugfix, reverting here until a better solution can be found. Fixes #1658 Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
This commit is contained in:
@ -203,7 +203,7 @@ def instrument_connection(
|
|||||||
Returns:
|
Returns:
|
||||||
An instrumented connection.
|
An instrumented connection.
|
||||||
"""
|
"""
|
||||||
if isinstance(connection, _TracedConnectionProxy):
|
if isinstance(connection, wrapt.ObjectProxy):
|
||||||
_logger.warning("Connection already instrumented")
|
_logger.warning("Connection already instrumented")
|
||||||
return connection
|
return connection
|
||||||
|
|
||||||
@ -230,8 +230,8 @@ def uninstrument_connection(connection):
|
|||||||
Returns:
|
Returns:
|
||||||
An uninstrumented connection.
|
An uninstrumented connection.
|
||||||
"""
|
"""
|
||||||
if isinstance(connection, _TracedConnectionProxy):
|
if isinstance(connection, wrapt.ObjectProxy):
|
||||||
return connection._connection
|
return connection.__wrapped__
|
||||||
|
|
||||||
_logger.warning("Connection is not instrumented")
|
_logger.warning("Connection is not instrumented")
|
||||||
return connection
|
return connection
|
||||||
@ -320,22 +320,14 @@ class DatabaseApiIntegration:
|
|||||||
self.span_attributes[SpanAttributes.NET_PEER_PORT] = port
|
self.span_attributes[SpanAttributes.NET_PEER_PORT] = port
|
||||||
|
|
||||||
|
|
||||||
class _TracedConnectionProxy:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def get_traced_connection_proxy(
|
def get_traced_connection_proxy(
|
||||||
connection, db_api_integration, *args, **kwargs
|
connection, db_api_integration, *args, **kwargs
|
||||||
):
|
):
|
||||||
# pylint: disable=abstract-method
|
# pylint: disable=abstract-method
|
||||||
class TracedConnectionProxy(type(connection), _TracedConnectionProxy):
|
class TracedConnectionProxy(wrapt.ObjectProxy):
|
||||||
def __init__(self, connection):
|
# pylint: disable=unused-argument
|
||||||
self._connection = connection
|
def __init__(self, connection, *args, **kwargs):
|
||||||
|
wrapt.ObjectProxy.__init__(self, connection)
|
||||||
def __getattr__(self, name):
|
|
||||||
return object.__getattribute__(
|
|
||||||
object.__getattribute__(self, "_connection"), name
|
|
||||||
)
|
|
||||||
|
|
||||||
def __getattribute__(self, name):
|
def __getattribute__(self, name):
|
||||||
if object.__getattribute__(self, name):
|
if object.__getattribute__(self, name):
|
||||||
@ -347,16 +339,17 @@ def get_traced_connection_proxy(
|
|||||||
|
|
||||||
def cursor(self, *args, **kwargs):
|
def cursor(self, *args, **kwargs):
|
||||||
return get_traced_cursor_proxy(
|
return get_traced_cursor_proxy(
|
||||||
self._connection.cursor(*args, **kwargs), db_api_integration
|
self.__wrapped__.cursor(*args, **kwargs), db_api_integration
|
||||||
)
|
)
|
||||||
|
|
||||||
# For some reason this is necessary as trying to access the close
|
def __enter__(self):
|
||||||
# method of self._connection via __getattr__ leads to unexplained
|
self.__wrapped__.__enter__()
|
||||||
# errors.
|
return self
|
||||||
def close(self):
|
|
||||||
self._connection.close()
|
|
||||||
|
|
||||||
return TracedConnectionProxy(connection)
|
def __exit__(self, *args, **kwargs):
|
||||||
|
self.__wrapped__.__exit__(*args, **kwargs)
|
||||||
|
|
||||||
|
return TracedConnectionProxy(connection, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class CursorTracer:
|
class CursorTracer:
|
||||||
|
@ -325,14 +325,14 @@ class TestDBApiIntegration(TestBase):
|
|||||||
|
|
||||||
@mock.patch("opentelemetry.instrumentation.dbapi")
|
@mock.patch("opentelemetry.instrumentation.dbapi")
|
||||||
def test_wrap_connect(self, mock_dbapi):
|
def test_wrap_connect(self, mock_dbapi):
|
||||||
dbapi.wrap_connect(self.tracer, MockConnectionEmpty(), "connect", "-")
|
dbapi.wrap_connect(self.tracer, mock_dbapi, "connect", "-")
|
||||||
connection = mock_dbapi.connect()
|
connection = mock_dbapi.connect()
|
||||||
self.assertEqual(mock_dbapi.connect.call_count, 1)
|
self.assertEqual(mock_dbapi.connect.call_count, 1)
|
||||||
self.assertIsInstance(connection._connection, mock.Mock)
|
self.assertIsInstance(connection.__wrapped__, mock.Mock)
|
||||||
|
|
||||||
@mock.patch("opentelemetry.instrumentation.dbapi")
|
@mock.patch("opentelemetry.instrumentation.dbapi")
|
||||||
def test_unwrap_connect(self, mock_dbapi):
|
def test_unwrap_connect(self, mock_dbapi):
|
||||||
dbapi.wrap_connect(self.tracer, MockConnectionEmpty(), "connect", "-")
|
dbapi.wrap_connect(self.tracer, mock_dbapi, "connect", "-")
|
||||||
connection = mock_dbapi.connect()
|
connection = mock_dbapi.connect()
|
||||||
self.assertEqual(mock_dbapi.connect.call_count, 1)
|
self.assertEqual(mock_dbapi.connect.call_count, 1)
|
||||||
|
|
||||||
@ -342,21 +342,19 @@ class TestDBApiIntegration(TestBase):
|
|||||||
self.assertIsInstance(connection, mock.Mock)
|
self.assertIsInstance(connection, mock.Mock)
|
||||||
|
|
||||||
def test_instrument_connection(self):
|
def test_instrument_connection(self):
|
||||||
connection = MockConnectionEmpty()
|
connection = mock.Mock()
|
||||||
# Avoid get_attributes failing because can't concatenate mock
|
# Avoid get_attributes failing because can't concatenate mock
|
||||||
# pylint: disable=attribute-defined-outside-init
|
|
||||||
connection.database = "-"
|
connection.database = "-"
|
||||||
connection2 = dbapi.instrument_connection(self.tracer, connection, "-")
|
connection2 = dbapi.instrument_connection(self.tracer, connection, "-")
|
||||||
self.assertIs(connection2._connection, connection)
|
self.assertIs(connection2.__wrapped__, connection)
|
||||||
|
|
||||||
def test_uninstrument_connection(self):
|
def test_uninstrument_connection(self):
|
||||||
connection = MockConnectionEmpty()
|
connection = mock.Mock()
|
||||||
# Set connection.database to avoid a failure because mock can't
|
# Set connection.database to avoid a failure because mock can't
|
||||||
# be concatenated
|
# be concatenated
|
||||||
# pylint: disable=attribute-defined-outside-init
|
|
||||||
connection.database = "-"
|
connection.database = "-"
|
||||||
connection2 = dbapi.instrument_connection(self.tracer, connection, "-")
|
connection2 = dbapi.instrument_connection(self.tracer, connection, "-")
|
||||||
self.assertIs(connection2._connection, connection)
|
self.assertIs(connection2.__wrapped__, connection)
|
||||||
|
|
||||||
connection3 = dbapi.uninstrument_connection(connection2)
|
connection3 = dbapi.uninstrument_connection(connection2)
|
||||||
self.assertIs(connection3, connection)
|
self.assertIs(connection3, connection)
|
||||||
@ -372,12 +370,10 @@ def mock_connect(*args, **kwargs):
|
|||||||
server_host = kwargs.get("server_host")
|
server_host = kwargs.get("server_host")
|
||||||
server_port = kwargs.get("server_port")
|
server_port = kwargs.get("server_port")
|
||||||
user = kwargs.get("user")
|
user = kwargs.get("user")
|
||||||
return MockConnectionWithAttributes(
|
return MockConnection(database, server_port, server_host, user)
|
||||||
database, server_port, server_host, user
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class MockConnectionWithAttributes:
|
class MockConnection:
|
||||||
def __init__(self, database, server_port, server_host, user):
|
def __init__(self, database, server_port, server_host, user):
|
||||||
self.database = database
|
self.database = database
|
||||||
self.server_port = server_port
|
self.server_port = server_port
|
||||||
@ -410,7 +406,3 @@ class MockCursor:
|
|||||||
def callproc(self, query, params=None, throw_exception=False):
|
def callproc(self, query, params=None, throw_exception=False):
|
||||||
if throw_exception:
|
if throw_exception:
|
||||||
raise Exception("Test Exception")
|
raise Exception("Test Exception")
|
||||||
|
|
||||||
|
|
||||||
class MockConnectionEmpty:
|
|
||||||
pass
|
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
from unittest.mock import Mock, patch
|
from unittest import mock
|
||||||
|
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
|
|
||||||
@ -23,15 +23,6 @@ from opentelemetry.sdk import resources
|
|||||||
from opentelemetry.test.test_base import TestBase
|
from opentelemetry.test.test_base import TestBase
|
||||||
|
|
||||||
|
|
||||||
def mock_connect(*args, **kwargs):
|
|
||||||
class MockConnection:
|
|
||||||
def cursor(self):
|
|
||||||
# pylint: disable=no-self-use
|
|
||||||
return Mock()
|
|
||||||
|
|
||||||
return MockConnection()
|
|
||||||
|
|
||||||
|
|
||||||
def connect_and_execute_query():
|
def connect_and_execute_query():
|
||||||
cnx = mysql.connector.connect(database="test")
|
cnx = mysql.connector.connect(database="test")
|
||||||
cursor = cnx.cursor()
|
cursor = cnx.cursor()
|
||||||
@ -47,9 +38,9 @@ class TestMysqlIntegration(TestBase):
|
|||||||
with self.disable_logging():
|
with self.disable_logging():
|
||||||
MySQLInstrumentor().uninstrument()
|
MySQLInstrumentor().uninstrument()
|
||||||
|
|
||||||
@patch("mysql.connector.connect", new=mock_connect)
|
@mock.patch("mysql.connector.connect")
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def test_instrumentor(self):
|
def test_instrumentor(self, mock_connect):
|
||||||
MySQLInstrumentor().instrument()
|
MySQLInstrumentor().instrument()
|
||||||
|
|
||||||
connect_and_execute_query()
|
connect_and_execute_query()
|
||||||
@ -71,8 +62,9 @@ class TestMysqlIntegration(TestBase):
|
|||||||
spans_list = self.memory_exporter.get_finished_spans()
|
spans_list = self.memory_exporter.get_finished_spans()
|
||||||
self.assertEqual(len(spans_list), 1)
|
self.assertEqual(len(spans_list), 1)
|
||||||
|
|
||||||
@patch("mysql.connector.connect", new=mock_connect)
|
@mock.patch("mysql.connector.connect")
|
||||||
def test_custom_tracer_provider(self):
|
# pylint: disable=unused-argument
|
||||||
|
def test_custom_tracer_provider(self, mock_connect):
|
||||||
resource = resources.Resource.create({})
|
resource = resources.Resource.create({})
|
||||||
result = self.create_tracer_provider(resource=resource)
|
result = self.create_tracer_provider(resource=resource)
|
||||||
tracer_provider, exporter = result
|
tracer_provider, exporter = result
|
||||||
@ -86,9 +78,9 @@ class TestMysqlIntegration(TestBase):
|
|||||||
|
|
||||||
self.assertIs(span.resource, resource)
|
self.assertIs(span.resource, resource)
|
||||||
|
|
||||||
@patch("mysql.connector.connect", new=mock_connect)
|
@mock.patch("mysql.connector.connect")
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def test_instrument_connection(self):
|
def test_instrument_connection(self, mock_connect):
|
||||||
cnx, query = connect_and_execute_query()
|
cnx, query = connect_and_execute_query()
|
||||||
|
|
||||||
spans_list = self.memory_exporter.get_finished_spans()
|
spans_list = self.memory_exporter.get_finished_spans()
|
||||||
@ -101,8 +93,8 @@ class TestMysqlIntegration(TestBase):
|
|||||||
spans_list = self.memory_exporter.get_finished_spans()
|
spans_list = self.memory_exporter.get_finished_spans()
|
||||||
self.assertEqual(len(spans_list), 1)
|
self.assertEqual(len(spans_list), 1)
|
||||||
|
|
||||||
@patch("mysql.connector.connect", new=mock_connect)
|
@mock.patch("mysql.connector.connect")
|
||||||
def test_instrument_connection_no_op_tracer_provider(self):
|
def test_instrument_connection_no_op_tracer_provider(self, mock_connect):
|
||||||
tracer_provider = trace_api.NoOpTracerProvider()
|
tracer_provider = trace_api.NoOpTracerProvider()
|
||||||
MySQLInstrumentor().instrument(tracer_provider=tracer_provider)
|
MySQLInstrumentor().instrument(tracer_provider=tracer_provider)
|
||||||
connect_and_execute_query()
|
connect_and_execute_query()
|
||||||
@ -110,9 +102,9 @@ class TestMysqlIntegration(TestBase):
|
|||||||
spans_list = self.memory_exporter.get_finished_spans()
|
spans_list = self.memory_exporter.get_finished_spans()
|
||||||
self.assertEqual(len(spans_list), 0)
|
self.assertEqual(len(spans_list), 0)
|
||||||
|
|
||||||
@patch("mysql.connector.connect", new=mock_connect)
|
@mock.patch("mysql.connector.connect")
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def test_uninstrument_connection(self):
|
def test_uninstrument_connection(self, mock_connect):
|
||||||
MySQLInstrumentor().instrument()
|
MySQLInstrumentor().instrument()
|
||||||
cnx, query = connect_and_execute_query()
|
cnx, query = connect_and_execute_query()
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
from unittest.mock import Mock, patch
|
from unittest import mock
|
||||||
|
|
||||||
import pymysql
|
import pymysql
|
||||||
|
|
||||||
@ -22,24 +22,15 @@ from opentelemetry.sdk import resources
|
|||||||
from opentelemetry.test.test_base import TestBase
|
from opentelemetry.test.test_base import TestBase
|
||||||
|
|
||||||
|
|
||||||
def mock_connect(*args, **kwargs):
|
|
||||||
class MockConnection:
|
|
||||||
def cursor(self):
|
|
||||||
# pylint: disable=no-self-use
|
|
||||||
return Mock()
|
|
||||||
|
|
||||||
return MockConnection()
|
|
||||||
|
|
||||||
|
|
||||||
class TestPyMysqlIntegration(TestBase):
|
class TestPyMysqlIntegration(TestBase):
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
super().tearDown()
|
super().tearDown()
|
||||||
with self.disable_logging():
|
with self.disable_logging():
|
||||||
PyMySQLInstrumentor().uninstrument()
|
PyMySQLInstrumentor().uninstrument()
|
||||||
|
|
||||||
@patch("pymysql.connect", new=mock_connect)
|
@mock.patch("pymysql.connect")
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def test_instrumentor(self):
|
def test_instrumentor(self, mock_connect):
|
||||||
PyMySQLInstrumentor().instrument()
|
PyMySQLInstrumentor().instrument()
|
||||||
|
|
||||||
cnx = pymysql.connect(database="test")
|
cnx = pymysql.connect(database="test")
|
||||||
@ -67,9 +58,9 @@ class TestPyMysqlIntegration(TestBase):
|
|||||||
spans_list = self.memory_exporter.get_finished_spans()
|
spans_list = self.memory_exporter.get_finished_spans()
|
||||||
self.assertEqual(len(spans_list), 1)
|
self.assertEqual(len(spans_list), 1)
|
||||||
|
|
||||||
@patch("pymysql.connect", new=mock_connect)
|
@mock.patch("pymysql.connect")
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def test_custom_tracer_provider(self):
|
def test_custom_tracer_provider(self, mock_connect):
|
||||||
resource = resources.Resource.create({})
|
resource = resources.Resource.create({})
|
||||||
result = self.create_tracer_provider(resource=resource)
|
result = self.create_tracer_provider(resource=resource)
|
||||||
tracer_provider, exporter = result
|
tracer_provider, exporter = result
|
||||||
@ -87,9 +78,9 @@ class TestPyMysqlIntegration(TestBase):
|
|||||||
|
|
||||||
self.assertIs(span.resource, resource)
|
self.assertIs(span.resource, resource)
|
||||||
|
|
||||||
@patch("pymysql.connect", new=mock_connect)
|
@mock.patch("pymysql.connect")
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def test_instrument_connection(self):
|
def test_instrument_connection(self, mock_connect):
|
||||||
cnx = pymysql.connect(database="test")
|
cnx = pymysql.connect(database="test")
|
||||||
query = "SELECT * FROM test"
|
query = "SELECT * FROM test"
|
||||||
cursor = cnx.cursor()
|
cursor = cnx.cursor()
|
||||||
@ -105,9 +96,9 @@ class TestPyMysqlIntegration(TestBase):
|
|||||||
spans_list = self.memory_exporter.get_finished_spans()
|
spans_list = self.memory_exporter.get_finished_spans()
|
||||||
self.assertEqual(len(spans_list), 1)
|
self.assertEqual(len(spans_list), 1)
|
||||||
|
|
||||||
@patch("pymysql.connect", new=mock_connect)
|
@mock.patch("pymysql.connect")
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def test_uninstrument_connection(self):
|
def test_uninstrument_connection(self, mock_connect):
|
||||||
PyMySQLInstrumentor().instrument()
|
PyMySQLInstrumentor().instrument()
|
||||||
cnx = pymysql.connect(database="test")
|
cnx = pymysql.connect(database="test")
|
||||||
query = "SELECT * FROM test"
|
query = "SELECT * FROM test"
|
||||||
|
Reference in New Issue
Block a user