def test_multiple_commands(self): first_mock_event = MockEvent({}, ("firstUrl", "123"), "first") second_mock_event = MockEvent({}, ("secondUrl", "456"), "second") command_tracer = CommandTracer(self.tracer) command_tracer.started(event=first_mock_event) command_tracer.started(event=second_mock_event) command_tracer.succeeded(event=first_mock_event) command_tracer.failed(event=second_mock_event) spans_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans_list), 2) first_span = spans_list[0] second_span = spans_list[1] self.assertEqual(first_span.attributes["db.mongo.request_id"], "first") self.assertIs( first_span.status.canonical_code, trace_api.status.StatusCanonicalCode.OK, ) self.assertEqual(second_span.attributes["db.mongo.request_id"], "second") self.assertIs( second_span.status.canonical_code, trace_api.status.StatusCanonicalCode.UNKNOWN, )
def test_succeeded(self): mock_event = MockEvent({}) command_tracer = CommandTracer(self.tracer) command_tracer.started(event=mock_event) command_tracer.succeeded(event=mock_event) spans_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans_list), 1) span = spans_list[0] self.assertIs(span.status.status_code, trace_api.StatusCode.UNSET) 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_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_succeeded(self): mock_event = MockEvent({}) command_tracer = CommandTracer(self.tracer) command_tracer.started(event=mock_event) command_tracer.succeeded(event=mock_event) spans_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans_list), 1) span = spans_list[0] self.assertEqual(span.attributes["db.mongo.duration_micros"], "duration_micros") self.assertIs(span.status.canonical_code, trace_api.status.StatusCanonicalCode.OK) self.assertEqual(span.status.description, "reply") self.assertIsNotNone(span.end_time)
def test_int_command(self): command_attrs = { "command_name": 123, } mock_event = MockEvent(command_attrs) command_tracer = CommandTracer(self.tracer) command_tracer.started(event=mock_event) command_tracer.succeeded(event=mock_event) spans_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans_list), 1) span = spans_list[0] self.assertEqual(span.name, "command_name.123")
def test_multiple_commands(self): first_mock_event = MockEvent({}, ("firstUrl", "123"), "first") second_mock_event = MockEvent({}, ("secondUrl", "456"), "second") command_tracer = CommandTracer(self.tracer) command_tracer.started(event=first_mock_event) command_tracer.started(event=second_mock_event) command_tracer.succeeded(event=first_mock_event) command_tracer.failed(event=second_mock_event) spans_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(spans_list), 2) first_span = spans_list[0] second_span = spans_list[1] self.assertIs( first_span.status.status_code, trace_api.status.StatusCode.UNSET, ) self.assertIs( second_span.status.status_code, trace_api.status.StatusCode.ERROR, )
def test_suppression_key(self): mock_tracer = mock.Mock() mock_span = mock.Mock() mock_span.is_recording.return_value = True mock_tracer.start_span.return_value = mock_span mock_event = MockEvent({}) mock_event.command.get = mock.Mock() mock_event.command.get.return_value = "dummy" token = context.attach( context.set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)) try: command_tracer = CommandTracer(mock_tracer) command_tracer.started(event=mock_event) command_tracer.succeeded(event=mock_event) finally: context.detach(token) # if suppression key is set, CommandTracer methods return immediately, so command.get is not invoked. self.assertFalse(mock_event.command.get.called # pylint: disable=no-member )