Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 0c7c83d

Browse files
authored
Add support for configure transport in Django middleware (#83)
1 parent 1ec1394 commit 0c7c83d

6 files changed

Lines changed: 76 additions & 11 deletions

File tree

trace/opencensus/trace/ext/django/config.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@
2828
DEFAULT_DJANGO_TRACER_PARAMS = {
2929
# https://cloud.google.com/appengine/docs/flexible/python/
3030
# how-instances-are-managed#health_checking
31-
'BLACKLIST_PATHS': ['/_ah/health'],
31+
'BLACKLIST_PATHS': ['_ah/health'],
3232
'GCP_EXPORTER_PROJECT': None,
3333
'SAMPLING_RATE': 0.5,
3434
'ZIPKIN_EXPORTER_SERVICE_NAME': 'my_service',
3535
'ZIPKIN_EXPORTER_HOST_NAME': 'localhost',
3636
'ZIPKIN_EXPORTER_PORT': 9411,
37-
37+
'TRANSPORT': 'opencensus.trace.exporters.transports.sync.SyncTransport',
3838
}
3939

4040

4141
PATH_DELIMITER = '.'
42+
TRANSPORT = 'TRANSPORT'
4243

4344
log = logging.getLogger(__name__)
4445

@@ -61,6 +62,12 @@ def __init__(self):
6162
'OPENCENSUS_TRACE_PARAMS',
6263
DEFAULT_DJANGO_TRACER_PARAMS)
6364

65+
# Set default value for the tracer config if user not specified
66+
_set_default_configs(self.settings, DEFAULT_DJANGO_TRACER_CONFIG)
67+
68+
# Set default value for the params if user not specified
69+
_set_default_configs(self.params, DEFAULT_DJANGO_TRACER_PARAMS)
70+
6471
def __getattr__(self, attr):
6572
# If not in defaults, it is something we cannot parse.
6673
if attr not in DEFAULT_DJANGO_TRACER_CONFIG:
@@ -74,6 +81,17 @@ def __getattr__(self, attr):
7481
return module_class
7582

7683

