Skip to content

Commit b646b04

Browse files
author
Emanuele Palazzetti
committed
[pyramid] add DATADOG_PYRAMID_DISTRIBUTED_TRACING env var
1 parent a697ea9 commit b646b04

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

ddtrace/contrib/pyramid/patch.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
22

33
from .trace import trace_pyramid, DD_TWEEN_NAME
4-
from .constants import SETTINGS_SERVICE
4+
from .constants import SETTINGS_SERVICE, SETTINGS_DISTRIBUTED_TRACING
5+
from ...util import asbool
56

67
import pyramid.config
78
from pyramid.path import caller_package
@@ -24,8 +25,10 @@ def patch():
2425
def traced_init(wrapped, instance, args, kwargs):
2526
settings = kwargs.pop('settings', {})
2627
service = os.environ.get('DATADOG_SERVICE_NAME') or 'pyramid'
28+
distributed_tracing = asbool(os.environ.get('DATADOG_PYRAMID_DISTRIBUTED_TRACING')) or False
2729
trace_settings = {
28-
SETTINGS_SERVICE : service,
30+
SETTINGS_SERVICE: service,
31+
SETTINGS_DISTRIBUTED_TRACING: distributed_tracing,
2932
}
3033
settings.update(trace_settings)
3134
# If the tweens are explicitly set with 'pyramid.tweens', we need to

tests/contrib/pyramid/test_pyramid_autopatch.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from ...test_tracer import get_dummy_tracer
1414
from ...util import override_global_tracer
1515

16-
from .test_pyramid import PyramidTestCase
16+
from .test_pyramid import PyramidTestCase, PyramidBase
1717

1818

1919
class TestPyramidAutopatch(PyramidTestCase):
@@ -29,6 +29,28 @@ def get_settings(self):
2929
}
3030

3131

32+
class TestPyramidDistributedTracing(PyramidBase):
33+
instrument = False
34+
35+
def test_distributed_tracing(self):
36+
# ensure the Context is properly created
37+
# if distributed tracing is enabled
38+
headers = {
39+
'x-datadog-trace-id': '100',
40+
'x-datadog-parent-id': '42',
41+
'x-datadog-sampling-priority': '2',
42+
}
43+
res = self.app.get('/', headers=headers, status=200)
44+
writer = self.tracer.writer
45+
spans = writer.pop()
46+
eq_(len(spans), 1)
47+
# check the propagated Context
48+
span = spans[0]
49+
eq_(span.trace_id, 100)
50+
eq_(span.parent_id, 42)
51+
eq_(span.get_metric('_sampling_priority_v1'), 2)
52+
53+
3254
def _include_me(config):
3355
pass
3456

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ setenv =
322322
[pyramid_autopatch]
323323
setenv =
324324
DATADOG_SERVICE_NAME = foobar
325+
DATADOG_PYRAMID_DISTRIBUTED_TRACING = True
325326

326327
[testenv:py27-pyramid-autopatch17-webtest]
327328
setenv =

0 commit comments

Comments
 (0)