def _add_timer_to_jenkins_requester()

in services/jenkins-autoscaling/lambda_mxnet_ci/autoscaling/handler.py [0:0]


def _add_timer_to_jenkins_requester(jenkins_server):
    """
    Attach a hook to the Jenkins requester functions that allow to print the duration of an HTTP request
    :param jenkins_server: Hooked jenkins server
    :return: Nothing
    """

    def timing_decorator(f):
        def wrap(*args, **kwargs):
            # These guys don't like to call their API in a consistent fashion. Thus, we have to play hide and seek
            # and search for the argument...
            url = None
            for arg in args:
                if '://' in arg:
                    url = arg
                    break

            if not url:
                url = kwargs['url']

            logging.debug('%s is starting request to %s', f.__name__, url)

            time1 = time.time()
            ret = f(*args, **kwargs)
            time2 = time.time()

            logging.debug('{:s} took {:.3f} ms to request {:s}'.format(f.__name__, (time2 - time1) * 1000.0, url))

            return ret

        return wrap

    jenkins_server.requester.get_and_confirm_status = \
        timing_decorator(jenkins_server.requester.get_and_confirm_status)
    jenkins_server.requester.post_and_confirm_status = \
        timing_decorator(jenkins_server.requester.post_and_confirm_status)
    jenkins_server.requester.get_url = \
        timing_decorator(jenkins_server.requester.get_url)
    jenkins_server.requester.post_url = \
        timing_decorator(jenkins_server.requester.post_url)