|
| 1 | +# This is a wrapper for PEP-0249: Python Database API Specification v2.0 |
| 2 | +import opentracing.ext.tags as ext |
| 3 | +import wrapt |
| 4 | + |
| 5 | +from ..tracer import internal_tracer |
| 6 | +from ..log import logger |
| 7 | + |
| 8 | + |
| 9 | +class CursorWrapper(wrapt.ObjectProxy): |
| 10 | + __slots__ = ('_module_name', '_connect_params', '_cursor_params') |
| 11 | + |
| 12 | + def __init__(self, cursor, module_name, |
| 13 | + connect_params=None, cursor_params=None): |
| 14 | + super(CursorWrapper, self).__init__(wrapped=cursor) |
| 15 | + self._module_name = module_name |
| 16 | + self._connect_params = connect_params |
| 17 | + self._cursor_params = cursor_params |
| 18 | + |
| 19 | + def _collect_kvs(self, span, sql): |
| 20 | + try: |
| 21 | + span.set_tag(ext.SPAN_KIND, 'exit') |
| 22 | + span.set_tag(ext.DATABASE_INSTANCE, self._connect_params[1]['db']) |
| 23 | + span.set_tag(ext.DATABASE_STATEMENT, sql) |
| 24 | + span.set_tag(ext.DATABASE_TYPE, 'mysql') |
| 25 | + span.set_tag(ext.DATABASE_USER, self._connect_params[1]['user']) |
| 26 | + span.set_tag(ext.PEER_ADDRESS, "mysql://%s:%s" % |
| 27 | + (self._connect_params[1]['host'], |
| 28 | + self._connect_params[1]['port'])) |
| 29 | + except Exception as e: |
| 30 | + logger.debug(e) |
| 31 | + finally: |
| 32 | + return span |
| 33 | + |
| 34 | + |
| 35 | + def execute(self, sql, params=None): |
| 36 | + try: |
| 37 | + span = None |
| 38 | + context = internal_tracer.current_context() |
| 39 | + |
| 40 | + # If we're not tracing, just return |
| 41 | + if context is None: |
| 42 | + return self.__wrapped__.execute(sql, params) |
| 43 | + |
| 44 | + span = internal_tracer.start_span(self._module_name, child_of=context) |
| 45 | + span = self._collect_kvs(span, sql) |
| 46 | + span.set_tag('op', 'execute') |
| 47 | + |
| 48 | + result = self.__wrapped__.execute(sql, params) |
| 49 | + except Exception as e: |
| 50 | + if span: |
| 51 | + span.log_exception(e) |
| 52 | + raise |
| 53 | + else: |
| 54 | + return result |
| 55 | + finally: |
| 56 | + if span: |
| 57 | + span.finish() |
| 58 | + |
| 59 | + def executemany(self, sql, seq_of_parameters): |
| 60 | + try: |
| 61 | + span = None |
| 62 | + context = internal_tracer.current_context() |
| 63 | + |
| 64 | + # If we're not tracing, just return |
| 65 | + if context is None: |
| 66 | + return self.__wrapped__.execute(sql, params) |
| 67 | + |
| 68 | + span = internal_tracer.start_span(self._module_name, child_of=context) |
| 69 | + span = self._collect_kvs(span, sql) |
| 70 | + span.set_tag('op', 'executemany') |
| 71 | + span.set_tag('count', len(seq_of_parameters)) |
| 72 | + |
| 73 | + result = self.__wrapped__.executemany(sql, seq_of_parameters) |
| 74 | + except Exception as e: |
| 75 | + if span: |
| 76 | + span.log_exception(e) |
| 77 | + raise |
| 78 | + else: |
| 79 | + return result |
| 80 | + finally: |
| 81 | + if span: |
| 82 | + span.finish() |
| 83 | + |
| 84 | + |
| 85 | + def callproc(self, proc_name, params): |
| 86 | + try: |
| 87 | + span = None |
| 88 | + context = internal_tracer.current_context() |
| 89 | + |
| 90 | + # If we're not tracing, just return |
| 91 | + if context is None: |
| 92 | + return self.__wrapped__.execute(sql, params) |
| 93 | + |
| 94 | + span = internal_tracer.start_span(self._module_name, child_of=context) |
| 95 | + span = self._collect_kvs(span, proc_name) |
| 96 | + span.set_tag('op', 'callproc') |
| 97 | + |
| 98 | + result = self.__wrapped__.callproc(proc_name, params) |
| 99 | + except Exception as e: |
| 100 | + if span: |
| 101 | + span.log_exception(e) |
| 102 | + raise |
| 103 | + else: |
| 104 | + return result |
| 105 | + finally: |
| 106 | + if span: |
| 107 | + span.finish() |
| 108 | + |
| 109 | + |
| 110 | +class ConnectionWrapper(wrapt.ObjectProxy): |
| 111 | + __slots__ = ('_module_name', '_connect_params') |
| 112 | + |
| 113 | + def __init__(self, connection, module_name, connect_params): |
| 114 | + super(ConnectionWrapper, self).__init__(wrapped=connection) |
| 115 | + self._module_name = module_name |
| 116 | + self._connect_params = connect_params |
| 117 | + |
| 118 | + def cursor(self, *args, **kwargs): |
| 119 | + return CursorWrapper( |
| 120 | + cursor=self.__wrapped__.cursor(*args, **kwargs), |
| 121 | + module_name=self._module_name, |
| 122 | + connect_params=self._connect_params, |
| 123 | + cursor_params=(args, kwargs) if args or kwargs else None) |
| 124 | + |
| 125 | + def begin(self): |
| 126 | + return self.__wrapped__.begin() |
| 127 | + |
| 128 | + def commit(self): |
| 129 | + return self.__wrapped__.commit() |
| 130 | + |
| 131 | + def rollback(self): |
| 132 | + return self.__wrapped__.rollback() |
| 133 | + |
| 134 | + |
| 135 | +class ConnectionFactory(object): |
| 136 | + def __init__(self, connect_func, module_name): |
| 137 | + self._connect_func = connect_func |
| 138 | + self._module_name = module_name |
| 139 | + self._wrapper_ctor = ConnectionWrapper |
| 140 | + |
| 141 | + def __call__(self, *args, **kwargs): |
| 142 | + connect_params = (args, kwargs) if args or kwargs else None |
| 143 | + |
| 144 | + return self._wrapper_ctor( |
| 145 | + connection=self._connect_func(*args, **kwargs), |
| 146 | + module_name=self._module_name, |
| 147 | + connect_params=connect_params) |
0 commit comments