84+
def _set_default_configs(user_settings, default):
85+
"""Set the default value to user settings if user not specified
86+
the value.
87+
"""
88+
for key in default:
89+
if key not in user_settings:
90+
user_settings[key] = default[key]
91+
92+
return user_settings
93+
94+
7795
def convert_to_import(path):
7896
"""Given a string which represents the import path, convert to the
7997
class to import.

trace/opencensus/trace/ext/django/middleware.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import logging
1717

1818
from opencensus.trace.ext import utils
19-
from opencensus.trace.ext.django.config import settings
19+
from opencensus.trace.ext.django.config import (settings, convert_to_import)
2020
from opencensus.trace import labels_helper
2121
from opencensus.trace import request_tracer
2222
from opencensus.trace import execution_context
@@ -38,6 +38,7 @@
3838
BLACKLIST_PATHS = 'BLACKLIST_PATHS'
3939
GCP_EXPORTER_PROJECT = 'GCP_EXPORTER_PROJECT'
4040
SAMPLING_RATE = 'SAMPLING_RATE'
41+
TRANSPORT = 'TRANSPORT'
4142
ZIPKIN_EXPORTER_SERVICE_NAME = 'ZIPKIN_EXPORTER_SERVICE_NAME'
4243
ZIPKIN_EXPORTER_HOST_NAME = 'ZIPKIN_EXPORTER_HOST_NAME'
4344
ZIPKIN_EXPORTER_PORT = 'ZIPKIN_EXPORTER_PORT'
@@ -112,9 +113,13 @@ def __init__(self, get_response=None):
112113
self.sampler = self._sampler()
113114

114115
# Initialize the exporter
116+
transport = convert_to_import(settings.params.get(TRANSPORT))
117+
115118
if self._exporter.__name__ == 'GoogleCloudExporter':
116119
_project_id = settings.params.get(GCP_EXPORTER_PROJECT, None)
117-
self.exporter = self._exporter(project_id=_project_id)
120+
self.exporter = self._exporter(
121+
project_id=_project_id,
122+
transport=transport)
118123
elif self._exporter.__name__ == 'ZipkinExporter':
119124
_zipkin_service_name = settings.params.get(
120125
ZIPKIN_EXPORTER_SERVICE_NAME, 'my_service')
@@ -125,9 +130,10 @@ def __init__(self, get_response=None):
125130
self.exporter = self._exporter(
126131
service_name=_zipkin_service_name,
127132
host_name=_zipkin_host_name,
128-
port=_zipkin_port)
133+
port=_zipkin_port,
134+
transport=transport)
129135
else:
130-
self.exporter = self._exporter()
136+
self.exporter = self._exporter(transport=transport)
131137

132138
# Initialize the propagator
133139
self.propagator = self._propagator()

trace/tests/system/django/app/settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@
9191

9292
OPENCENSUS_TRACE_PARAMS = {
9393
'SAMPLING_RATE': 0.5,
94-
'BLACKLIST_PATHS': ['_ah/health',]
94+
'BLACKLIST_PATHS': ['_ah/health',],
95+
'TRANSPORT': 'opencensus.trace.exporters.transports.sync.SyncTransport',
9596
}
9697

9798
# Internationalization

trace/tests/system/django/app/views.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@
4040

4141

4242
def home(request):
43-
time.sleep(1)
4443
return render(request, 'home.html')
4544

4645

4746
def greetings(request):
48-
time.sleep(1)
4947
if request.method == 'POST':
5048
form = HelloForm(request.POST)
5149

trace/tests/unit/ext/django/test_config.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,36 @@ def test__getattr___valid(self):
6060
assert isinstance(sampler_class(), AlwaysOnSampler)
6161

6262

63+
class Test__set_default_configs(unittest.TestCase):
64+
65+
def test__set_default_configs(self):
66+
from opencensus.trace.ext.django import config
67+
68+
custom_django_params = {
69+
'SAMPLING_RATE': 0.6,
70+
'BLACKLIST_PATHS': ['_ah/health', ],
71+
'TRANSPORT':
72+
'opencensus.trace.exporters.transports.sync.SyncTransport',
73+
}
74+
75+
params = config._set_default_configs(
76+
custom_django_params,
77+
config.DEFAULT_DJANGO_TRACER_PARAMS)
78+
79+
expected_params = {
80+
'BLACKLIST_PATHS': ['_ah/health', ],
81+
'GCP_EXPORTER_PROJECT': None,
82+
'SAMPLING_RATE': 0.6,
83+
'ZIPKIN_EXPORTER_SERVICE_NAME': 'my_service',
84+
'ZIPKIN_EXPORTER_HOST_NAME': 'localhost',
85+
'ZIPKIN_EXPORTER_PORT': 9411,
86+
'TRANSPORT':
87+
'opencensus.trace.exporters.transports.sync.SyncTransport',
88+
}
89+
90+
self.assertEqual(params, expected_params)
91+
92+
6393
class Test_convert_to_import(unittest.TestCase):
6494

6595
def test_convert_to_import(self):

trace/tests/unit/ext/django/test_middleware.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@ def test_constructor_cloud(self):
3939
from opencensus.trace.ext.django import middleware
4040
from opencensus.trace.samplers import always_on
4141
from opencensus.trace.propagation import google_cloud_format
42+
from opencensus.trace.exporters.transports import sync
4243

4344
class MockCloudExporter(object):
44-
def __init__(self, project_id):
45+
def __init__(self, project_id, transport):
4546
self.project_id = project_id
47+
self.transport = transport
4648

4749
MockCloudExporter.__name__ = 'GoogleCloudExporter'
4850

4951
project_id = 'my_project'
5052
params = {
5153
'GCP_EXPORTER_PROJECT': project_id,
54+
'TRANSPORT':
55+
'opencensus.trace.exporters.transports.sync.SyncTransport',
5256
}
5357

5458
patch_params = mock.patch(
@@ -75,6 +79,7 @@ def __init__(self, project_id):
7579
google_cloud_format.GoogleCloudFormatPropagator)
7680

7781
self.assertEqual(middleware.exporter.project_id, project_id)
82+
self.assertEqual(middleware.exporter.transport, sync.SyncTransport)
7883

7984
def test_constructor_zipkin(self):
8085
from opencensus.trace.ext.django import middleware
@@ -89,6 +94,8 @@ def test_constructor_zipkin(self):
8994
'ZIPKIN_EXPORTER_SERVICE_NAME': service_name,
9095
'ZIPKIN_EXPORTER_HOST_NAME': host_name,
9196
'ZIPKIN_EXPORTER_PORT': port,
97+
'TRANSPORT':
98+
'opencensus.trace.exporters.transports.sync.SyncTransport',
9299
}
93100

94101
patch_zipkin = mock.patch(
@@ -129,6 +136,8 @@ def test_constructor_probability_sampler(self):
129136
rate = 0.8
130137
params = {
131138
'SAMPLING_RATE': 0.8,
139+
'TRANSPORT':
140+
'opencensus.trace.exporters.transports.sync.SyncTransport',
132141
}
133142

134143
patch_sampler = mock.patch(
@@ -210,7 +219,10 @@ def test_blacklist_path(self):
210219
execution_context.clear()
211220

212221
blacklist_paths = ['test_blacklist_path',]
213-
params = {'BLACKLIST_PATHS': ['test_blacklist_path',]}
222+
params = {
223+
'BLACKLIST_PATHS': ['test_blacklist_path',],
224+
'TRANSPORT':
225+
'opencensus.trace.exporters.transports.sync.SyncTransport',}
214226
patch_params = mock.patch(
215227
'opencensus.trace.ext.django.middleware.settings.params',
216228
params)

0 commit comments

Comments
 (0)