Exemplo n.º 1
0
 def decorator(*args, **keywords):
     t = time.time()
     try:
         return f(*args, **keywords)
     finally:
         elapsed = time.time() - t
         telemetry.record('wsgi.response.latency', elapsed)
Exemplo n.º 2
0
    def decorator(*args, **keywords):
        # The type of the returned instance depends on the url
        # but is typically urllib.addinfourl for the baked-in protocols

        if context.has_state('external'):
            # Avoid double counting nested calls. All metrics will be reported
            # relative to the outermost operation
            return f(*args, **keywords)

        url = get_parameter(1, 'fullurl', *args, **keywords)

        if hasattr(url, 'get_full_url'):
            url = url.get_full_url()

        scheme = url.split(':')[0] if ':' in url else 'unknown'

        Timing.push_timer()
        try:
            context.push_state('external')
            telemetry.count('external.{}.requests'.format(scheme))
            a = f(*args, **keywords)
            if a.getcode():
                # Not meaningful for ftp etc
                telemetry.count('external.{}.status.%ixx'.format(scheme) % floor(a.getcode() / 100))
        except:
            telemetry.count('external.{}.errors'.format(scheme))
            raise
        finally:
            context.pop_state('external')
            elapsed, _ = Timing.pop_timer()
            telemetry.record('external.{}.response.latency'.format(scheme), elapsed)

        # Return a wrapped object so we can time subsequent read, readline etc calls
        return _response_wrapper(scheme, a)
Exemplo n.º 3
0
def _urllib_open_wrapper(func, *args, **keywords):
    """ Wraps urllib.request.url_open """

    if not _should_be_instrumented(
            state='external', enable_if='web', disable_if='model'):
        return func(*args, **keywords)

    url = get_parameter(1, 'fullurl', *args, **keywords)

    if hasattr(url, 'get_full_url'):
        url = url.get_full_url()

    scheme = url.split(':')[0] if ':' in url else 'unknown'

    Timing.push_timer()
    try:
        context.push_state('external')
        telemetry.count('external.{}.requests'.format(scheme))
        a = func(*args, **keywords)
        if a.getcode():
            # Not meaningful for ftp etc
            telemetry.count('external.{}.status.%ixx'.format(scheme) %
                            floor(a.getcode() / 100))
    except:
        telemetry.count('external.{}.errors'.format(scheme))
        raise
    finally:
        context.pop_state('external')
        elapsed, _ = Timing.pop_timer()
        telemetry.record('external.{}.response.latency'.format(scheme),
                         elapsed)

    # Return a wrapped object so we can time subsequent read, readline etc calls
    return _response_wrapper(scheme, a)
Exemplo n.º 4
0
def _flask_wsgi_call(f, *args, **kwargs):
    t = time.time()
    try:
        return f(*args, **kwargs)
    finally:
        elapsed = time.time() - t
        telemetry.record('wsgi.response.latency', elapsed)
Exemplo n.º 5
0
def _cherrypy_wsgi_call(func, *args, **keywords):
    t = time.time()
    try:
        return func(*args, **keywords)
    finally:
        elapsed = time.time() - t
        telemetry.record('wsgi.response.latency', elapsed)
Exemplo n.º 6
0
def _django_wsgi_call(original_method, *args, **keywords):
    Timing.push_timer()
    try:
        return original_method(*args, **keywords)
    finally:
        elapsed, _ = Timing.pop_timer()
        telemetry.record('wsgi.response.latency', elapsed)
Exemplo n.º 7
0
    def decorator(*args, **keywords):
        try:
            route = args[2] if args[2] else None
            context.push_tag('web.route', route)
            context.push_tag('web.method', args[1])
            telemetry.count('web.requests')
            Timing.push_timer()

            # call the request function
            response = f(*args, **keywords)

            if response.status:
                telemetry.count('web.status.%sxx' % response.status[0:1])
            return response
        except Exception as e:
            telemetry.count('web.errors')
            raise e
        finally:
            try:
                elapsed, net_elapsed = Timing.pop_timer()
                telemetry.record('web.response.latency', elapsed)
                telemetry.record('app.response.latency', net_elapsed)
                try:
                    context.pop_tag()
                    context.pop_tag()
                except:
                    logger.exception('Problem popping contexts')
            except:
                logger.exception('Teardown handler failed')
                raise
