|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import opentracing |
| 4 | +from opentracing.scope_managers.tornado import tracer_stack_context |
| 5 | +import wrapt |
| 6 | +import sys |
| 7 | + |
| 8 | +from ...log import logger |
| 9 | +from ...singletons import agent, tornado_tracer |
| 10 | +from ...util import strip_secrets |
| 11 | + |
| 12 | +from distutils.version import LooseVersion |
| 13 | + |
| 14 | +# Tornado >=6.0 switched to contextvars for context management. This requires changes to the opentracing |
| 15 | +# scope managers which we will tackle soon. |
| 16 | +# Limit Tornado version for the time being. |
| 17 | +if (('tornado' in sys.modules) and |
| 18 | + hasattr(sys.modules['tornado'], 'version') and |
| 19 | + (LooseVersion(sys.modules['tornado'].version) < LooseVersion('6.0.0'))): |
| 20 | + |
| 21 | + try: |
| 22 | + import tornado |
| 23 | + |
| 24 | + @wrapt.patch_function_wrapper('tornado.web', 'RequestHandler._execute') |
| 25 | + def execute_with_instana(wrapped, instance, argv, kwargs): |
| 26 | + try: |
| 27 | + ctx = tornado_tracer.extract(opentracing.Format.HTTP_HEADERS, instance.request.headers) |
| 28 | + scope = tornado_tracer.start_active_span('tornado-server', child_of=ctx) |
| 29 | + |
| 30 | + # Query param scrubbing |
| 31 | + if instance.request.query is not None and len(instance.request.query) > 0: |
| 32 | + cleaned_qp = strip_secrets(instance.request.query, agent.secrets_matcher, agent.secrets_list) |
| 33 | + scope.span.set_tag("http.params", cleaned_qp) |
| 34 | + |
| 35 | + scope.span.set_tag("http.host", instance.request.host) |
| 36 | + scope.span.set_tag("http.method", instance.request.method) |
| 37 | + scope.span.set_tag("http.path", instance.request.path) |
| 38 | + |
| 39 | + # Custom header tracking support |
| 40 | + if agent.extra_headers is not None: |
| 41 | + for custom_header in agent.extra_headers: |
| 42 | + if custom_header in instance.request.headers: |
| 43 | + scope.span.set_tag("http.%s" % custom_header, instance.request.headers[custom_header]) |
| 44 | + |
| 45 | + with tracer_stack_context(): |
| 46 | + setattr(instance.request, "_instana", scope) |
| 47 | + |
| 48 | + # Set the context response headers now because tornado doesn't give us a better option to do so |
| 49 | + # later for this request. |
| 50 | + tornado_tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, instance._headers) |
| 51 | + instance.set_header(name='Server-Timing', value="intid;desc=%s" % scope.span.context.trace_id) |
| 52 | + |
| 53 | + return wrapped(*argv, **kwargs) |
| 54 | + except Exception: |
| 55 | + logger.debug("tornado execute", exc_info=True) |
| 56 | + |
| 57 | + |
| 58 | + @wrapt.patch_function_wrapper('tornado.web', 'RequestHandler.set_default_headers') |
| 59 | + def set_default_headers_with_instana(wrapped, instance, argv, kwargs): |
| 60 | + if not hasattr(instance.request, '_instana'): |
| 61 | + return wrapped(*argv, **kwargs) |
| 62 | + |
| 63 | + scope = instance.request._instana |
| 64 | + tornado_tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, instance._headers) |
| 65 | + instance.set_header(name='Server-Timing', value="intid;desc=%s" % scope.span.context.trace_id) |
| 66 | + |
| 67 | + |
| 68 | + @wrapt.patch_function_wrapper('tornado.web', 'RequestHandler.on_finish') |
| 69 | + def on_finish_with_instana(wrapped, instance, argv, kwargs): |
| 70 | + try: |
| 71 | + if not hasattr(instance.request, '_instana'): |
| 72 | + return wrapped(*argv, **kwargs) |
| 73 | + |
| 74 | + scope = instance.request._instana |
| 75 | + status_code = instance.get_status() |
| 76 | + |
| 77 | + # Mark 500 responses as errored |
| 78 | + if 500 <= status_code <= 511: |
| 79 | + scope.span.set_tag("error", True) |
| 80 | + ec = scope.span.tags.get('ec', 0) |
| 81 | + if ec is 0: |
| 82 | + scope.span.set_tag("ec", ec + 1) |
| 83 | + |
| 84 | + scope.span.set_tag("http.status_code", status_code) |
| 85 | + scope.span.finish() |
| 86 | + scope.close() |
| 87 | + |
| 88 | + return wrapped(*argv, **kwargs) |
| 89 | + except Exception: |
| 90 | + logger.debug("tornado on_finish", exc_info=True) |
| 91 | + |
| 92 | + @wrapt.patch_function_wrapper('tornado.web', 'RequestHandler.log_exception') |
| 93 | + def log_exception_with_instana(wrapped, instance, argv, kwargs): |
| 94 | + try: |
| 95 | + if not hasattr(instance.request, '_instana'): |
| 96 | + return wrapped(*argv, **kwargs) |
| 97 | + |
| 98 | + if not isinstance(argv[1], tornado.web.HTTPError): |
| 99 | + scope = instance.request._instana |
| 100 | + scope.span.log_exception(argv[0]) |
| 101 | + |
| 102 | + return wrapped(*argv, **kwargs) |
| 103 | + except Exception: |
| 104 | + logger.debug("tornado log_exception", exc_info=True) |
| 105 | + |
| 106 | + logger.debug("Instrumenting tornado server") |
| 107 | + except ImportError: |
| 108 | + pass |
| 109 | + |
0 commit comments