Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aiohttp 2.x+ client tracing support #294

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
132 commits
Select commit Hold shift + click to select a range
907a086
Merge remote-tracking branch 'DataDog/master'
thehesiod Apr 12, 2017
ca55ba4
work on adding support for aiopg + aiobotocore + context patching
thehesiod Apr 12, 2017
26b708b
work on adding unittests
thehesiod Apr 12, 2017
cd7e57f
fix aiopg patching
thehesiod Apr 12, 2017
1bb231a
flake fixes
thehesiod Apr 12, 2017
837e8c2
fix comments
thehesiod Apr 12, 2017
4ebcc16
add distributed tracing support
thehesiod Apr 13, 2017
ad585cd
asyncio fixes
thehesiod Apr 13, 2017
79d71be
set span.error
thehesiod Apr 13, 2017
a7da295
add support for minimum response status to be considered an error
thehesiod Apr 13, 2017
e715c9f
add support for distributed aiobotocore spans
thehesiod Apr 13, 2017
7649d89
move to helpers
thehesiod Apr 13, 2017
c0ab3df
add ability to specify tracer and pep fixes
thehesiod Apr 14, 2017
d5562e1
wrap read method
thehesiod Apr 14, 2017
7b82dfc
populate read span's info
thehesiod Apr 14, 2017
8cc236e
add support for truncating arg values
thehesiod Apr 14, 2017
2b7c8f1
attempt to fix tests
thehesiod Apr 14, 2017
ca15385
bugfix
thehesiod Apr 14, 2017
ef9a5a0
fix unittests
thehesiod Apr 15, 2017
2c4fe87
fix unittest
thehesiod Apr 15, 2017
da5dc97
fix unittests
thehesiod Apr 15, 2017
9a5508e
flake fix
thehesiod Apr 15, 2017
df29727
fix tests
thehesiod Apr 15, 2017
13f8536
fix tests
thehesiod Apr 15, 2017
8c29099
fix aiopg unittests
thehesiod Apr 15, 2017
93c13a7
bugfix
thehesiod Apr 15, 2017
98480dd
remove await syntax support
thehesiod Apr 15, 2017
d3e49be
add flask dep
thehesiod Apr 15, 2017
0ce4b56
fix unittests
thehesiod Apr 15, 2017
b9ade72
Merge remote-tracking branch 'DataDog/master' into thehesiod-aio-utils
thehesiod May 4, 2017
28c67b6
Merge remote-tracking branch 'DataDog/master'
thehesiod May 4, 2017
64e71aa
aiohttp enhancements
thehesiod May 4, 2017
90b65e3
Merge remote-tracking branch 'DataDog/master'
thehesiod May 15, 2017
950d65c
Merge remote-tracking branch 'DataDog/master' into thehesiod-aio-utils
thehesiod May 15, 2017
064e4d4
add support for base parent span IDs
thehesiod May 15, 2017
5afcdc0
better asyncio task chaining
thehesiod May 16, 2017
b1257cc
add support for patterns
thehesiod May 16, 2017
78445de
add support for patterns
thehesiod May 16, 2017
69d59a9
set_traceback already sets this
thehesiod May 16, 2017
34bb4c9
set_traceback already sets this
thehesiod May 16, 2017
f82f666
bugfix
thehesiod May 17, 2017
8ec719d
bugfix
thehesiod May 17, 2017
f385deb
remove unused code
thehesiod May 19, 2017
79ed2e0
remove unused code
thehesiod May 19, 2017
e21d3ae
remove bad change
thehesiod May 19, 2017
c58b95b
remove bad change
thehesiod May 19, 2017
0107224
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp
thehesiod May 19, 2017
ecd49de
Merge remote-tracking branch 'DataDog/master' into thehesiod-aio-utils
thehesiod May 19, 2017
517d0be
Merge remote-tracking branch 'DataDog/master'
thehesiod May 25, 2017
2f1085e
Merge remote-tracking branch 'DataDog/master' into thehesiod-aio-utils
thehesiod May 25, 2017
3f7aa11
updates
thehesiod May 25, 2017
14823c3
Merge remote-tracking branch 'DataDog/master' into thehesiod-aio-utils
thehesiod May 31, 2017
8402096
Merge remote-tracking branch 'DataDog/master'
thehesiod May 31, 2017
4b29d60
Merge remote-tracking branch 'DataDog/master' into thehesiod-aio-utils
thehesiod Jun 7, 2017
77fdcc5
fix merge change
thehesiod Jun 7, 2017
3b7ce2f
Merge remote-tracking branch 'DataDog/master'
thehesiod Jun 8, 2017
efb5e08
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp
thehesiod Jun 8, 2017
766609a
Merge remote-tracking branch 'DataDog/master'
thehesiod Jun 14, 2017
a74ff3d
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp
thehesiod Jun 14, 2017
2285b98
Merge remote-tracking branch 'DataDog/master'
thehesiod Jun 20, 2017
c54c031
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp
thehesiod Jun 20, 2017
421e88d
changes based on slack chat w/ manu
thehesiod Jun 21, 2017
45b3f1b
remove whitespace
thehesiod Jun 21, 2017
2885f9b
Merge branch 'thehesiod-aiohttp' into thehesiod-aiohttp-client
thehesiod Jun 21, 2017
984eb96
add aiohttp client tracing support
thehesiod Jun 22, 2017
cb0e6a7
Merge remote-tracking branch 'DataDog/master' into thehesiod-aio-utils
thehesiod Jun 22, 2017
e977aa1
add aiohttp client tracing support
thehesiod Jun 22, 2017
260e54b
fix unittests
thehesiod Jun 22, 2017
f212020
fix unittests
thehesiod Jun 22, 2017
4eadc04
Merge branch 'thehesiod-aio-utils' into thehesiod-aiohttp
thehesiod Jun 22, 2017
249eea4
Revert "Merge branch 'thehesiod-aio-utils' into thehesiod-aiohttp"
thehesiod Jun 22, 2017
378c404
fix unittests
thehesiod Jun 23, 2017
1f71ae9
avoid unnecessary stringification
thehesiod Jun 23, 2017
caf025e
Merge branch 'thehesiod-aiohttp' into thehesiod-aiohttp-client
thehesiod Jun 23, 2017
71511f3
updates
thehesiod Jun 23, 2017
2e86f11
fix spacing
thehesiod Jun 23, 2017
e5924e7
we have to manually set the service
thehesiod Jun 24, 2017
5a39ced
add length
thehesiod Jun 24, 2017
eb046ce
ensure trace_headers applies to retries
thehesiod Jun 26, 2017
d1eca92
fix check
thehesiod Jun 26, 2017
e285168
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp-…
thehesiod Jul 2, 2017
e572209
revert changes
thehesiod Jul 2, 2017
e9d8056
revert more changes
thehesiod Jul 2, 2017
d823662
Merge remote-tracking branch 'DataDog/master'
thehesiod Jul 2, 2017
dd47c2c
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp-…
thehesiod Jul 3, 2017
8a7ce47
fix
thehesiod Jul 3, 2017
a019984
add parity with middleware
thehesiod Jul 3, 2017
c3b2cd9
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp-…
thehesiod Jul 6, 2017
ea3ba40
fix for parent spans
thehesiod Jul 6, 2017
c0ede6e
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp-…
thehesiod Nov 14, 2017
69739aa
switch to new mechanism
thehesiod Nov 20, 2017
0c0f379
cleanup
thehesiod Nov 20, 2017
aa03185
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp-…
thehesiod Jan 8, 2018
8ebd4a2
changes based on review
thehesiod Jan 8, 2018
5d747ba
update based on review
thehesiod Jan 8, 2018
be038a9
fix bug and unittests
thehesiod Jan 9, 2018
8b8c769
Merge remote-tracking branch 'DataDog/master'
thehesiod Jan 9, 2018
0f8e82c
ver bump
thehesiod Jan 9, 2018
58f78db
add short-circuit ability
thehesiod Jan 9, 2018
f3845d6
set error code on error like httplib
thehesiod Jan 9, 2018
09813da
missed a spot
thehesiod Jan 9, 2018
534d553
add missing aenter/aexit
thehesiod Jan 16, 2018
839e5dd
bugfix
thehesiod Jan 16, 2018
a6ab0c9
Merge remote-tracking branch 'DataDog/master'
thehesiod Jan 23, 2018
5af9b51
Merge remote-tracking branch 'DataDog/master'
thehesiod Feb 3, 2018
af07ff9
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp-…
thehesiod Feb 3, 2018
3f53e27
add connector tracing
thehesiod Feb 3, 2018
99fb053
fix port
thehesiod Feb 8, 2018
d3bc98e
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp-…
thehesiod Mar 10, 2018
ae9521c
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp-…
thehesiod Jun 14, 2018
2232332
bugfix
thehesiod Aug 10, 2018
e90a0a8
fix warnings
thehesiod Sep 19, 2018
d3b687d
flake fixes
thehesiod Oct 13, 2018
3aca41e
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp-…
thehesiod Oct 13, 2018
4e4e7e3
revert change
thehesiod Oct 13, 2018
50c487a
pull in changes from aio-utils
thehesiod Oct 13, 2018
6619697
add missing change
thehesiod Oct 13, 2018
3021151
tweak
thehesiod Oct 13, 2018
e13fb26
flake fixes
thehesiod Oct 13, 2018
d776869
Merge branch 'master' of https://github.com/DataDog/dd-trace-py
thehesiod Apr 4, 2019
550b804
Merge branch 'master' into thehesiod-aiohttp-client
thehesiod Apr 4, 2019
f2beee9
cherry pick 3982991dd8b8eaa4a537c3026e73c64e0463574c
thehesiod Apr 4, 2019
528bac5
fix merge
thehesiod Apr 4, 2019
3f169fa
flake fix
thehesiod Apr 4, 2019
ffeb9d3
fix flake
thehesiod Apr 4, 2019
ce78aad
fix tests
thehesiod Apr 4, 2019
51979e8
bugfix
thehesiod Apr 4, 2019
689c708
revert change
thehesiod Apr 8, 2019
304c40a
Merge branch 'master' of https://github.com/DataDog/dd-trace-py
thehesiod Apr 27, 2019
e8d93c4
Merge branch 'master' into thehesiod-aiohttp-client
thehesiod Apr 27, 2019
ed8996d
fix patch
thehesiod Jun 14, 2019
14a266f
Merge remote-tracking branch 'DataDog/master' into thehesiod-aiohttp-…
thehesiod Feb 27, 2020
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
10 changes: 7 additions & 3 deletions ddtrace/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,13 @@ 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