Exemplo n.º 8
0
 def decorator(*args, **keywords):
     t = time.time()
     try:
         return f(*args, **keywords)
     finally:
         elapsed = time.time() - t
         telemetry.record('wsgi.response.latency', elapsed)
Exemplo n.º 9
0
 def decorator(*args, **keywords):
     Timing.push_timer()
     try:
         return original_method(*args, **keywords)
     finally:
         elapsed, net_elapsed = Timing.pop_timer()
         telemetry.record('wsgi.response.latency', elapsed)
Exemplo n.º 10
0
def _django_wsgi_call(original_method, *args, **keywords):
    Timing.push_timer()
    try:
        return original_method(*args, **keywords)
    finally:
        elapsed, _ = Timing.pop_timer()
        telemetry.record("wsgi.response.latency", elapsed)
Exemplo n.º 11
0
def _flask_dispatch(f, *args, **keywords):
    try:
        telemetry.count('web.requests')
        Timing.push_timer()

        return f(*args, **keywords)
    finally:
        elapsed, net_elapsed = Timing.pop_timer()
        telemetry.record('web.response.latency', elapsed)
        telemetry.record('app.response.latency', net_elapsed)
Exemplo n.º 12
0
    def decorator(*args, **keywords):
        response = f(*args, **keywords)

        try:
            arbiter = args[0]
            telemetry.record('gunicorn.workers', arbiter._last_logged_active_worker_count, is_timer=False,
                             reporter='gunicorn')
        except Exception as e:
            logger.exception("Instrumentation error while reporting gunicorn.workers")

        return response
Exemplo n.º 13
0
 def process_response(self, request, response):
     elapsed, net_elapsed = Timing.pop_timer()
     if self.is_active:
         telemetry.record("web.response.latency", elapsed)
         telemetry.record("app.response.latency", net_elapsed)
         telemetry.count("web.status.%ixx" % floor(response.status_code / 100))
         context.pop_state(STATE_NAME)
         self.is_active = False
     else:
         logger.warn("process_response without request")
     return response
Exemplo n.º 14
0
 def process_response(self, request, response):
     elapsed, net_elapsed = Timing.pop_timer()
     if self.is_active:
         telemetry.record('web.response.latency', elapsed)
         telemetry.record('app.response.latency', net_elapsed)
         telemetry.count('web.status.%ixx' % floor(response.status_code / 100))
         context.pop_state(STATE_NAME)
         self.is_active = False
     else:
         logger.warn('process_response without request')
     return response
Exemplo n.º 15
0
 def decorator(*args, **keywords):
     telemetry.count('external.http.requests')
     Timing.push_timer()
     try:
         a = f(*args, **keywords)
         telemetry.count('external.http.status.%ixx' % floor(a.status_code / 100))
         return a
     except:
         telemetry.count('external.http.errors')
         raise
     finally:
         elapsed, _ = Timing.pop_timer()
         telemetry.record('external.http.response.latency', elapsed)
Exemplo n.º 16
0
 def complex_wrapper(func, *args, **keywords):
     if _should_be_instrumented(state, enable_if, disable_if):
         Timing.push_timer()
         context.push_state(state)
         try:
             return func(*args, **keywords)
         finally:
             elapsed, _ = Timing.pop_timer()
             count(metric + 'requests', reporter=reporter)
             record(metric + 'latency', elapsed, reporter=reporter)
             context.pop_state(state)
     else:
         return func(*args, **keywords)
