Esempio n. 1
0
    def test_process_request(self):
        from opencensus.trace.ext.django import middleware

        trace_id = '2dd43a1d6b2549c6bc2a1a54c2fc0b05'
        span_id = '6e0c63257de34c92'
        django_trace_id = '{}/{}'.format(trace_id, span_id)

        django_request = RequestFactory().get('/', **{
            'HTTP_X_CLOUD_TRACE_CONTEXT': django_trace_id})

        middleware_obj = middleware.OpencensusMiddleware()

        # test process_request
        middleware_obj.process_request(django_request)

        tracer = middleware._get_current_tracer()

        span = tracer.current_span()

        expected_attributes = {
            'http.url': u'/',
            'http.method': 'GET',
        }
        self.assertEqual(span.span_kind, span_module.SpanKind.SERVER)
        self.assertEqual(span.attributes, expected_attributes)
        self.assertEqual(span.parent_span.span_id, span_id)

        span_context = tracer.span_context
        self.assertEqual(span_context.trace_id, trace_id)

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

        self.assertEqual(span.name, 'mock.mock.Mock')
    def test_constructor_fixed_rate_sampler(self):
        from opencensus.trace.ext.django import middleware
        from opencensus.trace.samplers import fixed_rate
        from opencensus.trace.reporters import print_reporter
        from opencensus.trace.propagation import google_cloud_format

        rate = 0.8
        params = {
            'SAMPLING_RATE': 0.8,
        }

        patch_sampler = mock.patch(
            'opencensus.trace.ext.django.config.settings.SAMPLER',
            fixed_rate.FixedRateSampler)
        patch_reporter = mock.patch(
            'opencensus.trace.ext.django.config.settings.REPORTER',
            print_reporter.PrintReporter)

        patch_params = mock.patch(
            'opencensus.trace.ext.django.config.settings.params', params)

        with patch_sampler, patch_reporter, patch_params:
            middleware = middleware.OpencensusMiddleware()

        self.assertIs(middleware._sampler, fixed_rate.FixedRateSampler)
        self.assertIs(middleware._reporter, print_reporter.PrintReporter)
        self.assertIs(middleware._propagator,
                      google_cloud_format.GoogleCloudFormatPropagator)

        assert isinstance(middleware.sampler, fixed_rate.FixedRateSampler)
        assert isinstance(middleware.reporter, print_reporter.PrintReporter)
        assert isinstance(middleware.propagator,
                          google_cloud_format.GoogleCloudFormatPropagator)

        self.assertEqual(middleware.sampler.rate, rate)
Esempio n. 3
0
    def test_constructor_zipkin_service_name_param(self):
        from opencensus.trace.ext.django import middleware

        service_name = 'test_service'
        host_name = 'test_hostname'
        port = 2333
        protocol = 'http'
        params = {
            'SERVICE_NAME': service_name,
            'ZIPKIN_EXPORTER_HOST_NAME': host_name,
            'ZIPKIN_EXPORTER_PORT': port,
            'ZIPKIN_EXPORTER_PROTOCOL': protocol,
            'TRANSPORT':
                'opencensus.trace.exporters.transports.sync.SyncTransport',
        }

        patch_zipkin = mock.patch(
            'opencensus.trace.ext.django.config.settings.EXPORTER',
            zipkin_exporter.ZipkinExporter)

        patch_params = mock.patch(
            'opencensus.trace.ext.django.config.settings.params',
            params)

        with patch_zipkin, patch_params:
            middleware = middleware.OpencensusMiddleware()

        self.assertEqual(middleware.exporter.service_name, service_name)
        self.assertEqual(middleware.exporter.host_name, host_name)
        self.assertEqual(middleware.exporter.port, port)
Esempio n. 4
0
    def test_constructor_probability_sampler(self):
        from opencensus.trace.ext.django import middleware

        rate = 0.8
        params = {
            'SAMPLING_RATE':
            0.8,
            'TRANSPORT':
            'opencensus.trace.exporters.transports.sync.SyncTransport',
        }

        patch_sampler = mock.patch(
            'opencensus.trace.ext.django.config.settings.SAMPLER',
            probability.ProbabilitySampler)
        patch_exporter = mock.patch(
            'opencensus.trace.ext.django.config.settings.EXPORTER',
            print_exporter.PrintExporter)

        patch_params = mock.patch(
            'opencensus.trace.ext.django.config.settings.params', params)

        with patch_sampler, patch_exporter, patch_params:
            middleware = middleware.OpencensusMiddleware()

        self.assertIs(middleware._sampler, probability.ProbabilitySampler)
        self.assertIs(middleware._exporter, print_exporter.PrintExporter)
        self.assertIs(middleware._propagator,
                      google_cloud_format.GoogleCloudFormatPropagator)

        assert isinstance(middleware.sampler, probability.ProbabilitySampler)
        assert isinstance(middleware.exporter, print_exporter.PrintExporter)
        assert isinstance(middleware.propagator,
                          google_cloud_format.GoogleCloudFormatPropagator)

        self.assertEqual(middleware.sampler.rate, rate)
    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)
