# Path: osprofiler/profiler.py
# def clean():
# def _ensure_no_multiple_traced(traceable_attrs):
# def init(hmac_key, base_id=None, parent_id=None):
# def get():
# def start(name, info=None):
# def stop(info=None):
# def trace(name, info=None, hide_args=False, hide_result=True,
#           allow_multiple_trace=True):
#     def decorator(f):
#         def wrapper(*args, **kwargs):
# def trace_cls(name, info=None, hide_args=False, hide_result=True,
#               trace_private=False, allow_multiple_trace=True,
#               trace_class_methods=False, trace_static_methods=False):
#     def trace_checker(attr_name, to_be_wrapped):
#     def decorator(cls):
#     def __init__(cls, cls_name, bases, attrs):
#     def __init__(self, name, info=None):
#     def __enter__(self):
#     def __exit__(self, etype, value, traceback):
#     def __init__(self, hmac_key, base_id=None, parent_id=None):
#     def get_shorten_id(self, uuid_id):
#     def get_base_id(self):
#     def get_parent_id(self):
#     def get_id(self):
#     def start(self, name, info=None):
#     def stop(self, info=None):
#     def _notify(self, name, info):
# class TracedMeta(type):
# class Trace(object):
# class _Profiler(object):


import contextlib
import logging as log
from oslo_utils import reflection
from osprofiler import profiler
def add_tracing(sqlalchemy, engine, name, hide_result=True):
    """Add tracing to all sqlalchemy calls."""

    if not _DISABLED:
        sqlalchemy.event.listen(engine, "before_cursor_execute",
                                _before_cursor_execute(name))
        sqlalchemy.event.listen(
            engine, "after_cursor_execute",
            _after_cursor_execute(hide_result=hide_result)
        )
        sqlalchemy.event.listen(engine, "handle_error", handle_error)


@contextlib.contextmanager
def wrap_session(sqlalchemy, sess):
    with sess as s:
        if not getattr(s.bind, "traced", False):
            add_tracing(sqlalchemy, s.bind, "db")
            s.bind.traced = True
        yield s


def _before_cursor_execute(name):
    """Add listener that will send trace info before query is executed."""

    def handler(conn, cursor, statement, params, context, executemany):
        info = {"db": {
            "statement": statement,
            "params": params}
        }

# Path: osprofiler/profiler.py
# def clean():
# def _ensure_no_multiple_traced(traceable_attrs):
# def init(hmac_key, base_id=None, parent_id=None):
# def get():
# def start(name, info=None):
# def stop(info=None):
# def trace(name, info=None, hide_args=False, hide_result=True,
#           allow_multiple_trace=True):
#     def decorator(f):
#         def wrapper(*args, **kwargs):
# def trace_cls(name, info=None, hide_args=False, hide_result=True,
#               trace_private=False, allow_multiple_trace=True,
#               trace_class_methods=False, trace_static_methods=False):
#     def trace_checker(attr_name, to_be_wrapped):
#     def decorator(cls):
#     def __init__(cls, cls_name, bases, attrs):
#     def __init__(self, name, info=None):
#     def __enter__(self):
#     def __exit__(self, etype, value, traceback):
#     def __init__(self, hmac_key, base_id=None, parent_id=None):
#     def get_shorten_id(self, uuid_id):
#     def get_base_id(self):
#     def get_parent_id(self):
#     def get_id(self):
#     def start(self, name, info=None):
#     def stop(self, info=None):
#     def _notify(self, name, info):
# class TracedMeta(type):
# class Trace(object):
# class _Profiler(object):


# Path: osprofiler/sqlalchemy.py
import contextlib
import logging as log
from oslo_utils import reflection
from osprofiler import profiler

def add_tracing(sqlalchemy, engine, name, hide_result=True):
    """Add tracing to all sqlalchemy calls."""

    if not _DISABLED:
        sqlalchemy.event.listen(engine, "before_cursor_execute",
                                _before_cursor_execute(name))
        sqlalchemy.event.listen(
            engine, "after_cursor_execute",
            _after_cursor_execute(hide_result=hide_result)
        )
        sqlalchemy.event.listen(engine, "handle_error", handle_error)


@contextlib.contextmanager
def wrap_session(sqlalchemy, sess):
    with sess as s:
        if not getattr(s.bind, "traced", False):
            add_tracing(sqlalchemy, s.bind, "db")
            s.bind.traced = True
        yield s


def _before_cursor_execute(name):
    """Add listener that will send trace info before query is executed."""

    def handler(conn, cursor, statement, params, context, executemany):
        info = {"db": {
            "statement": statement,
            "params": params}
        }

        profiler.start(name, info=info)