Esempio n. 1
0
    def test_handles_noop(self):
        trace.trace_integration()

        def assert_noop():
            _tracer = execution_context.get_opencensus_tracer()
            self.assertTrue(isinstance(_tracer, NoopTracer))
            return _tracer.span_context.trace_id

        pool = ThreadPoolExecutor(max_workers=1)
        with execution_context.get_opencensus_tracer().span(name='span1'):
            future = pool.submit(assert_noop)
            future.result()
Esempio n. 2
0
    def test_wrap_pool(self):
        _tracer = tracer.Tracer()
        execution_context.set_opencensus_tracer(tracer)

        trace.trace_integration()
        context = tracer.Tracer().span_context
        print(context.trace_id)

        pool = Pool(processes=1)
        with _tracer.span(name='span1'):
            result = pool.apply_async(fake_pooled_func, ()).get(timeout=1)

        self.assertEqual(result, context.trace_id)
Esempio n. 3
0
    def test_wrap_threading(self):
        global global_tracer
        mock_span = mock.Mock()
        span_id = '1234'
        mock_span.span_id = span_id
        mock_tracer = MockTracer(mock_span)
        execution_context.set_opencensus_tracer(mock_tracer)

        trace.trace_integration()

        t = threading.Thread(target=self.fake_threaded_func)
        t.start()
        t.join()
        assert isinstance(global_tracer, MockTracer)
Esempio n. 4
0
    def test_wrap_futures(self):
        _tracer = tracer.Tracer()
        execution_context.set_opencensus_tracer(tracer)

        trace.trace_integration()
        context = tracer.Tracer().span_context
        print(context.trace_id)

        pool = ThreadPoolExecutor(max_workers=1)
        with _tracer.span(name='span1'):
            future = pool.submit(fake_pooled_func)
            result = future.result()

        self.assertEqual(result, context.trace_id)
Esempio n. 5
0
    def test_trace_integration(self):
        mock_wrap_start = mock.Mock()
        mock_wrap_run = mock.Mock()
        mock_wrap_apply_async = mock.Mock()

        mock_threading = mock.Mock()
        mock_pool = mock.Mock()

        wrap_start_result = 'wrap start result'
        wrap_run_result = 'wrap run result'
        wrap_apply_async_result = 'wrap apply_async result'
        mock_wrap_start.return_value = wrap_start_result
        mock_wrap_run.return_value = wrap_run_result
        mock_wrap_apply_async.return_value = wrap_apply_async_result

        mock_start_func = mock.Mock()
        mock_run_func = mock.Mock()
        mock_apply_async_func = mock.Mock()
        mock_start_func.__name__ = 'start'
        mock_run_func.__name__ = 'run'
        mock_apply_async_func.__name__ = 'apply_async'
        setattr(mock_threading.Thread, 'start', mock_start_func)
        setattr(mock_threading.Thread, 'run', mock_run_func)
        setattr(mock_pool.Pool, 'apply_async', mock_apply_async_func)

        patch_wrap_start = mock.patch(
            'opencensus.ext.threading.trace.wrap_threading_start',
            mock_wrap_start)
        patch_wrap_run = mock.patch(
            'opencensus.ext.threading.trace.wrap_threading_run',
            mock_wrap_run)
        patch_wrap_apply_async = mock.patch(
            'opencensus.ext.threading.trace.wrap_apply_async',
            mock_wrap_apply_async)
        patch_threading = mock.patch(
            'opencensus.ext.threading.trace.threading', mock_threading)
        patch_pool = mock.patch('opencensus.ext.threading.trace.pool',
                                mock_pool)

        with patch_wrap_start, patch_wrap_run, patch_wrap_apply_async, \
                patch_threading, patch_pool:
            trace.trace_integration()

        self.assertEqual(
            getattr(mock_threading.Thread, 'start'), wrap_start_result)
        self.assertEqual(
            getattr(mock_threading.Thread, 'run'), wrap_run_result)
        self.assertEqual(
            getattr(mock_pool.Pool, 'apply_async'), wrap_apply_async_result)