Esempio n. 6
0
    def test_constructor_zipkin(self):
        from opencensus.trace.ext.django import middleware

        service_name = 'test_service'
        host_name = 'test_hostname'
        port = 2333
        protocol = 'http'
        params = {
            'ZIPKIN_EXPORTER_SERVICE_NAME': service_name,
            'ZIPKIN_EXPORTER_HOST_NAME': host_name,
            'ZIPKIN_EXPORTER_PORT': port,
            'ZIPKIN_EXPORTER_PROTOCOL': protocol,
            'TRANSPORT':
                'opencensus.trace.exporters.transports.sync.SyncTransport',
        }

        patch_zipkin = mock.patch(
            'opencensus.trace.ext.django.config.settings.EXPORTER',
            zipkin_exporter.ZipkinExporter)

        patch_params = mock.patch(
            'opencensus.trace.ext.django.config.settings.params',
            params)

        with patch_zipkin, patch_params:
            middleware = middleware.OpencensusMiddleware()

        self.assertIs(middleware._sampler, always_on.AlwaysOnSampler)
        self.assertIs(
            middleware._exporter, zipkin_exporter.ZipkinExporter)
        self.assertIs(
            middleware._propagator,
            google_cloud_format.GoogleCloudFormatPropagator)

        assert isinstance(middleware.sampler, always_on.AlwaysOnSampler)
        assert isinstance(
            middleware.exporter, zipkin_exporter.ZipkinExporter)
        assert isinstance(
            middleware.propagator,
            google_cloud_format.GoogleCloudFormatPropagator)

        self.assertEqual(middleware.exporter.service_name, service_name)
        self.assertEqual(middleware.exporter.host_name, host_name)
        self.assertEqual(middleware.exporter.port, port)
Esempio n. 7
0
    def test_constructor_cloud(self):
        from opencensus.trace.ext.django import middleware

        class MockCloudExporter(object):
            def __init__(self, project_id, transport):
                self.project_id = project_id
                self.transport = transport

        MockCloudExporter.__name__ = 'GoogleCloudExporter'

        project_id = 'my_project'
        params = {
            'GCP_EXPORTER_PROJECT': project_id,
            'TRANSPORT':
                'opencensus.trace.exporters.transports.sync.SyncTransport',
        }

        patch_params = mock.patch(
            'opencensus.trace.ext.django.config.settings.params', params)
        patch_exporter = mock.patch(
            'opencensus.trace.ext.django.config.settings.EXPORTER',
            MockCloudExporter)

        with patch_params, patch_exporter:
            middleware = middleware.OpencensusMiddleware()

        self.assertIs(middleware._sampler, always_on.AlwaysOnSampler)
        self.assertIs(
            middleware._exporter, MockCloudExporter)
        self.assertIs(
            middleware._propagator,
            google_cloud_format.GoogleCloudFormatPropagator)

        assert isinstance(middleware.sampler, always_on.AlwaysOnSampler)
        assert isinstance(
            middleware.exporter, MockCloudExporter)
        assert isinstance(
            middleware.propagator,
            google_cloud_format.GoogleCloudFormatPropagator)

        self.assertEqual(middleware.exporter.project_id, project_id)
        self.assertEqual(middleware.exporter.transport, sync.SyncTransport)
    def test_process_response(self):
        from django.test import RequestFactory

        from opencensus.trace.ext.django import middleware

        trace_id = '2dd43a1d6b2549c6bc2a1a54c2fc0b05'
        span_id = 123
        django_trace_id = '{}/{}'.format(trace_id, span_id)

        django_request = RequestFactory().get(
            '/', **{middleware._DJANGO_TRACE_HEADER: django_trace_id})

        middleware_obj = middleware.OpencensusMiddleware()

        middleware_obj.process_request(django_request)
        tracer = middleware._get_current_request_tracer()
        span = tracer.current_span()

        reporter_mock = mock.Mock()
        tracer.reporter = reporter_mock

        django_response = mock.Mock()
        django_response.status_code = 200

        expected_labels = {
            '/http/url': u'/',
            '/http/method': 'GET',
            '/http/status_code': '200',
            '/django/user/id': '123',
            '/django/user/name': 'test_name'
        }

        mock_user = mock.Mock()
        mock_user.pk = 123
        mock_user.get_username.return_value = 'test_name'
        django_request.user = mock_user

        middleware_obj.process_response(django_request, django_response)

        self.assertEqual(span.labels, expected_labels)
        self.assertTrue(reporter_mock.report.called)
