def test_multiple_commands(self):
        mock_tracer = MockTracer()
        first_mock_event = MockEvent({}, ("firstUrl", "123"), "first")
        second_mock_event = MockEvent({}, ("secondUrl", "456"), "second")
        command_tracer = CommandTracer(mock_tracer)
        command_tracer.started(event=first_mock_event)
        # pylint: disable=protected-access
        first_span = command_tracer._get_span(first_mock_event)
        command_tracer.started(event=second_mock_event)
        # pylint: disable=protected-access
        second_span = command_tracer._get_span(second_mock_event)
        command_tracer.succeeded(event=first_mock_event)
        command_tracer.failed(event=second_mock_event)

        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_started(self):
        command_attrs = {
            "filter": "filter",
            "sort": "sort",
            "limit": "limit",
            "pipeline": "pipeline",
            "command_name": "find",
        }
        mock_tracer = MockTracer()
        command_tracer = CommandTracer(mock_tracer)
        mock_event = MockEvent(command_attrs, ("test.com", "1234"),
                               "test_request_id")
        command_tracer.started(event=mock_event)
        # pylint: disable=protected-access
        span = command_tracer._get_span(mock_event)
        self.assertIs(span.kind, trace_api.SpanKind.CLIENT)
        self.assertEqual(span.name, "mongodb.command_name.find")
        self.assertEqual(span.attributes["component"], "mongodb")
        self.assertEqual(span.attributes["db.type"], "mongodb")
        self.assertEqual(span.attributes["db.instance"], "database_name")
        self.assertEqual(span.attributes["db.statement"], "command_name find")
        self.assertEqual(span.attributes["net.peer.name"], "test.com")
        self.assertEqual(span.attributes["net.peer.port"], "1234")
        self.assertEqual(span.attributes["db.mongo.operation_id"],
                         "operation_id")
        self.assertEqual(span.attributes["db.mongo.request_id"],
                         "test_request_id")

        self.assertEqual(span.attributes["db.mongo.filter"], "filter")
        self.assertEqual(span.attributes["db.mongo.sort"], "sort")
        self.assertEqual(span.attributes["db.mongo.limit"], "limit")
        self.assertEqual(span.attributes["db.mongo.pipeline"], "pipeline")
 def test_succeeded(self):
     mock_tracer = MockTracer()
     mock_event = MockEvent({})
     command_tracer = CommandTracer(mock_tracer)
     command_tracer.started(event=mock_event)
     # pylint: disable=protected-access
     span = command_tracer._get_span(mock_event)
     command_tracer.succeeded(event=mock_event)
     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)