Skip to content

Commit 652629a

Browse files
author
Emanuele Palazzetti
authored
Merge pull request #403 from palazzem/pyramid-distributed-tracing
[pyramid] add distributed tracing
2 parents b71a40e + 2a3b20e commit 652629a

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-7
lines changed

ddtrace/contrib/pyramid/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
# use your config as normal.
1616
config.add_route('index', '/')
1717
18+
Available settings are:
19+
20+
* `datadog_trace_service`: change the `pyramid` service name
21+
* `datadog_trace_enabled`: sets if the Tracer is enabled or not
22+
* `datadog_distributed_tracing`: set it to `True` to enable Distributed Tracing
23+
1824
If you use the 'pyramid.tweens' settings value to set the tweens for your
1925
application, you need to add 'ddtrace.contrib.pyramid:trace_tween_factory'
2026
explicitly to the list. For example::
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
SETTINGS_SERVICE = 'datadog_trace_service'
22
SETTINGS_TRACER = 'datadog_tracer'
33
SETTINGS_TRACE_ENABLED = 'datadog_trace_enabled'
4+
SETTINGS_DISTRIBUTED_TRACING = 'datadog_distributed_tracing'

ddtrace/contrib/pyramid/trace.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@
99
# project
1010
import ddtrace
1111
from ...ext import http, AppTypes
12-
from .constants import SETTINGS_SERVICE, SETTINGS_TRACE_ENABLED, SETTINGS_TRACER
12+
from ...propagation.http import HTTPPropagator
13+
from .constants import (
14+
SETTINGS_TRACER,
15+
SETTINGS_SERVICE,
16+
SETTINGS_TRACE_ENABLED,
17+
SETTINGS_DISTRIBUTED_TRACING,
18+
)
19+
1320

1421
log = logging.getLogger(__name__)
1522

1623
DD_TWEEN_NAME = 'ddtrace.contrib.pyramid:trace_tween_factory'
1724
DD_SPAN = '_datadog_span'
1825

26+
1927
def trace_pyramid(config):
2028
config.include('ddtrace.contrib.pyramid')
2129

@@ -49,6 +57,7 @@ def trace_tween_factory(handler, registry):
4957
service = settings.get(SETTINGS_SERVICE) or 'pyramid'
5058
tracer = settings.get(SETTINGS_TRACER) or ddtrace.tracer
5159
enabled = asbool(settings.get(SETTINGS_TRACE_ENABLED, tracer.enabled))
60+
distributed_tracing = asbool(settings.get(SETTINGS_DISTRIBUTED_TRACING, False))
5261

5362
# set the service info
5463
tracer.set_service_info(
@@ -59,6 +68,12 @@ def trace_tween_factory(handler, registry):
5968
if enabled:
6069
# make a request tracing function
6170
def trace_tween(request):
71+
if distributed_tracing:
72+
propagator = HTTPPropagator()
73+
context = propagator.extract(request.headers)
74+
# only need to active the new context if something was propagated
75+
if context.trace_id:
76+
tracer.context_provider.activate(context)
6277
with tracer.trace('pyramid.request', service=service, resource='404') as span:
6378
setattr(request, DD_SPAN, span) # used to find the tracer in templates
6479
response = None

tests/contrib/pyramid/test_pyramid.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from nose.tools import eq_, assert_raises
55

6-
# project
76
from ddtrace import compat
87
from ddtrace.contrib.pyramid.patch import insert_tween_if_needed
98

@@ -16,6 +15,7 @@
1615

1716

1817
class PyramidBase(object):
18+
"""Base Pyramid test application"""
1919
instrument = False
2020

2121
def setUp(self):
@@ -38,6 +38,9 @@ def get_settings(self):
3838
def override_settings(self, settings):
3939
self.create_app(settings)
4040

41+
class PyramidTestCase(PyramidBase):
42+
"""Pyramid TestCase that includes tests for automatic instrumentation"""
43+
4144
def test_200(self):
4245
res = self.app.get('/', status=200)
4346
assert b'idx' in res.body
@@ -248,7 +251,7 @@ def includeme(config):
248251
pass
249252

250253

251-
class TestPyramid(PyramidBase):
254+
class TestPyramid(PyramidTestCase):
252255
instrument = True
253256

254257
def get_settings(self):
@@ -263,3 +266,30 @@ def test_tween_overridden(self):
263266
self.app.get('/json', status=200)
264267
spans = self.tracer.writer.pop()
265268
eq_(len(spans), 0)
269+
270+
271+
class TestPyramidDistributedTracing(PyramidBase):
272+
instrument = True
273+
274+
def get_settings(self):
275+
return {
276+
'datadog_distributed_tracing': True,
277+
}
278+
279+
def test_distributed_tracing(self):
280+
# ensure the Context is properly created
281+
# if distributed tracing is enabled
282+
headers = {
283+
'x-datadog-trace-id': '100',
284+
'x-datadog-parent-id': '42',
285+
'x-datadog-sampling-priority': '2',
286+
}
287+
res = self.app.get('/', headers=headers, status=200)
288+
writer = self.tracer.writer
289+
spans = writer.pop()
290+
eq_(len(spans), 1)
291+
# check the propagated Context
292+
span = spans[0]
293+
eq_(span.trace_id, 100)
294+
eq_(span.parent_id, 42)
295+
eq_(span.get_metric('_sampling_priority_v1'), 2)

tests/contrib/pyramid/test_pyramid_autopatch.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
from ...test_tracer import get_dummy_tracer
1414
from ...util import override_global_tracer
1515

16-
#from .test_pyramid import PyramidBase, get_app, custom_exception_view
17-
from .test_pyramid import PyramidBase
16+
from .test_pyramid import PyramidTestCase
1817

1918

20-
class TestPyramidAutopatch(PyramidBase):
19+
class TestPyramidAutopatch(PyramidTestCase):
2120
instrument = False
2221

2322

24-
class TestPyramidExplicitTweens(PyramidBase):
23+
class TestPyramidExplicitTweens(PyramidTestCase):
2524
instrument = False
2625

2726
def get_settings(self):

0 commit comments

Comments
 (0)