# 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 or (trace and trace[0].trace_id == self._parent_trace_id):
self._parent_trace_id = None
self._parent_span_id = None
self._sampling_priority = None
return trace, sampled

elif self._partial_flush_enabled:
Expand Down
113 changes: 67 additions & 46 deletions ddtrace/contrib/aiohttp/middlewares.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import functools

from ..asyncio import context_provider
from ...compat import stringify
Expand All @@ -8,14 +9,73 @@
from ...settings import config


try:
from aiohttp.web import middleware
AIOHTTP_2x = True
except ImportError:
AIOHTTP_2x = False

def middleware(f):
return f


CONFIG_KEY = 'datadog_trace'
REQUEST_CONTEXT_KEY = 'datadog_context'
REQUEST_CONFIG_KEY = '__datadog_trace_config'
REQUEST_SPAN_KEY = '__datadog_request_span'


@asyncio.coroutine
def trace_middleware(app, handler):
@middleware
def trace_middleware_2x(request, handler, app=None):
# application configs
if app is None:
app = request.app

tracer = app[CONFIG_KEY]['tracer']
service = app[CONFIG_KEY]['service']
distributed_tracing = app[CONFIG_KEY]['distributed_tracing_enabled']

# Create a new context based on the propagated information.
if distributed_tracing:
propagator = HTTPPropagator()
context = propagator.extract(request.headers)
# Only need to active the new context if something was propagated
if context.trace_id:
tracer.context_provider.activate(context)

