mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-30 13:43:03 +08:00
Use is_recording flag in aiopg, asyncpg, dbapi, psycopg2, pymemcache, pymongo, redis, sqlalchemy instrumentations (#1212)
This commit is contained in:
@ -311,6 +311,8 @@ class TracedCursor:
|
||||
def _populate_span(
|
||||
self, span: trace_api.Span, *args: typing.Tuple[typing.Any, typing.Any]
|
||||
):
|
||||
if not span.is_recording():
|
||||
return
|
||||
statement = args[0] if args else ""
|
||||
span.set_attribute(
|
||||
"component", self._db_api_integration.database_component
|
||||
@ -341,10 +343,14 @@ class TracedCursor:
|
||||
self._populate_span(span, *args)
|
||||
try:
|
||||
result = query_method(*args, **kwargs)
|
||||
span.set_status(Status(StatusCanonicalCode.OK))
|
||||
if span.is_recording():
|
||||
span.set_status(Status(StatusCanonicalCode.OK))
|
||||
return result
|
||||
except Exception as ex: # pylint: disable=broad-except
|
||||
span.set_status(Status(StatusCanonicalCode.UNKNOWN, str(ex)))
|
||||
if span.is_recording():
|
||||
span.set_status(
|
||||
Status(StatusCanonicalCode.UNKNOWN, str(ex))
|
||||
)
|
||||
raise ex
|
||||
|
||||
|
||||
|
@ -69,6 +69,38 @@ class TestDBApiIntegration(TestBase):
|
||||
trace_api.status.StatusCanonicalCode.OK,
|
||||
)
|
||||
|
||||
def test_span_not_recording(self):
|
||||
connection_props = {
|
||||
"database": "testdatabase",
|
||||
"server_host": "testhost",
|
||||
"server_port": 123,
|
||||
"user": "testuser",
|
||||
}
|
||||
connection_attributes = {
|
||||
"database": "database",
|
||||
"port": "server_port",
|
||||
"host": "server_host",
|
||||
"user": "user",
|
||||
}
|
||||
mock_tracer = mock.Mock()
|
||||
mock_span = mock.Mock()
|
||||
mock_span.is_recording.return_value = False
|
||||
mock_tracer.start_span.return_value = mock_span
|
||||
mock_tracer.use_span.return_value.__enter__ = mock_span
|
||||
mock_tracer.use_span.return_value.__exit__ = True
|
||||
db_integration = dbapi.DatabaseApiIntegration(
|
||||
mock_tracer, "testcomponent", "testtype", connection_attributes
|
||||
)
|
||||
mock_connection = db_integration.wrapped_connection(
|
||||
mock_connect, {}, connection_props
|
||||
)
|
||||
cursor = mock_connection.cursor()
|
||||
cursor.execute("Test query", ("param1Value", False))
|
||||
self.assertFalse(mock_span.is_recording())
|
||||
self.assertTrue(mock_span.is_recording.called)
|
||||
self.assertFalse(mock_span.set_attribute.called)
|
||||
self.assertFalse(mock_span.set_status.called)
|
||||
|
||||
def test_span_failed(self):
|
||||
db_integration = dbapi.DatabaseApiIntegration(
|
||||
self.tracer, "testcomponent"
|
||||
|
Reference in New Issue
Block a user