Example #1
0
def instrument_methods(method_wrappers):
    """
    Instruments the methods as specified by the method_wrappers dict. Key is the method path and value is the function
    that wraps the method.

    :param method_wrappers: dictionary of qualified method names to wrapper methods
    """
    for qualified_method_name, method_wrapper in six.iteritems(
            method_wrappers):
        (fully_qualified_class_name,
         method_name) = qualified_method_name.rsplit('.', 1)
        try:
            class_def = get_class_by_name(fully_qualified_class_name)
            if class_def:
                logger.debug('instrumenting method %s', qualified_method_name)
                wrap_method(class_def, method_name, method_wrapper)
            else:
                logger.warn('%s not instrumented because not found',
                            fully_qualified_class_name)
        except ImportError:
            logger.debug('could not instrument %s', qualified_method_name)
            logger.warn('%s not instrumented because not found',
                        fully_qualified_class_name)
        except AttributeError:
            logger.warn('could not instrument %s', qualified_method_name)
            logger.exception('details')
Example #2
0
def get_wrapper_methods_for_type(rules, return_type):
    """
    Returns the proxy methods from the rules for the given return type. Presumes that the rules use qualified method
    names and that the return type can be used to identify them
    :param rules: dict of qualified method names to wrapper methods
    :param return_type: qualified name of the return type of interest
    :return: dict of simple method names to wrapper methods
    """
    return_cls = get_class_by_name(return_type)
    pf_len = len(return_type) + 1  # length of prefix, which facilitates extracting method names
    # build a dictionary of simple method names to instrumentors for the given type
    return {k[pf_len:]: v(object.__getattribute__(return_cls, k[pf_len:]))
            for k, v in six.iteritems(rules) if k.startswith(return_type) and hasattr(return_cls, k[pf_len:])}
Example #3
0
def get_wrapper_methods_for_type(rules, return_type):
    """
    Returns the proxy methods from the rules for the given return type. Presumes that the rules use qualified method
    names and that the return type can be used to identify them
    :param rules: dict of qualified method names to wrapper methods
    :param return_type: qualified name of the return type of interest
    :return: dict of simple method names to wrapper methods
    """
    return_cls = get_class_by_name(return_type)
    pf_len = len(
        return_type
    ) + 1  # length of prefix, which facilitates extracting method names
    # build a dictionary of simple method names to instrumentors for the given type
    return {
        k[pf_len:]: v(object.__getattribute__(return_cls, k[pf_len:]))
        for k, v in six.iteritems(rules)
        if k.startswith(return_type) and hasattr(return_cls, k[pf_len:])
    }
Example #4
0
def override_classes(overridden_classes, wrapped):
    """
    Override static classes by creating a subclass (via type) that contains overridden methods. Typically this is
    applied to native classes so that their methods can be overridden to wrap returned instances.

    :param overridden_classes: dict of qualified class names as keys and list of targeted method names as values
    """
    for fully_qualified_class_name, targeted_methods in six.iteritems(overridden_classes):
        try:
            cls = get_class_by_name(fully_qualified_class_name)
            if cls:
                logger.debug('instrumenting class %s', fully_qualified_class_name)
                wrap_methods(cls, targeted_methods, wrapped)
            else:
                logger.warn('%s not overridden because not found', fully_qualified_class_name)
        except ImportError:
            logger.debug('could not override %s', fully_qualified_class_name)
            logger.warn('%s not overridden because not found', fully_qualified_class_name)
Example #5
0
def instrument_methods(method_wrappers):
    for qualified_method_name, method_wrapper in six.iteritems(method_wrappers):
        (fully_qualified_class_name, method_name) = qualified_method_name.rsplit('.', 1)
        try:
            class_def = get_class_by_name(fully_qualified_class_name)
            if class_def:
                logger.debug('instrumenting method %s', qualified_method_name)

                original_method = getattr(class_def, method_name)
                delegator = _get_delegating_wrapper(original_method, method_wrapper)

                setattr(class_def, method_name, delegator)
            else:
                logger.warn('%s not instrumented because not found', fully_qualified_class_name)
        except ImportError:
            logger.debug('could not instrument %s', qualified_method_name)
            logger.warn('%s not instrumented because not found', fully_qualified_class_name)
        except:
            logger.exception('could not instrument %s', qualified_method_name)
Example #6
0
def instrument_methods(method_wrappers):
    """
    Instruments the methods as specified by the method_wrappers dict. Key is the method path and value is the function
    that wraps the method.

    :param method_wrappers: dictionary of qualified method names to wrapper methods
    """
    for qualified_method_name, method_wrapper in six.iteritems(method_wrappers):
        (fully_qualified_class_name, method_name) = qualified_method_name.rsplit('.', 1)
        try:
            class_def = get_class_by_name(fully_qualified_class_name)
            if class_def:
                logger.debug('instrumenting method %s', qualified_method_name)
                wrap_method(class_def, method_name, method_wrapper)
            else:
                logger.info('%s not instrumented because not found', fully_qualified_class_name)
        except ImportError:
            logger.debug('could not instrument %s', qualified_method_name)
            logger.info('%s not instrumented because not found', fully_qualified_class_name)
        except AttributeError:
            logger.warn('could not instrument %s', qualified_method_name)
Example #7
0
def override_classes(overridden_classes, wrapped):
    """
    Override static classes by creating a subclass (via type) that contains overridden methods. Typically this is
    applied to native classes so that their methods can be overridden to wrap returned instances.

    :param overridden_classes: dict of qualified class names as keys and list of targeted method names as values
    """
    for fully_qualified_class_name, targeted_methods in six.iteritems(
            overridden_classes):
        try:
            cls = get_class_by_name(fully_qualified_class_name)
            if cls:
                logger.debug('instrumenting class %s',
                             fully_qualified_class_name)
                wrap_methods(cls, targeted_methods, wrapped)
            else:
                logger.warn('%s not overridden because not found',
                            fully_qualified_class_name)
        except ImportError:
            logger.debug('could not override %s', fully_qualified_class_name)
            logger.warn('%s not overridden because not found',
                        fully_qualified_class_name)