Skip to content

Commit

Permalink
fix for later versions
Browse files Browse the repository at this point in the history
  • Loading branch information
thehesiod committed Apr 4, 2019
1 parent a1fdf15 commit 3982991
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 13 deletions.
10 changes: 6 additions & 4 deletions ddtrace/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ def get(self):
# clean the current state
self._trace = []
self._finished_spans = 0
self._parent_trace_id = None
self._parent_span_id = None
self._sampling_priority = None
self._sampled = True

# Don't clear out the parent trace IDs as this may be a cloned
# context in a new thread/task without a outer span
if not self._parent_trace_id:
self._sampling_priority = None
self._sampled = True
return trace, sampled
else:
return None, None
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/contrib/aiohttp/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def __init__(self, obj, pin, trace_headers):
self._self_parent_span_id = parent_span.span_id
else:
self._self_parent_trace_id, self._self_parent_span_id = \
ctx._get_parent_span_ids()
ctx.trace_id, ctx.span_id

self._self_trace_headers = trace_headers

Expand Down
8 changes: 1 addition & 7 deletions ddtrace/contrib/asyncio/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,7 @@ def _wrapped_create_task(wrapped, instance, args, kwargs):
ctx = getattr(current_task, CONTEXT_ATTR, None)
if ctx:
# current task has a context, so parent a new context to the base context
new_ctx = Context(
trace_id=ctx.trace_id,
span_id=ctx.span_id,
sampled=ctx.is_sampled(),
sampling_priority=ctx.sampling_priority,
service=ctx.service
)
new_ctx = ctx.clone()
set_call_context(new_task, new_ctx)

return new_task
90 changes: 90 additions & 0 deletions tests/contrib/aiohttp/test_35.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import asyncio

from nose.tools import eq_
import aiohttp
from aiohttp.test_utils import unittest_run_loop

from ddtrace.contrib.asyncio.patch import patch as aio_patch, \
unpatch as aio_unpatch
from ddtrace.contrib.aiohttp.patch import patch, unpatch

from .utils import TraceTestCase


class AIOHttpTest(TraceTestCase):
"""Botocore integration testsuite"""

def setUp(self):
super(AIOHttpTest, self).setUp()
patch(self.tracer, enable_distributed=True)

def tearDown(self):
super(TraceTestCase, self).tearDown()
unpatch()
self.tracer = None

@unittest_run_loop
async def test_wait_for_full_request(self):
aio_patch()

session = aiohttp.ClientSession()
url = self.client.make_url('/')

try:
with self.tracer.trace("foo"):
async def doit():
async with session.get(url) as request:
eq_(200, request.status)
await request.text()
# the trace is created

await asyncio.wait_for(doit(), 20)

traces = self.tracer.writer.pop_traces()
eq_(2, len(traces))

# outer span
eq_(1, len(traces[1]))
root_span = traces[1][0]
root_span_id = root_span.span_id

eq_(None, root_span.parent_id)
eq_(None, root_span.service)
eq_('foo', root_span.name)
eq_('foo', root_span.resource)

eq_(4, len(traces[0]))
client_request_span = traces[0][0]
eq_(root_span_id, client_request_span.parent_id)
eq_('aiohttp.client', client_request_span.service)
eq_('ClientSession.request', client_request_span.name)
eq_('/', client_request_span.resource)

# TCPConnector.connect
connector_connect_span = traces[0][1]
eq_(client_request_span.span_id, connector_connect_span.parent_id)
eq_(client_request_span.trace_id, connector_connect_span.trace_id)
eq_('aiohttp.client', connector_connect_span.service)
eq_('TCPConnector.connect', connector_connect_span.name)
eq_('/', connector_connect_span.resource)

# TCPConnector._create_connection
connector_create_connection_span = traces[0][2]
eq_(connector_connect_span.span_id,
connector_create_connection_span.parent_id)
eq_(connector_connect_span.trace_id,
connector_create_connection_span.trace_id)
eq_('aiohttp.client', connector_create_connection_span.service)
eq_('TCPConnector._create_connection',
connector_create_connection_span.name)
eq_('/', connector_create_connection_span.resource)

# client start span
client_start_span = traces[0][3]
eq_(client_request_span.span_id, client_start_span.parent_id)
eq_(client_request_span.trace_id, client_start_span.trace_id)
eq_('aiohttp.client', client_start_span.service)
eq_('ClientResponse.start', client_start_span.name)
eq_('/', client_start_span.resource)
finally:
aio_unpatch()
2 changes: 2 additions & 0 deletions tests/contrib/aiohttp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .app.web import setup_app
from ...test_tracer import get_dummy_tracer
from ddtrace.contrib.asyncio import context_provider


class TraceTestCase(AioHTTPTestCase):
Expand Down Expand Up @@ -33,5 +34,6 @@ def get_app(self, loop=None):
self.app = setup_app(loop)
# trace the app
self.tracer = get_dummy_tracer()
self.tracer.configure(context_provider=context_provider)
self.enable_tracing()
return self.app
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ commands =
aiopg_contrib-{py34}: nosetests {posargs} --exclude=".*(test_aiopg_35).*" tests/contrib/aiopg
aiopg_contrib-{py35,py36}: nosetests {posargs} tests/contrib/aiopg
asyncpg_contrib-{py35,py36}: nosetests {posargs} tests/contrib/asyncpg
aiohttp_contrib: nosetests {posargs} tests/contrib/aiohttp
aiohttp_contrib-{py34}: nosetests {posargs} --exclude=".*(test_35).*" tests/contrib/aiohttp
aiohttp_contrib-{py35,py36}: nosetests {posargs} tests/contrib/aiohttp
asyncio_contrib: nosetests {posargs} tests/contrib/asyncio
boto_contrib: nosetests {posargs} tests/contrib/boto
botocore_contrib: nosetests {posargs} tests/contrib/botocore
Expand Down

0 comments on commit 3982991

Please sign in to comment.