# trace the handler
request_span = tracer.trace(
'aiohttp.request',
service=service,
span_type=SpanTypes.WEB,
)
request_span.set_tag(SPAN_MEASURED_KEY)

# Configure trace search sample rate
# DEV: aiohttp is special case maintains separate configuration from config api
analytics_enabled = app[CONFIG_KEY]['analytics_enabled']
if (config.analytics_enabled and analytics_enabled is not False) or analytics_enabled is True:
request_span.set_tag(
ANALYTICS_SAMPLE_RATE_KEY,
app[CONFIG_KEY].get('analytics_sample_rate', True)
)

# attach the context and the root span to the request; the Context
# may be freely used by the application code
request[REQUEST_CONTEXT_KEY] = request_span.context
request[REQUEST_SPAN_KEY] = request_span
request[REQUEST_CONFIG_KEY] = app[CONFIG_KEY]
try:
response = yield from handler(request)
return response
except Exception:
request_span.set_traceback()
raise


@asyncio.coroutine
def trace_middleware_1x(app, handler):
"""
``aiohttp`` middleware that traces the handler execution.
Because handlers are run in different tasks for each request, we attach the Context
Expand All @@ -24,50 +84,10 @@ def trace_middleware(app, handler):
* the Task is used by the internal automatic instrumentation
* the ``Context`` attached to the request can be freely used in the application code
"""
@asyncio.coroutine
def attach_context(request):
# application configs
tracer = app[CONFIG_KEY]['tracer']
service = app[CONFIG_KEY]['service']
distributed_tracing = app[CONFIG_KEY]['distributed_tracing_enabled']

