Example #1
0
    def test_started(self):
        mock_tracer = MockTracer()

        patch = mock.patch(
            'opencensus.trace.execution_context.get_opencensus_tracer',
            return_value=mock_tracer)

        command_attrs = {
            'filter': 'filter',
            'sort': 'sort',
            'limit': 'limit',
            'pipeline': 'pipeline',
            'command_name': 'find'
        }

        expected_attrs = {
            'filter': 'filter',
            'sort': 'sort',
            'limit': 'limit',
            'pipeline': 'pipeline',
            'request_id': 'request_id',
            'connection_id': 'connection_id'
        }

        expected_name = 'pymongo.database_name.find.command_name'

        with patch:
            trace.MongoCommandListener().started(
                event=MockEvent(command_attrs))

        self.assertEqual(mock_tracer.current_span.attributes, expected_attrs)
        self.assertEqual(mock_tracer.current_span.name, expected_name)
Example #2
0
    def test_failed(self):
        mock_tracer = MockTracer()
        mock_tracer.start_span()

        patch = mock.patch(
            'opencensus.trace.execution_context.get_opencensus_tracer',
            return_value=mock_tracer)

        expected_attrs = {'status': 'failed'}

        with patch:
            trace.MongoCommandListener().failed(event=MockEvent(None))

        self.assertEqual(mock_tracer.current_span.attributes, expected_attrs)
        mock_tracer.end_span.assert_called_with()
    def test_succeed(self):
        mock_tracer = MockTracer()
        mock_tracer.start_span()

        patch = mock.patch(
            'opencensus.trace.execution_context.get_opencensus_tracer',
            return_value=mock_tracer)

        expected_status = {'code': 0, 'message': '', 'details': None}

        with patch:
            trace.MongoCommandListener().succeeded(event=MockEvent(None))

        self.assertEqual(mock_tracer.span.status, expected_status)
        mock_tracer.end_span.assert_called_with()