Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions ddtrace/contrib/pyramid/patch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os

from .trace import trace_pyramid, DD_TWEEN_NAME
from .constants import SETTINGS_SERVICE
from .constants import SETTINGS_SERVICE, SETTINGS_DISTRIBUTED_TRACING
from ...util import asbool

import pyramid.config
from pyramid.path import caller_package
Expand All @@ -24,8 +25,10 @@ def patch():
def traced_init(wrapped, instance, args, kwargs):
settings = kwargs.pop('settings', {})
service = os.environ.get('DATADOG_SERVICE_NAME') or 'pyramid'
distributed_tracing = asbool(os.environ.get('DATADOG_PYRAMID_DISTRIBUTED_TRACING')) or False
trace_settings = {
SETTINGS_SERVICE : service,
SETTINGS_SERVICE: service,
SETTINGS_DISTRIBUTED_TRACING: distributed_tracing,
}
settings.update(trace_settings)
# If the tweens are explicitly set with 'pyramid.tweens', we need to
Expand Down
12 changes: 12 additions & 0 deletions ddtrace/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ def _get_original_method(thing, key):
setattr(patchable, key, dest.__get__(patchable, patchable.__class__))


def asbool(value):
"""Convert the given String to a boolean object. Accepted
values are `True` and `1`."""
if value is None:
return False

if isinstance(value, bool):
return value

return value.lower() in ("true", "1")


def unwrap(obj, attr):
f = getattr(obj, attr, None)
if f and isinstance(f, wrapt.ObjectProxy) and hasattr(f, '__wrapped__'):
Expand Down
24 changes: 23 additions & 1 deletion tests/contrib/pyramid/test_pyramid_autopatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ...test_tracer import get_dummy_tracer
from ...util import override_global_tracer

from .test_pyramid import PyramidTestCase
from .test_pyramid import PyramidTestCase, PyramidBase


class TestPyramidAutopatch(PyramidTestCase):
Expand All @@ -29,6 +29,28 @@ def get_settings(self):
}


class TestPyramidDistributedTracing(PyramidBase):
instrument = False

def test_distributed_tracing(self):
# ensure the Context is properly created
# if distributed tracing is enabled
headers = {
'x-datadog-trace-id': '100',
'x-datadog-parent-id': '42',
'x-datadog-sampling-priority': '2',
}
res = self.app.get('/', headers=headers, status=200)
writer = self.tracer.writer
spans = writer.pop()
eq_(len(spans), 1)
# check the propagated Context
span = spans[0]
eq_(span.trace_id, 100)
eq_(span.parent_id, 42)
eq_(span.get_metric('_sampling_priority_v1'), 2)


def _include_me(config):
pass

Expand Down
1 change: 1 addition & 0 deletions tests/contrib/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from nose.tools import eq_

from ddtrace.contrib.util import func_name
from ddtrace.util import asbool
from functools import partial

class SomethingCallable(object):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest

from nose.tools import eq_

from ddtrace.util import asbool


class TestUtilities(unittest.TestCase):
def test_asbool(self):
# ensure the value is properly cast
eq_(asbool("True"), True)
eq_(asbool("true"), True)
eq_(asbool("1"), True)
eq_(asbool("False"), False)
eq_(asbool("false"), False)
eq_(asbool(None), False)
eq_(asbool(""), False)
eq_(asbool(True), True)
eq_(asbool(False), False)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ setenv =
[pyramid_autopatch]
setenv =
DATADOG_SERVICE_NAME = foobar
DATADOG_PYRAMID_DISTRIBUTED_TRACING = True

[testenv:py27-pyramid-autopatch17-webtest]
setenv =
Expand Down