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:
|
try:
|
||||||
span = self._tracer.start_span(name, kind=SpanKind.CLIENT)
|
span = self._tracer.start_span(name, kind=SpanKind.CLIENT)
|
||||||
span.set_attribute("component", DATABASE_TYPE)
|
if span.is_recording():
|
||||||
span.set_attribute("db.type", DATABASE_TYPE)
|
span.set_attribute("component", DATABASE_TYPE)
|
||||||
span.set_attribute("db.instance", event.database_name)
|
span.set_attribute("db.type", DATABASE_TYPE)
|
||||||
span.set_attribute("db.statement", statement)
|
span.set_attribute("db.instance", event.database_name)
|
||||||
if event.connection_id is not None:
|
span.set_attribute("db.statement", statement)
|
||||||
span.set_attribute("net.peer.name", event.connection_id[0])
|
if event.connection_id is not None:
|
||||||
span.set_attribute("net.peer.port", event.connection_id[1])
|
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
|
# pymongo specific, not specified by spec
|
||||||
span.set_attribute("db.mongo.operation_id", event.operation_id)
|
span.set_attribute("db.mongo.operation_id", event.operation_id)
|
||||||
span.set_attribute("db.mongo.request_id", event.request_id)
|
span.set_attribute("db.mongo.request_id", event.request_id)
|
||||||
|
|
||||||
for attr in COMMAND_ATTRIBUTES:
|
for attr in COMMAND_ATTRIBUTES:
|
||||||
_attr = event.command.get(attr)
|
_attr = event.command.get(attr)
|
||||||
if _attr is not None:
|
if _attr is not None:
|
||||||
span.set_attribute("db.mongo." + attr, str(_attr))
|
span.set_attribute("db.mongo." + attr, str(_attr))
|
||||||
|
|
||||||
# Add Span to dictionary
|
# Add Span to dictionary
|
||||||
self._span_dict[_get_span_dict_key(event)] = span
|
self._span_dict[_get_span_dict_key(event)] = span
|
||||||
except Exception as ex: # noqa pylint: disable=broad-except
|
except Exception as ex: # noqa pylint: disable=broad-except
|
||||||
if span is not None:
|
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()
|
span.end()
|
||||||
self._pop_span(event)
|
self._pop_span(event)
|
||||||
|
|
||||||
@ -103,8 +107,11 @@ class CommandTracer(monitoring.CommandListener):
|
|||||||
span = self._pop_span(event)
|
span = self._pop_span(event)
|
||||||
if span is None:
|
if span is None:
|
||||||
return
|
return
|
||||||
span.set_attribute("db.mongo.duration_micros", event.duration_micros)
|
if span.is_recording():
|
||||||
span.set_status(Status(StatusCanonicalCode.OK, event.reply))
|
span.set_attribute(
|
||||||
|
"db.mongo.duration_micros", event.duration_micros
|
||||||
|
)
|
||||||
|
span.set_status(Status(StatusCanonicalCode.OK, event.reply))
|
||||||
span.end()
|
span.end()
|
||||||
|
|
||||||
def failed(self, event: monitoring.CommandFailedEvent):
|
def failed(self, event: monitoring.CommandFailedEvent):
|
||||||
@ -114,8 +121,11 @@ class CommandTracer(monitoring.CommandListener):
|
|||||||
span = self._pop_span(event)
|
span = self._pop_span(event)
|
||||||
if span is None:
|
if span is None:
|
||||||
return
|
return
|
||||||
span.set_attribute("db.mongo.duration_micros", event.duration_micros)
|
if span.is_recording():
|
||||||
span.set_status(Status(StatusCanonicalCode.UNKNOWN, event.failure))
|
span.set_attribute(
|
||||||
|
"db.mongo.duration_micros", event.duration_micros
|
||||||
|
)
|
||||||
|
span.set_status(Status(StatusCanonicalCode.UNKNOWN, event.failure))
|
||||||
span.end()
|
span.end()
|
||||||
|
|
||||||
def _pop_span(self, event):
|
def _pop_span(self, event):
|
||||||
|
@ -91,6 +91,22 @@ class TestPymongo(TestBase):
|
|||||||
self.assertEqual(span.status.description, "reply")
|
self.assertEqual(span.status.description, "reply")
|
||||||
self.assertIsNotNone(span.end_time)
|
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):
|
def test_failed(self):
|
||||||
mock_event = MockEvent({})
|
mock_event = MockEvent({})
|
||||||
command_tracer = CommandTracer(self.tracer)
|
command_tracer = CommandTracer(self.tracer)
|
||||||
|
Reference in New Issue
Block a user