Exemplo n.º 17
0
def _manage_workers(func, *args, **keywords):
    response = func(*args, **keywords)

    try:
        arbiter = args[0]
        telemetry.record('gunicorn.workers',
                         arbiter._last_logged_active_worker_count,
                         is_timer=False,
                         reporter='gunicorn')
    except Exception as e:
        logger.exception(
            "Instrumentation error while reporting gunicorn.workers")

    return response
Exemplo n.º 18
0
 def decorator(*args, **keywords):
     telemetry.count('external.http.requests')
     Timing.push_timer()
     try:
         a = f(*args, **keywords)
         telemetry.count('external.http.status.%ixx' %
                         floor(a.status_code / 100))
         return a
     except:
         telemetry.count('external.http.errors')
         raise
     finally:
         elapsed, _ = Timing.pop_timer()
         telemetry.record('external.http.response.latency', elapsed)
Exemplo n.º 19
0
def _wrapped_call(metric, func, *args, **keywords):
    """ Times and executes arbitrary method """
    state = 'external'

    if not _should_be_instrumented(state, enable_if='web', disable_if='model'):
        return func(*args, **keywords)

    Timing.push_timer()
    try:
        context.push_state(state)
        return func(*args, **keywords)
    finally:
        context.pop_state(state)
        elapsed, _ = Timing.pop_timer()
        telemetry.record(metric, elapsed)
Exemplo n.º 20
0
def _wrapped_call(metric, func, *args, **keywords):
    """ Times and executes arbitrary method """
    state = 'external'

    if not _should_be_instrumented(state, enable_if='web', disable_if='model'):
        return func(*args, **keywords)

    Timing.push_timer()
    try:
        context.push_state(state)
        return func(*args, **keywords)
    finally:
        context.pop_state(state)
        elapsed, _ = Timing.pop_timer()
        telemetry.record(metric, elapsed)
Exemplo n.º 21
0
    def decorator(*args, **keywords):
        try:
            from flask import request
            route = request.url_rule.rule if request.url_rule else None
            context.push_tag('web.route', route)
            context.push_tag('web.method', request.method)
            telemetry.count('web.requests')
            Timing.push_timer()

            return f(*args, **keywords)
        finally:
            elapsed, net_elapsed = Timing.pop_timer()
            telemetry.record('web.response.latency', elapsed)
            telemetry.record('app.response.latency', net_elapsed)
            context.pop_tag()
            context.pop_tag()
Exemplo n.º 22
0
 def decorator(*args, **keywords):
     method = get_parameter(0, 'method', *args, **keywords)
     url = get_parameter(1, 'url', *args, **keywords)
     with context.add_all_tags([('external.url', url), ('external.method', method)]):
         telemetry.count('external.http.requests')
         Timing.push_timer()
         try:
             a = f(*args, **keywords)
             telemetry.count('external.http.status.%ixx' % floor(a.status_code / 100))
             return a
         except:
             telemetry.count('external.http.errors')
             raise
         finally:
             elapsed, _ = Timing.pop_timer()
             telemetry.record('external.http.response.latency', elapsed)
Exemplo n.º 23
0
def _session_send_wrapper(func, *args, **keywords):
    if not _should_be_instrumented(state="external", enable_if="web", disable_if="model"):
        return func(*args, **keywords)

    telemetry.count("external.http.requests")
    Timing.push_timer()
    try:
        context.push_state("external")
        a = func(*args, **keywords)
        telemetry.count("external.http.status.%ixx" % floor(a.status_code / 100))
        return a
    except:
        telemetry.count("external.http.errors")
        raise
    finally:
        context.pop_state("external")
        elapsed, _ = Timing.pop_timer()
        telemetry.record("external.http.response.latency", elapsed)
