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

Commit e77d5b7

Browse files
Liudmila Molkovasongy23
authored andcommitted
Enable ocagent TraceExporter configuration in django and flask (#300)
1 parent 2a7788a commit e77d5b7

9 files changed

Lines changed: 249 additions & 14 deletions

File tree

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ setting in ``settings.py``:
286286
'BLACKLIST_PATHS': ['/_ah/health'],
287287
'GCP_EXPORTER_PROJECT': None,
288288
'SAMPLING_RATE': 0.5,
289-
'ZIPKIN_EXPORTER_SERVICE_NAME': 'my_service',
289+
'SERVICE_NAME': 'my_service',
290290
'ZIPKIN_EXPORTER_HOST_NAME': 'localhost',
291291
'ZIPKIN_EXPORTER_PORT': 9411,
292292
'ZIPKIN_EXPORTER_PROTOCOL': 'http',

docs/trace/usage.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ setting in ``settings.py``:
248248
'BLACKLIST_PATHS': ['/_ah/health'],
249249
'GCP_EXPORTER_PROJECT': None,
250250
'SAMPLING_RATE': 0.5,
251-
'ZIPKIN_EXPORTER_SERVICE_NAME': 'my_service',
251+
'SERVICE_NAME': 'my_service',
252252
'ZIPKIN_EXPORTER_HOST_NAME': 'localhost',
253253
'ZIPKIN_EXPORTER_PORT': 9411,
254254
'ZIPKIN_EXPORTER_PROTOCOL': 'http',

opencensus/trace/exporters/ocagent/trace_exporter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ def __init__(
6969
client=None,
7070
transport=sync.SyncTransport):
7171
self.transport = transport(self)
72-
7372
self.endpoint = DEFAULT_ENDPOINT if endpoint is None else endpoint
7473

7574
if client is None:
@@ -79,6 +78,7 @@ def __init__(
7978
else:
8079
self.client = client
8180

81+
self.service_name = service_name
8282
self.node = common_pb2.Node(
8383
identifier=common_pb2.ProcessIdentifier(
8484
host_name=socket.gethostname() if host_name is None
@@ -91,7 +91,7 @@ def __init__(
9191
language=common_pb2.LibraryInfo.Language.Value('PYTHON'),
9292
version=VERSION
9393
),
94-
service_info=common_pb2.ServiceInfo(name=service_name))
94+
service_info=common_pb2.ServiceInfo(name=self.service_name))
9595

9696
def emit(self, span_datas):
9797
"""

opencensus/trace/ext/django/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
'BLACKLIST_PATHS': ['_ah/health'],
3232
'GCP_EXPORTER_PROJECT': None,
3333
'SAMPLING_RATE': 0.5,
34+
'SERVICE_NAME': 'my_service',
3435
'ZIPKIN_EXPORTER_SERVICE_NAME': 'my_service',
3536
'ZIPKIN_EXPORTER_HOST_NAME': 'localhost',
3637
'ZIPKIN_EXPORTER_PORT': 9411,
3738
'ZIPKIN_EXPORTER_PROTOCOL': 'http',
39+
'OCAGENT_TRACE_EXPORTER_ENDPOINT': None,
3840
'TRANSPORT': 'opencensus.trace.exporters.transports.sync.SyncTransport',
3941
}
4042

opencensus/trace/ext/django/middleware.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@
3838
GCP_EXPORTER_PROJECT = 'GCP_EXPORTER_PROJECT'
3939
SAMPLING_RATE = 'SAMPLING_RATE'
4040
TRANSPORT = 'TRANSPORT'
41+
SERVICE_NAME = 'SERVICE_NAME'
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'
4445
ZIPKIN_EXPORTER_PROTOCOL = 'ZIPKIN_EXPORTER_PROTOCOL'
45-
46+
OCAGENT_TRACE_EXPORTER_ENDPOINT = 'OCAGENT_TRACE_EXPORTER_ENDPOINT'
4647

4748
log = logging.getLogger(__name__)
4849

@@ -121,20 +122,27 @@ def __init__(self, get_response=None):
121122
project_id=_project_id,
122123
transport=transport)
123124
elif self._exporter.__name__ == 'ZipkinExporter':
124-
_zipkin_service_name = settings.params.get(
125-
ZIPKIN_EXPORTER_SERVICE_NAME, 'my_service')
125+
_service_name = self._get_service_name(settings.params)
126126
_zipkin_host_name = settings.params.get(
127127
ZIPKIN_EXPORTER_HOST_NAME, 'localhost')
128128
_zipkin_port = settings.params.get(
129129
ZIPKIN_EXPORTER_PORT, 9411)
130130
_zipkin_protocol = settings.params.get(
131131
ZIPKIN_EXPORTER_PROTOCOL, 'http')
132132
self.exporter = self._exporter(
133-
service_name=_zipkin_service_name,
133+
service_name=_service_name,
134134
host_name=_zipkin_host_name,
135135
port=_zipkin_port,
136136
protocol=_zipkin_protocol,
137137
transport=transport)
138+
elif self._exporter.__name__ == 'TraceExporter':
139+
_service_name = self._get_service_name(settings.params)
140+
_endpoint = settings.params.get(
141+
OCAGENT_TRACE_EXPORTER_ENDPOINT, None)
142+
self.exporter = self._exporter(
143+
service_name=_service_name,
144+
endpoint=_endpoint,
145+
transport=transport)
138146
else:
139147
self.exporter = self._exporter(transport=transport)
140148

@@ -216,3 +224,13 @@ def process_response(self, request, response):
216224
log.error('Failed to trace request', exc_info=True)
217225
finally:
218226
return response
227+
228+
def _get_service_name(self, params):
229+
_service_name = params.get(
230+
SERVICE_NAME, None)
231+
232+
if _service_name is None:
233+
_service_name = params.get(
234+
ZIPKIN_EXPORTER_SERVICE_NAME, 'my_service')
235+
236+
return _service_name

opencensus/trace/ext/flask/flask_middleware.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@
3939
GCP_EXPORTER_PROJECT = 'GCP_EXPORTER_PROJECT'
4040
SAMPLING_RATE = 'SAMPLING_RATE'
4141
TRANSPORT = 'TRANSPORT'
42+
SERVICE_NAME = 'SERVICE_NAME'
4243
ZIPKIN_EXPORTER_SERVICE_NAME = 'ZIPKIN_EXPORTER_SERVICE_NAME'
4344
ZIPKIN_EXPORTER_HOST_NAME = 'ZIPKIN_EXPORTER_HOST_NAME'
4445
ZIPKIN_EXPORTER_PORT = 'ZIPKIN_EXPORTER_PORT'
4546
ZIPKIN_EXPORTER_PROTOCOL = 'ZIPKIN_EXPORTER_PROTOCOL'
47+
OCAGENT_TRACE_EXPORTER_ENDPOINT = 'OCAGENT_TRACE_EXPORTER_ENDPOINT'
4648

4749
log = logging.getLogger(__name__)
4850

@@ -79,6 +81,7 @@ class FlaskMiddleware(object):
7981
:class:`.TextFormatPropagator` and
8082
:class:`.TraceContextPropagator`.
8183
"""
84+
8285
def __init__(self, app=None, blacklist_paths=None, sampler=None,
8386
exporter=None, propagator=None):
8487
self.app = app
@@ -133,20 +136,27 @@ def init_app(self, app):
133136
project_id=_project_id,
134137
transport=transport)
135138
elif self.exporter.__name__ == 'ZipkinExporter':
136-
_zipkin_service_name = params.get(
137-
ZIPKIN_EXPORTER_SERVICE_NAME, 'my_service')
139+
_service_name = self._get_service_name(params)
138140
_zipkin_host_name = params.get(
139141
ZIPKIN_EXPORTER_HOST_NAME, 'localhost')
140142
_zipkin_port = params.get(
141143
ZIPKIN_EXPORTER_PORT, 9411)
142144
_zipkin_protocol = params.get(
143145
ZIPKIN_EXPORTER_PROTOCOL, 'http')
144146
self.exporter = self.exporter(
145-
service_name=_zipkin_service_name,
147+
service_name=_service_name,
146148
host_name=_zipkin_host_name,
147149
port=_zipkin_port,
148150
protocol=_zipkin_protocol,
149151
transport=transport)
152+
elif self.exporter.__name__ == 'TraceExporter':
153+
_service_name = self._get_service_name(params)
154+
_endpoint = params.get(
155+
OCAGENT_TRACE_EXPORTER_ENDPOINT, None)
156+
self.exporter = self.exporter(
157+
service_name=_service_name,
158+
endpoint=_endpoint,
159+
transport=transport)
150160
else:
151161
self.exporter = self.exporter(transport=transport)
152162

@@ -237,3 +247,13 @@ def _teardown_request(self, exception):
237247
tracer.finish()
238248
except Exception: # pragma: NO COVER
239249
log.error('Failed to trace request', exc_info=True)
250+
251+
def _get_service_name(self, params):
252+
_service_name = params.get(
253+
SERVICE_NAME, None)
254+
255+
if _service_name is None:
256+
_service_name = params.get(
257+
ZIPKIN_EXPORTER_SERVICE_NAME, 'my_service')
258+
259+
return _service_name

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ def test__set_default_configs(self):
8080
'BLACKLIST_PATHS': ['_ah/health', ],
8181
'GCP_EXPORTER_PROJECT': None,
8282
'SAMPLING_RATE': 0.6,
83+
'SERVICE_NAME': 'my_service',
8384
'ZIPKIN_EXPORTER_SERVICE_NAME': 'my_service',
8485
'ZIPKIN_EXPORTER_HOST_NAME': 'localhost',
8586
'ZIPKIN_EXPORTER_PORT': 9411,
8687
'ZIPKIN_EXPORTER_PROTOCOL': 'http',
88+
'OCAGENT_TRACE_EXPORTER_ENDPOINT': None,
8789
'TRANSPORT':
8890
'opencensus.trace.exporters.transports.sync.SyncTransport',
8991
}

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from opencensus.trace import span as span_module
2323
from opencensus.trace.exporters import print_exporter
2424
from opencensus.trace.exporters import zipkin_exporter
25+
from opencensus.trace.exporters.ocagent import trace_exporter
2526
from opencensus.trace.exporters.transports import sync
2627
from opencensus.trace.ext import utils
2728
from opencensus.trace.propagation import google_cloud_format
@@ -132,6 +133,102 @@ def test_constructor_zipkin(self):
132133
self.assertEqual(middleware.exporter.host_name, host_name)
133134
self.assertEqual(middleware.exporter.port, port)
134135

