|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import opentracing |
| 4 | +import opentracing.ext.tags as ext |
| 5 | +import wrapt |
| 6 | + |
| 7 | +from ...log import logger |
| 8 | +from ...singletons import agent, tracer |
| 9 | +from ...util import strip_secrets |
| 10 | + |
| 11 | +import flask |
| 12 | +from flask import request_started, request_finished, got_request_exception |
| 13 | + |
| 14 | + |
| 15 | +def request_started_with_instana(sender, **extra): |
| 16 | + try: |
| 17 | + env = flask.request.environ |
| 18 | + ctx = None |
| 19 | + |
| 20 | + if 'HTTP_X_INSTANA_T' in env and 'HTTP_X_INSTANA_S' in env: |
| 21 | + ctx = tracer.extract(opentracing.Format.HTTP_HEADERS, env) |
| 22 | + |
| 23 | + flask.g.scope = tracer.start_active_span('wsgi', child_of=ctx) |
| 24 | + span = flask.g.scope.span |
| 25 | + |
| 26 | + if agent.extra_headers is not None: |
| 27 | + for custom_header in agent.extra_headers: |
| 28 | + # Headers are available in this format: HTTP_X_CAPTURE_THIS |
| 29 | + header = ('HTTP_' + custom_header.upper()).replace('-', '_') |
| 30 | + if header in env: |
| 31 | + span.set_tag("http.%s" % custom_header, env[header]) |
| 32 | + |
| 33 | + span.set_tag(ext.HTTP_METHOD, flask.request.method) |
| 34 | + if 'PATH_INFO' in env: |
| 35 | + span.set_tag(ext.HTTP_URL, env['PATH_INFO']) |
| 36 | + if 'QUERY_STRING' in env and len(env['QUERY_STRING']): |
| 37 | + scrubbed_params = strip_secrets(env['QUERY_STRING'], agent.secrets_matcher, agent.secrets_list) |
| 38 | + span.set_tag("http.params", scrubbed_params) |
| 39 | + if 'HTTP_HOST' in env: |
| 40 | + span.set_tag("http.host", env['HTTP_HOST']) |
| 41 | + except: |
| 42 | + logger.debug("Flask before_request", exc_info=True) |
| 43 | + |
| 44 | + |
| 45 | +def request_finished_with_instana(sender, response, **extra): |
| 46 | + try: |
| 47 | + scope = None |
| 48 | + |
| 49 | + # If we're not tracing, just return |
| 50 | + if not hasattr(flask.g, 'scope'): |
| 51 | + return |
| 52 | + |
| 53 | + scope = flask.g.scope |
| 54 | + span = scope.span |
| 55 | + |
| 56 | + if 500 <= response.status_code <= 511: |
| 57 | + span.set_tag("error", True) |
| 58 | + ec = span.tags.get('ec', 0) |
| 59 | + if ec is 0: |
| 60 | + span.set_tag("ec", ec+1) |
| 61 | + |
| 62 | + span.set_tag(ext.HTTP_STATUS_CODE, int(response.status_code)) |
| 63 | + tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, response.headers) |
| 64 | + response.headers.add('Server-Timing', "intid;desc=%s" % scope.span.context.trace_id) |
| 65 | + except: |
| 66 | + logger.debug("Flask after_request", exc_info=True) |
| 67 | + finally: |
| 68 | + if scope is not None: |
| 69 | + scope.close() |
| 70 | + return response |
| 71 | + |
| 72 | + |
| 73 | +def log_exception_with_instana(sender, exception, **extra): |
| 74 | + # If we're not tracing, just return |
| 75 | + if not hasattr(flask.g, 'scope'): |
| 76 | + return |
| 77 | + |
| 78 | + scope = flask.g.scope |
| 79 | + |
| 80 | + if scope is not None: |
| 81 | + span = scope.span |
| 82 | + if span is not None: |
| 83 | + span.log_exception(exception) |
| 84 | + |
| 85 | + |
| 86 | +@wrapt.patch_function_wrapper('flask', 'Flask.handle_user_exception') |
| 87 | +def handle_user_exception_with_instana(wrapped, instance, argv, kwargs): |
| 88 | + exc = argv[0] |
| 89 | + |
| 90 | + if hasattr(flask.g, 'scope'): |
| 91 | + scope = flask.g.scope |
| 92 | + span = scope.span |
| 93 | + |
| 94 | + if not hasattr(exc, 'code'): |
| 95 | + span.log_exception(exc) |
| 96 | + span.set_tag(ext.HTTP_STATUS_CODE, 500) |
| 97 | + scope.close() |
| 98 | + flask.g.scope = None |
| 99 | + |
| 100 | + return wrapped(*argv, **kwargs) |
| 101 | + |
| 102 | + |
| 103 | +@wrapt.patch_function_wrapper('flask', 'templating._render') |
| 104 | +def render_with_instana(wrapped, instance, argv, kwargs): |
| 105 | + ctx = argv[1] |
| 106 | + |
| 107 | + # If we're not tracing, just return |
| 108 | + if not hasattr(ctx['g'], 'scope'): |
| 109 | + return wrapped(*argv, **kwargs) |
| 110 | + |
| 111 | + with tracer.start_active_span("render", child_of=ctx['g'].scope.span) as rscope: |
| 112 | + try: |
| 113 | + template = argv[0] |
| 114 | + |
| 115 | + rscope.span.set_tag("type", "template") |
| 116 | + if template.name is None: |
| 117 | + rscope.span.set_tag("name", '(from string)') |
| 118 | + else: |
| 119 | + rscope.span.set_tag("name", template.name) |
| 120 | + return wrapped(*argv, **kwargs) |
| 121 | + except Exception as e: |
| 122 | + rscope.span.log_exception(e) |
| 123 | + raise |
| 124 | + |
| 125 | + |
| 126 | +@wrapt.patch_function_wrapper('flask', 'Flask.full_dispatch_request') |
| 127 | +def full_dispatch_request_with_instana(wrapped, instance, argv, kwargs): |
| 128 | + if not hasattr(instance, '_stan_wuz_here'): |
| 129 | + logger.debug("Applying flask before/after instrumentation funcs") |
| 130 | + setattr(instance, "_stan_wuz_here", True) |
| 131 | + got_request_exception.connect(log_exception_with_instana, instance) |
| 132 | + request_started.connect(request_started_with_instana, instance) |
| 133 | + request_finished.connect(request_finished_with_instana, instance) |
| 134 | + return wrapped(*argv, **kwargs) |
| 135 | + |
| 136 | + |
| 137 | +logger.debug("Instrumenting flask (with blinker support)") |
0 commit comments