# Create a new context based on the propagated information.
if distributed_tracing:
propagator = HTTPPropagator()
context = propagator.extract(request.headers)
# Only need to active the new context if something was propagated
if context.trace_id:
tracer.context_provider.activate(context)

# trace the handler
request_span = tracer.trace(
'aiohttp.request',
service=service,
span_type=SpanTypes.WEB,
)
request_span.set_tag(SPAN_MEASURED_KEY)
return functools.partial(trace_middleware_2x, handler=handler, app=app)

# Configure trace search sample rate
# DEV: aiohttp is special case maintains separate configuration from config api
analytics_enabled = app[CONFIG_KEY]['analytics_enabled']
if (config.analytics_enabled and analytics_enabled is not False) or analytics_enabled is True:
request_span.set_tag(
ANALYTICS_SAMPLE_RATE_KEY,
app[CONFIG_KEY].get('analytics_sample_rate', True)
)

# attach the context and the root span to the request; the Context
# may be freely used by the application code
request[REQUEST_CONTEXT_KEY] = request_span.context
request[REQUEST_SPAN_KEY] = request_span
request[REQUEST_CONFIG_KEY] = app[CONFIG_KEY]
try:
response = yield from handler(request)
return response
except Exception:
request_span.set_traceback()
raise
return attach_context

trace_middleware = trace_middleware_2x if AIOHTTP_2x else trace_middleware_1x


@asyncio.coroutine
Expand Down Expand Up @@ -114,14 +134,15 @@ def on_prepare(request, response):
request_span.finish()


def trace_app(app, tracer, service='aiohttp-web'):
def trace_app(app, tracer, service='aiohttp-web', distributed_tracing=True):
"""
Tracing function that patches the ``aiohttp`` application so that it will be
traced using the given ``tracer``.

:param app: aiohttp application to trace
:param tracer: tracer instance to use
:param service: service name of tracer
:param distributed_tracing: set to True to enable distributed tracing
"""

# safe-guard: don't trace an application twice
Expand All @@ -133,7 +154,7 @@ def trace_app(app, tracer, service='aiohttp-web'):
app[CONFIG_KEY] = {
'tracer': tracer,
'service': service,
'distributed_tracing_enabled': True,
'distributed_tracing_enabled': distributed_tracing,
'analytics_enabled': None,
'analytics_sample_rate': 1.0,
}
Expand Down
Loading