Exemplo n.º 24
0
def _session_send_wrapper(func, *args, **keywords):
    if not _should_be_instrumented(
            state='external', enable_if='web', disable_if='model'):
        return func(*args, **keywords)

    telemetry.count('external.http.requests')
    Timing.push_timer()
    try:
        context.push_state('external')
        a = func(*args, **keywords)
        telemetry.count('external.http.status.%ixx' %
                        floor(a.status_code / 100))
        return a
    except:
        telemetry.count('external.http.errors')
        raise
    finally:
        context.pop_state('external')
        elapsed, _ = Timing.pop_timer()
        telemetry.record('external.http.response.latency', elapsed)
Exemplo n.º 25
0
    def decorator(*args, **keywords):
        try:
            telemetry.count('web.requests')
            Timing.push_timer()

            # call the request function
            response = f(*args, **keywords)

            if response.status:
                telemetry.count('web.status.%sxx' % response.status[0:1])
            return response
        except Exception as e:
            telemetry.count('web.errors')
            raise e
        finally:
            try:
                elapsed, net_elapsed = Timing.pop_timer()
                telemetry.record('web.response.latency', elapsed)
                telemetry.record('app.response.latency', net_elapsed)
            except:
                logger.exception('Teardown handler failed')
                raise
Exemplo n.º 26
0
def record(metric, value):
    """
    Records a given value as a data point for the given metric at the current timestamp.

    The current of the context stack is included.

    Example
        telemetry.record('maxHeap', max_heap_size)

    :param metric: the given metric name
    :param value: the value to be recorded
    """
    return telemetry.record(metric, value)
Exemplo n.º 27
0
def record(metric, value):
    """
    Records a given value as a data point for the given metric at the current timestamp.

    The current of the context stack is included.

    Example
        telemetry.record('maxHeap', max_heap_size)

    :param metric: the given metric name
    :param value: the value to be recorded
    """
    return telemetry.record(metric, value)
Exemplo n.º 28
0
    def decorator(*args, **keywords):
        try:
            telemetry.count('web.requests')
            Timing.push_timer()

            # call the request function
            response = f(*args, **keywords)

            if response.status:
                telemetry.count('web.status.%sxx' % response.status[0:1])
            return response
        except Exception as e:
            telemetry.count('web.errors')
            raise e
        finally:
            try:
                elapsed, net_elapsed = Timing.pop_timer()
                telemetry.record('web.response.latency', elapsed)
                telemetry.record('app.response.latency', net_elapsed)
            except:
                logger.exception('Teardown handler failed')
                raise
Exemplo n.º 29
0
    def decorator(*args, **keywords):
        # The type of the returned instance depends on the url
        # but is typically urllib.addinfourl for the baked-in protocols

        if context.has_state('external'):
            # Avoid double counting nested calls. All metrics will be reported
            # relative to the outermost operation
            return f(*args, **keywords)

        url = get_parameter(1, 'fullurl', *args, **keywords)

        if hasattr(url, 'get_full_url'):
            url = url.get_full_url()

        scheme = url.split(':')[0] if ':' in url else 'unknown'

        Timing.push_timer()
        try:
            context.push_state('external')
            telemetry.count('external.{}.requests'.format(scheme))
            a = f(*args, **keywords)
            if a.getcode():
                # Not meaningful for ftp etc
                telemetry.count('external.{}.status.%ixx'.format(scheme) %
                                floor(a.getcode() / 100))
        except:
            telemetry.count('external.{}.errors'.format(scheme))
            raise
        finally:
            context.pop_state('external')
            elapsed, _ = Timing.pop_timer()
            telemetry.record('external.{}.response.latency'.format(scheme),
                             elapsed)

        # Return a wrapped object so we can time subsequent read, readline etc calls
        return _response_wrapper(scheme, a)
Exemplo n.º 30
0
 def process_view(self, request, view_func, view_args, view_kwargs):
     if self.is_active:
         telemetry.record("web.view.latency", time.time() - self.is_active)
Exemplo n.º 31
0
 def process_view(self, request, view_func, view_args, view_kwargs):
     if self.is_active:
         telemetry.record('web.view.latency', time.time() - self.is_active)