Esempio n. 9
0
    def test_process_response_unfinished_child_span(self):
        from opencensus.trace.ext.django import middleware

        trace_id = '2dd43a1d6b2549c6bc2a1a54c2fc0b05'
        span_id = '6e0c63257de34c92'
        django_trace_id = '{}/{}'.format(trace_id, span_id)

        django_request = RequestFactory().get('/', **{
            google_cloud_format._TRACE_CONTEXT_HEADER_NAME: django_trace_id})

        middleware_obj = middleware.OpencensusMiddleware()

        middleware_obj.process_request(django_request)
        tracer = middleware._get_current_tracer()
        span = tracer.current_span()

        exporter_mock = mock.Mock()
        tracer.exporter = exporter_mock

        django_response = mock.Mock()
        django_response.status_code = 500

        expected_attributes = {
            'http.url': u'/',
            'http.method': 'GET',
            'http.status_code': '500',
            'django.user.id': '123',
            'django.user.name': 'test_name'
        }

        mock_user = mock.Mock()
        mock_user.pk = 123
        mock_user.get_username.return_value = 'test_name'
        django_request.user = mock_user

        tracer.start_span()
        self.assertNotEqual(span, tracer.current_span())
        middleware_obj.process_response(django_request, django_response)

        self.assertEqual(span.attributes, expected_attributes)
Esempio n. 10
0
    def test_constructor_ocagent_trace_exporter(self):
        from opencensus.trace.ext.django import middleware

        service_name = 'test_service'
        endpoint = 'localhost:50001'
        params = {
            'SERVICE_NAME': service_name,
            'OCAGENT_TRACE_EXPORTER_ENDPOINT': endpoint,
            'TRANSPORT':
                'opencensus.trace.exporters.transports.sync.SyncTransport',
        }

        patch_ocagent_trace = mock.patch(
            'opencensus.trace.ext.django.config.settings.EXPORTER',
            trace_exporter.TraceExporter)

        patch_params = mock.patch(
            'opencensus.trace.ext.django.config.settings.params',
            params)

        with patch_ocagent_trace, patch_params:
            middleware = middleware.OpencensusMiddleware()

        self.assertIs(middleware._sampler, always_on.AlwaysOnSampler)
        self.assertIs(
            middleware._exporter, trace_exporter.TraceExporter)
        self.assertIs(
            middleware._propagator,
            google_cloud_format.GoogleCloudFormatPropagator)

        assert isinstance(middleware.sampler, always_on.AlwaysOnSampler)
        assert isinstance(
            middleware.exporter, trace_exporter.TraceExporter)
        assert isinstance(
            middleware.propagator,
            google_cloud_format.GoogleCloudFormatPropagator)

        self.assertEqual(middleware.exporter.service_name, service_name)
        self.assertEqual(middleware.exporter.endpoint, endpoint)
    def test_constructor_zipkin(self):
        from opencensus.trace.ext.django import middleware
        from opencensus.trace.samplers import always_on
        from opencensus.trace.reporters import zipkin_reporter
        from opencensus.trace.propagation import google_cloud_format

        service_name = 'test_service'
        host_name = 'test_hostname'
        port = 2333
        params = {
            'ZIPKIN_REPORTER_SERVICE_NAME': service_name,
            'ZIPKIN_REPORTER_HOST_NAME': host_name,
            'ZIPKIN_REPORTER_PORT': port,
        }

        patch_zipkin = mock.patch(
            'opencensus.trace.ext.django.config.settings.REPORTER',
            zipkin_reporter.ZipkinReporter)

        patch_params = mock.patch(
            'opencensus.trace.ext.django.config.settings.params', params)

        with patch_zipkin, patch_params:
            middleware = middleware.OpencensusMiddleware()

        self.assertIs(middleware._sampler, always_on.AlwaysOnSampler)
        self.assertIs(middleware._reporter, zipkin_reporter.ZipkinReporter)
        self.assertIs(middleware._propagator,
                      google_cloud_format.GoogleCloudFormatPropagator)

        assert isinstance(middleware.sampler, always_on.AlwaysOnSampler)
        assert isinstance(middleware.reporter, zipkin_reporter.ZipkinReporter)
        assert isinstance(middleware.propagator,
                          google_cloud_format.GoogleCloudFormatPropagator)

        self.assertEqual(middleware.reporter.service_name, service_name)
        self.assertEqual(middleware.reporter.host_name, host_name)
        self.assertEqual(middleware.reporter.port, port)
