|
| 1 | +# Copyright (c) 2015 Uber Technologies, Inc. |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | + |
| 21 | +from __future__ import absolute_import |
| 22 | + |
| 23 | +import logging |
| 24 | + |
| 25 | +import opentracing |
| 26 | +from ..trace_context import get_current_span |
| 27 | +from ._singleton import singleton |
| 28 | + |
| 29 | +log = logging.getLogger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +@singleton |
| 33 | +def install_patches(): |
| 34 | + try: |
| 35 | + from sqlalchemy.engine import Engine |
| 36 | + from sqlalchemy import event |
| 37 | + except ImportError: |
| 38 | + # If SQLAlchemy cannot be imported, then the project we are |
| 39 | + # instrumenting does not depend on it and we do not need to install |
| 40 | + # the SQL hooks. |
| 41 | + return |
| 42 | + |
| 43 | + log.info('Instrumenting SQL methods for tracing') |
| 44 | + |
| 45 | + @event.listens_for(Engine, 'before_cursor_execute') |
| 46 | + def before_cursor_execute(conn, cursor, statement, parameters, context, |
| 47 | + executemany): |
| 48 | + operation = 'SQL' |
| 49 | + statement = statement.strip() |
| 50 | + if statement: |
| 51 | + operation = '%s %s' % (operation, |
| 52 | + statement.split(' ', 1)[0].upper()) |
| 53 | + if get_current_span() is None: |
| 54 | + span = opentracing.tracer.start_trace( |
| 55 | + operation_name=operation) # TODO pass client=True |
| 56 | + else: |
| 57 | + span = get_current_span().start_child(operation_name=operation) |
| 58 | + if statement: |
| 59 | + span.add_tag('sql', statement) |
| 60 | + context.opentracing_span = span |
| 61 | + |
| 62 | + @event.listens_for(Engine, 'after_cursor_execute') |
| 63 | + def after_cursor_execute(conn, cursor, statement, parameters, context, |
| 64 | + executemany): |
| 65 | + if hasattr(context, 'opentracing_span') and context.opentracing_span: |
| 66 | + context.opentracing_span.finish() |
| 67 | + context.opentracing_span = None |
0 commit comments