mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-08-01 17:34:38 +08:00
Use is_recording flag in aiopg, asyncpg, dbapi, psycopg2, pymemcache, pymongo, redis, sqlalchemy instrumentations (#1212)
This commit is contained in:
@ -71,28 +71,32 @@ class CommandTracer(monitoring.CommandListener):
|
||||
|
||||
try:
|
||||
span = self._tracer.start_span(name, kind=SpanKind.CLIENT)
|
||||
span.set_attribute("component", DATABASE_TYPE)
|
||||
span.set_attribute("db.type", DATABASE_TYPE)
|
||||
span.set_attribute("db.instance", event.database_name)
|
||||
span.set_attribute("db.statement", statement)
|
||||
if event.connection_id is not None:
|
||||
span.set_attribute("net.peer.name", event.connection_id[0])
|
||||
span.set_attribute("net.peer.port", event.connection_id[1])
|
||||
if span.is_recording():
|
||||
span.set_attribute("component", DATABASE_TYPE)
|
||||
span.set_attribute("db.type", DATABASE_TYPE)
|
||||
span.set_attribute("db.instance", event.database_name)
|
||||
span.set_attribute("db.statement", statement)
|
||||
if event.connection_id is not None:
|
||||
span.set_attribute("net.peer.name", event.connection_id[0])
|
||||
span.set_attribute("net.peer.port", event.connection_id[1])
|
||||
|
||||
# pymongo specific, not specified by spec
|
||||
span.set_attribute("db.mongo.operation_id", event.operation_id)
|
||||
span.set_attribute("db.mongo.request_id", event.request_id)
|
||||
# pymongo specific, not specified by spec
|
||||
span.set_attribute("db.mongo.operation_id", event.operation_id)
|
||||
span.set_attribute("db.mongo.request_id", event.request_id)
|
||||
|
||||
for attr in COMMAND_ATTRIBUTES:
|
||||
_attr = event.command.get(attr)
|
||||
if _attr is not None:
|
||||
span.set_attribute("db.mongo." + attr, str(_attr))
|
||||
for attr in COMMAND_ATTRIBUTES:
|
||||
_attr = event.command.get(attr)
|
||||
if _attr is not None:
|
||||
span.set_attribute("db.mongo." + attr, str(_attr))
|
||||
|
||||
# Add Span to dictionary
|
||||
self._span_dict[_get_span_dict_key(event)] = span
|
||||
except Exception as ex: # noqa pylint: disable=broad-except
|
||||
if span is not None:
|
||||
span.set_status(Status(StatusCanonicalCode.INTERNAL, str(ex)))
|
||||
if span.is_recording():
|
||||
span.set_status(
|
||||
Status(StatusCanonicalCode.INTERNAL, str(ex))
|
||||
)
|
||||
span.end()
|
||||
self._pop_span(event)
|
||||
|
||||
@ -103,8 +107,11 @@ class CommandTracer(monitoring.CommandListener):
|
||||
span = self._pop_span(event)
|
||||
if span is None:
|
||||
return
|
||||
span.set_attribute("db.mongo.duration_micros", event.duration_micros)
|
||||
span.set_status(Status(StatusCanonicalCode.OK, event.reply))
|
||||
if span.is_recording():
|
||||
span.set_attribute(
|
||||
"db.mongo.duration_micros", event.duration_micros
|
||||
)
|
||||
span.set_status(Status(StatusCanonicalCode.OK, event.reply))
|
||||
span.end()
|
||||
|
||||
def failed(self, event: monitoring.CommandFailedEvent):
|
||||
@ -114,8 +121,11 @@ class CommandTracer(monitoring.CommandListener):
|
||||
span = self._pop_span(event)
|
||||
if span is None:
|
||||
return
|
||||
span.set_attribute("db.mongo.duration_micros", event.duration_micros)
|
||||
span.set_status(Status(StatusCanonicalCode.UNKNOWN, event.failure))
|
||||
if span.is_recording():
|
||||
span.set_attribute(
|
||||
"db.mongo.duration_micros", event.duration_micros
|
||||
)
|
||||
span.set_status(Status(StatusCanonicalCode.UNKNOWN, event.failure))
|
||||
span.end()
|
||||
|
||||
def _pop_span(self, event):
|
||||
|
@ -91,6 +91,22 @@ class TestPymongo(TestBase):
|
||||
self.assertEqual(span.status.description, "reply")
|
||||
self.assertIsNotNone(span.end_time)
|
||||
|
||||
def test_not_recording(self):
|
||||
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
|
||||
mock_event = MockEvent({})
|
||||
command_tracer = CommandTracer(mock_tracer)
|
||||
command_tracer.started(event=mock_event)
|
||||
command_tracer.succeeded(event=mock_event)
|
||||
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_failed(self):
|
||||
mock_event = MockEvent({})
|
||||
command_tracer = CommandTracer(self.tracer)
|
||||
|
Reference in New Issue
Block a user