Example #1
0
def _do_method_trace(wrapped, instance, args, kwargs):
    tracker = obtain_tracker_from_request(instance.request)
    if not tracker:
        return wrapped(*args, **kwargs)

    # 用户可能会重写RequestHandler中的_excute方法, 导致无法正确抓取metricName
    name = object_name(wrapped)
    http_methods = name.split(".")
    if len(http_methods) >= 2 and http_methods[-1] == instance.request.method.lower():
        tracker.set_tracker_name(name, priority=3)

    with FunctionTracker(tracker, name=name):
        return wrapped(*args, **kwargs)
Example #2
0
def trace_handler_execute(wrapped, instance, args, kwargs):
    """
    """
    handler = instance
    request = handler.request

    if not request:
        console.warning("No request instance got from handler. this maybe agent potential design issues. "
                        "if this continue, please report to us.")
        return wrapped(*args, **kwargs)

    tracker = obtain_tracker_from_request(request)
    if not tracker:
        return wrapped(*args, **kwargs)

    if request.method not in handler.SUPPORTED_METHODS:
        name = object_name(wrapped)
    else:
        name = object_name(getattr(handler, request.method.lower()))

    tracker.set_tracker_name(name, priority=3)
    with TrackerTransferContext(tracker):
        return wrapped(*args, **kwargs)
Example #3
0
    def tracker_aware(wrapped, instance, args, kwargs):
        # Variables from the outer scope are not assignable in a closure, so we use a mutable object to hold the
        # tracker, so we can change it if we need to.
        inner_tracker = tracker[0]

        if inner_tracker is not None:
            # Callback run outside the main thread must not affect the cache
            if inner_tracker.thread_id != current_thread_id():
                return fxn(*args, **kwargs)

        if inner_tracker is not None and inner_tracker._is_finalized:
            inner_tracker = None
            tracker[0] = None

        with TrackerTransferContext(inner_tracker):
            if inner_tracker is None:
                # A tracker will be None for fxns scheduled on the ioloop not associated with a tracker.
                ret = fxn(*args, **kwargs)
            elif should_trace is False:
                try:
                    ret = fxn(*args, **kwargs)
                except:
                    record_exception(sys.exc_info())
                    wrapped._nb_recorded_exception = True
                    raise

            else:
                name = object_name(fxn_for_name)
                with FunctionTracker(inner_tracker, name=name) as ft:

                    try:
                        ret = fxn(*args, **kwargs)
                    except:
                        record_exception(sys.exc_info())
                        wrapped._nb_recorded_exception = True
                        raise
                    if ft is not None and ret is not None and hasattr(
                            ret, '_nb_coroutine_name'):
                        ft.name = ret._nb_coroutine_name

                        if type(ret) == NoneProxy:
                            ret = None

        if inner_tracker and inner_tracker._ref_count == 0:
            finish_tracker(inner_tracker)

        return ret
def trace_server_request_init_(wrapped, request, args, kwargs):
    """agent tracer entrance in tornado, will be called  after header but body. Three situations will be called.
        1. a common web.Application instance.
        2.instance passed into httpserver.HTTPServer is a not tornado application.
        3. In wsgi module. some WSGI applications.

    :param request: a `request` Instance of current user request.
    """
    result = wrapped(*args, **kwargs)

    tracker = None if is_websocket(request) else generate_tracer(request)
    if tracker:
        tracker.set_tracker_name(object_name(wrapped), priority=1)
        tracker.start_time = getattr(request, "_start_time", int(time.time()))

    request._nb_tracker = tracker
    return result
Example #5
0
 def _coroutine_name(func):
     return '%s %s' % (object_name(func), '(coroutine)')