136+
def test_constructor_zipkin_service_name_param(self):
137+
from opencensus.trace.ext.django import middleware
138+
139+
service_name = 'test_service'
140+
host_name = 'test_hostname'
141+
port = 2333
142+
protocol = 'http'
143+
params = {
144+
'SERVICE_NAME': service_name,
145+
'ZIPKIN_EXPORTER_HOST_NAME': host_name,
146+
'ZIPKIN_EXPORTER_PORT': port,
147+
'ZIPKIN_EXPORTER_PROTOCOL': protocol,
148+
'TRANSPORT':
149+
'opencensus.trace.exporters.transports.sync.SyncTransport',
150+
}
151+
152+
patch_zipkin = mock.patch(
153+
'opencensus.trace.ext.django.config.settings.EXPORTER',
154+
zipkin_exporter.ZipkinExporter)
155+
156+
patch_params = mock.patch(
157+
'opencensus.trace.ext.django.config.settings.params',
158+
params)
159+
160+
with patch_zipkin, patch_params:
161+
middleware = middleware.OpencensusMiddleware()
162+
163+
self.assertEqual(middleware.exporter.service_name, service_name)
164+
self.assertEqual(middleware.exporter.host_name, host_name)
165+
self.assertEqual(middleware.exporter.port, port)
166+
167+
def test_constructor_ocagent_trace_exporter(self):
168+
from opencensus.trace.ext.django import middleware
169+
170+
service_name = 'test_service'
171+
endpoint = 'localhost:50001'
172+
params = {
173+
'SERVICE_NAME': service_name,
174+
'OCAGENT_TRACE_EXPORTER_ENDPOINT': endpoint,
175+
'TRANSPORT':
176+
'opencensus.trace.exporters.transports.sync.SyncTransport',
177+
}
178+
179+
patch_ocagent_trace = mock.patch(
180+
'opencensus.trace.ext.django.config.settings.EXPORTER',
181+
trace_exporter.TraceExporter)
182+
183+
patch_params = mock.patch(
184+
'opencensus.trace.ext.django.config.settings.params',
185+
params)
186+
187+
with patch_ocagent_trace, patch_params:
188+
middleware = middleware.OpencensusMiddleware()
189+
190+
self.assertIs(middleware._sampler, always_on.AlwaysOnSampler)
191+
self.assertIs(
192+
middleware._exporter, trace_exporter.TraceExporter)
193+
self.assertIs(
194+
middleware._propagator,
195+
google_cloud_format.GoogleCloudFormatPropagator)
196+
197+
assert isinstance(middleware.sampler, always_on.AlwaysOnSampler)
198+
assert isinstance(
199+
middleware.exporter, trace_exporter.TraceExporter)
200+
assert isinstance(
201+
middleware.propagator,
202+
google_cloud_format.GoogleCloudFormatPropagator)
203+
204+
self.assertEqual(middleware.exporter.service_name, service_name)
205+
self.assertEqual(middleware.exporter.endpoint, endpoint)
206+
207+
def test_constructor_ocagent_trace_exporter_default_endpoint(self):
208+
from opencensus.trace.ext.django import middleware
209+
210+
service_name = 'test_service'
211+
params = {
212+
'SERVICE_NAME': service_name,
213+
'TRANSPORT':
214+
'opencensus.trace.exporters.transports.sync.SyncTransport',
215+
}
216+
217+
patch_ocagent_trace = mock.patch(
218+
'opencensus.trace.ext.django.config.settings.EXPORTER',
219+
trace_exporter.TraceExporter)
220+
221+
patch_params = mock.patch(
222+
'opencensus.trace.ext.django.config.settings.params',
223+
params)
224+
225+
with patch_ocagent_trace, patch_params:
226+
middleware = middleware.OpencensusMiddleware()
227+
228+
self.assertEqual(middleware.exporter.service_name, service_name)
229+
self.assertEqual(middleware.exporter.endpoint,
230+
trace_exporter.DEFAULT_ENDPOINT)
231+
135232
def test_constructor_probability_sampler(self):
136233
from opencensus.trace.ext.django import middleware
137234

0 commit comments

Comments
 (0)