Esempio n. 12
0
    def test_constructor_ocagent_trace_exporter_default_endpoint(self):
        from opencensus.trace.ext.django import middleware

        service_name = 'test_service'
        params = {
            'SERVICE_NAME': service_name,
            'TRANSPORT':
                'opencensus.trace.exporters.transports.sync.SyncTransport',
        }

        patch_ocagent_trace = mock.patch(
            'opencensus.trace.ext.django.config.settings.EXPORTER',
            trace_exporter.TraceExporter)

        patch_params = mock.patch(
            'opencensus.trace.ext.django.config.settings.params',
            params)

        with patch_ocagent_trace, patch_params:
            middleware = middleware.OpencensusMiddleware()

        self.assertEqual(middleware.exporter.service_name, service_name)
        self.assertEqual(middleware.exporter.endpoint,
                         trace_exporter.DEFAULT_ENDPOINT)
Esempio n. 13
0
    def test_constructor_jaeger(self):
        from opencensus.trace.ext.django import middleware

        service_name = 'test_service'
        params = {
            'SERVICE_NAME': service_name,
            'TRANSPORT':
                'opencensus.trace.exporters.transports.sync.SyncTransport',
        }

        patch_jaeger = mock.patch(
            'opencensus.trace.ext.django.config.settings.EXPORTER',
            jaeger_exporter.JaegerExporter)

        patch_params = mock.patch(
            'opencensus.trace.ext.django.config.settings.params',
            params)

        with patch_jaeger, patch_params:
            middleware = middleware.OpencensusMiddleware()

        self.assertIs(middleware._sampler, always_on.AlwaysOnSampler)
        self.assertIs(
            middleware._exporter, jaeger_exporter.JaegerExporter)
        self.assertIs(
            middleware._propagator,
            google_cloud_format.GoogleCloudFormatPropagator)

        assert isinstance(middleware.sampler, always_on.AlwaysOnSampler)
        assert isinstance(
            middleware.exporter, jaeger_exporter.JaegerExporter)
        assert isinstance(
            middleware.propagator,
            google_cloud_format.GoogleCloudFormatPropagator)

        self.assertEqual(middleware.exporter.service_name, service_name)
    def test_process_request(self):
        from django.test import RequestFactory

        from opencensus.trace.ext.django import middleware

        trace_id = '2dd43a1d6b2549c6bc2a1a54c2fc0b05'
        span_id = 123
        django_trace_id = '{}/{}'.format(trace_id, span_id)

        django_request = RequestFactory().get(
            '/', **{middleware._DJANGO_TRACE_HEADER: django_trace_id})

        middleware_obj = middleware.OpencensusMiddleware()

        # test process_request
        middleware_obj.process_request(django_request)

        tracer = middleware._get_current_tracer()

        span = tracer.current_span()

        expected_attributes = {
            '/http/url': u'/',
            '/http/method': 'GET',
        }
        self.assertEqual(span.attributes, expected_attributes)
        self.assertEqual(span.parent_span.span_id, span_id)

        span_context = tracer.span_context
        self.assertEqual(span_context.trace_id, trace_id)

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

        self.assertEqual(span.name, 'mock.mock.Mock')
    def test_constructor_cloud(self):
        from opencensus.trace.ext.django import middleware
        from opencensus.trace.samplers import always_on
        from opencensus.trace.propagation import google_cloud_format

        class MockCloudReporter(object):
            def __init__(self, project_id):
                self.project_id = project_id

        MockCloudReporter.__name__ = 'GoogleCloudReporter'

        project_id = 'my_project'
        params = {
            'GCP_REPORTER_PROJECT': project_id,
        }

        patch_params = mock.patch(
            'opencensus.trace.ext.django.config.settings.params', params)
        patch_reporter = mock.patch(
            'opencensus.trace.ext.django.config.settings.REPORTER',
            MockCloudReporter)

        with patch_params, patch_reporter:
            middleware = middleware.OpencensusMiddleware()

        self.assertIs(middleware._sampler, always_on.AlwaysOnSampler)
        self.assertIs(middleware._reporter, MockCloudReporter)
        self.assertIs(middleware._propagator,
                      google_cloud_format.GoogleCloudFormatPropagator)

        assert isinstance(middleware.sampler, always_on.AlwaysOnSampler)
        assert isinstance(middleware.reporter, MockCloudReporter)
        assert isinstance(middleware.propagator,
                          google_cloud_format.GoogleCloudFormatPropagator)

        self.assertEqual(middleware.reporter.project_id, project_id)