|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import opentracing |
| 4 | +import wrapt |
| 5 | + |
| 6 | +from ...log import logger |
| 7 | +from ...singletons import agent, async_tracer |
| 8 | +from ...util import strip_secrets |
| 9 | + |
| 10 | + |
| 11 | +try: |
| 12 | + import aiohttp |
| 13 | + import asyncio |
| 14 | + |
| 15 | + async def stan_request_start(session, trace_config_ctx, params): |
| 16 | + try: |
| 17 | + parent_span = async_tracer.active_span |
| 18 | + |
| 19 | + # If we're not tracing, just return |
| 20 | + if parent_span is None: |
| 21 | + return |
| 22 | + |
| 23 | + scope = async_tracer.start_active_span("aiohttp-client", child_of=parent_span) |
| 24 | + trace_config_ctx.scope = scope |
| 25 | + |
| 26 | + async_tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, params.headers) |
| 27 | + |
| 28 | + parts = str(params.url).split('?') |
| 29 | + if len(parts) > 1: |
| 30 | + cleaned_qp = strip_secrets(parts[1], agent.secrets_matcher, agent.secrets_list) |
| 31 | + scope.span.set_tag("http.params", cleaned_qp) |
| 32 | + scope.span.set_tag("http.url", parts[0]) |
| 33 | + scope.span.set_tag('http.method', params.method) |
| 34 | + except: |
| 35 | + logger.debug("stan_request_start", exc_info=True) |
| 36 | + |
| 37 | + async def stan_request_end(session, trace_config_ctx, params): |
| 38 | + try: |
| 39 | + scope = trace_config_ctx.scope |
| 40 | + if scope is not None: |
| 41 | + scope.span.set_tag('http.status_code', params.response.status) |
| 42 | + |
| 43 | + if 400 <= params.response.status <= 599: |
| 44 | + scope.span.set_tag("http.error", params.response.reason) |
| 45 | + scope.span.set_tag("error", True) |
| 46 | + ec = scope.span.tags.get('ec', 0) |
| 47 | + scope.span.set_tag("ec", ec + 1) |
| 48 | + |
| 49 | + scope.close() |
| 50 | + except: |
| 51 | + logger.debug("stan_request_end", exc_info=True) |
| 52 | + |
| 53 | + async def stan_request_exception(session, trace_config_ctx, params): |
| 54 | + try: |
| 55 | + scope = trace_config_ctx.scope |
| 56 | + if scope is not None: |
| 57 | + scope.span.log_exception(params.exception) |
| 58 | + scope.span.set_tag("http.error", str(params.exception)) |
| 59 | + scope.close() |
| 60 | + except: |
| 61 | + logger.debug("stan_request_exception", exc_info=True) |
| 62 | + |
| 63 | + @wrapt.patch_function_wrapper('aiohttp.client','ClientSession.__init__') |
| 64 | + def init_with_instana(wrapped, instance, argv, kwargs): |
| 65 | + instana_trace_config = aiohttp.TraceConfig() |
| 66 | + instana_trace_config.on_request_start.append(stan_request_start) |
| 67 | + instana_trace_config.on_request_end.append(stan_request_end) |
| 68 | + instana_trace_config.on_request_exception.append(stan_request_exception) |
| 69 | + if 'trace_configs' in kwargs: |
| 70 | + kwargs['trace_configs'].append(instana_trace_config) |
| 71 | + else: |
| 72 | + kwargs['trace_configs'] = [instana_trace_config] |
| 73 | + |
| 74 | + return wrapped(*argv, **kwargs) |
| 75 | + |
| 76 | + logger.debug("Instrumenting aiohttp client") |
| 77 | +except ImportError: |
| 78 | + pass |
| 79 | + |
0 commit comments