def tearDown(self):
        from django.test.utils import teardown_test_environment
        from opencensus.trace import execution_context

        execution_context.clear()

        teardown_test_environment()
 def __enter__(self):
     self.orig_tracer = OpenCensusSpan.get_current_tracer()
     self.orig_current_span = OpenCensusSpan.get_current_span()
     execution_context.clear()
     if self.tracer_to_use is not None:
         settings.tracing_implementation.set_value(self.tracer_to_use)
     self.os_env.start()
     execution_context.clear()
     return self
    def test_blacklist_path(self):
        from django.test import RequestFactory

        from opencensus.trace.ext.django import middleware
        from opencensus.trace.tracers import base
        from opencensus.trace.tracers import noop_tracer
        from opencensus.trace.ext import utils
        from opencensus.trace import execution_context

        execution_context.clear()

        blacklist_paths = [
            'test_blacklist_path',
        ]
        params = {
            'BLACKLIST_PATHS': [
                'test_blacklist_path',
            ],
            'TRANSPORT':
            'opencensus.trace.exporters.transports.sync.SyncTransport',
        }
        patch_params = mock.patch(
            'opencensus.trace.ext.django.middleware.settings.params', params)

        with patch_params:
            middleware_obj = middleware.OpencensusMiddleware()

        django_request = RequestFactory().get('/test_blacklist_path')
        disabled = utils.disable_tracing_url(django_request.path,
                                             blacklist_paths)
        self.assertTrue(disabled)
        self.assertEqual(middleware_obj._blacklist_paths, blacklist_paths)

        # test process_request
        middleware_obj.process_request(django_request)

        tracer = middleware._get_current_tracer()
        span = tracer.current_span()

        # process view
        view_func = mock.Mock()
        middleware_obj.process_view(django_request, view_func)

        tracer = middleware._get_current_tracer()
        span = tracer.current_span()

        assert isinstance(span, base.NullContextManager)

        # process response
        django_response = mock.Mock()
        django_response.status_code = 200

        middleware_obj.process_response(django_request, django_response)

        tracer = middleware._get_current_tracer()
        span = tracer.current_span()
        assert isinstance(span, base.NullContextManager)
Beispiel #4
0
    def test_excludelist_path(self):
        from opencensus.ext.django import middleware

        execution_context.clear()

        excludelist_paths = ['test_excludelist_path']
        settings = type('Test', (object,), {})
        settings.OPENCENSUS = {
            'TRACE': {
                'SAMPLER': 'opencensus.trace.samplers.AlwaysOnSampler()',  # noqa
                'EXCLUDELIST_PATHS': excludelist_paths,
                'EXPORTER': mock.Mock(),
            }
        }
        patch_settings = mock.patch(
            'django.conf.settings',
            settings)

        with patch_settings:
            middleware_obj = middleware.OpencensusMiddleware()

        django_request = RequestFactory().get('/test_excludelist_path')
        disabled = utils.disable_tracing_url(django_request.path,
                                             excludelist_paths)
        self.assertTrue(disabled)
        self.assertEqual(middleware_obj.excludelist_paths, excludelist_paths)

        # test process_request
        middleware_obj.process_request(django_request)

        tracer = middleware._get_current_tracer()
        span = tracer.current_span()

        # process view
        view_func = mock.Mock()
        middleware_obj.process_view(django_request, view_func)

        tracer = middleware._get_current_tracer()
        span = tracer.current_span()

        assert isinstance(span, BlankSpan)

        # process response
        django_response = mock.Mock()
        django_response.status_code = 200

        middleware_obj.process_response(django_request, django_response)

        tracer = middleware._get_current_tracer()
        span = tracer.current_span()
        assert isinstance(span, BlankSpan)
def test_get_span_from_thread():

    result = []
    def get_span_from_thread(output):
        current_span = OpenCensusSpan.get_current_span()
        output.append(current_span)

    tracer = Tracer(sampler=AlwaysOnSampler())
    with tracer.span(name="TestSpan") as span:

        thread = threading.Thread(
            target=get_span_from_thread,
            args=(result,)
        )
        thread.start()
        thread.join()

        assert span is result[0]

    execution_context.clear()
 def tearDown(self):
     execution_context.clear()
     teardown_test_environment()
Beispiel #7
0
    def setUp(self):
        from opencensus.trace import execution_context

        execution_context.clear()
    def tearDown(self):
        from opencensus.trace import execution_context

        execution_context.clear()
Beispiel #9
0
 def tearDown(self):
     execution_context.clear()
Beispiel #10
0
 def setUp(self):
     execution_context.clear()