def _increment_ref_count(callback, wrapped, instance, args, kwargs): tracker = obtain_current_tracker() if hasattr(callback, '_self_tracker'): if callback._self_tracker is not None: if current_thread_id() != callback._self_tracker.thread_id: # Callback being added not in the main thread; ignore. # Since we are not incrementing the counter for this callback, # we need to remove the tracker from the callback, so it # doesn't get decremented either. callback._self_tracker = None return wrapped(*args, **kwargs) if tracker is not callback._self_tracker: console.warning( "Attempt to add callback to ioloop with different tracer attached than in the cache." "if this continue, Please report to us.") callback._self_tracker = None return wrapped(*args, **kwargs) if tracker is None: return wrapped(*args, **kwargs) tracker._ref_count += 1 return wrapped(*args, **kwargs)
def trace_runner_init(wrapped, instance, args, kwargs): """ """ tracker = obtain_current_tracker() if tracker is None: return wrapped(*args, **kwargs) try: frame = get_frame(1) except ValueError: console.debug( 'tornado.gen.Runner is being created at the top of the stack. .') return wrapped(*args, **kwargs) max_frame_depth = 5 frame_depth = 1 while frame and frame_depth <= max_frame_depth: if frame.f_globals['__name__'] == 'tornado.gen': break frame = frame.f_back frame_depth += 1 def _coroutine_name(func): return '%s %s' % (callable_name(func), '(coroutine)') if '__name__' in frame.f_globals and frame.f_globals['__name__'] == 'tornado.gen' and \ 'func' in frame.f_locals and 'replace_callback' in frame.f_locals and frame.f_code.co_name == 'wrapper': instance._self_coroutine_name = _coroutine_name(frame.f_locals['func']) tracker._ref_count += 1 return wrapped(*args, **kwargs)
def trace_add_done_callback(wrapped, instance, args, kwargs): """ """ def _fxn_arg_extractor(fn, *args, **kwargs): return fn fxn = _fxn_arg_extractor(*args, **kwargs) should_trace = not hasattr(fxn, '_previous_object') tracker_aware_fxn = create_tracker_aware_fxn(fxn, should_trace=should_trace) # If tracker_aware_fxn is None then it is already wrapped, or the fxn is None. if tracker_aware_fxn is None: return wrapped(*args, **kwargs) tracker = obtain_current_tracker() tracker_aware_fxn._nb_tracker = tracker # We replace the function we call in the callback with the tracker aware version of the function. if len(args) > 0: args = list(args) args[0] = tracker_aware_fxn else: kwargs['fn'] = tracker_aware_fxn return wrapped(*args, **kwargs)
def trace_add_done_callback(wrapped, instance, args, kwargs): """ """ def _fxn_arg_extractor(fn, *args, **kwargs): return fn fxn = _fxn_arg_extractor(*args, **kwargs) should_trace = callable_name(fxn) not in fxn_cache tracker_aware_fxn = create_tracker_aware_fxn(fxn, should_trace=should_trace) if should_trace: fxn_cache.append(callable_name(fxn)) # If tracker_aware_fxn is None then it is already wrapped, or the fxn is None. if tracker_aware_fxn is None: return wrapped(*args, **kwargs) tracker = obtain_current_tracker() tracker_aware_fxn._self_tracker = tracker # We replace the function we call in the callback with the tracker aware version of the function. if len(args) > 0: args = list(args) args[0] = tracker_aware_fxn else: kwargs['fn'] = tracker_aware_fxn return wrapped(*args, **kwargs)
def trace_tornado_http_request(wrapped, instance, args, kwargs): """ """ tracker = obtain_current_tracker() if not tracker: return wrapped(*args, **kwargs) _url = parse_request_url(*args, **kwargs) with ExternalTrace(tracker, "httpclient", _url): return wrapped(*args, **kwargs)
def _do_wrap_decorated(wrapped, instance, args, kwargs): """ """ tracker = obtain_current_tracker() if tracker is None: return wrapped(*args, **kwargs) with FunctionTracker(tracker, name=callable_name(wrapped)): return wrapped(*args, **kwargs)
def trace_http_headers_init_(wrapped, instance, args, kwargs): """ :return: """ result = wrapped(*args, **kwargs) tracker = obtain_current_tracker() if tracker and hasattr(instance, 'X-Tingyun-Tx-Data'): tracker._called_traced_data = eval( instance.headers.get("X-Tingyun-Tx-Data", '{}')) process_cross_trace(instance) return result
def trace_wrap(wrapped, instance, args, kwargs): """ """ def _fxn_arg_extractor(fn, *args, **kwargs): return fn unwrapped_fxn = _fxn_arg_extractor(*args, **kwargs) wrapped_fxn = wrapped(*args, **kwargs) should_trace = not hasattr(unwrapped_fxn, '_previous_object') tracker_aware_fxn = create_tracker_aware_fxn(wrapped_fxn, fxn_for_name=unwrapped_fxn, should_trace=should_trace) if tracker_aware_fxn is None: return wrapped_fxn tracker_aware_fxn._wrapped = True tracker_aware_fxn._nb_tracker = obtain_current_tracker() return tracker_aware_fxn
def trace_runner_run(wrapped, instance, args, kwargs): """ """ result = wrapped(*args, **kwargs) tracker = obtain_current_tracker() if tracker is None: return result if result is None: result = NoneProxy() if hasattr(instance, '_self_coroutine_name' ) and instance._self_coroutine_name is not None: result._self_coroutine_name = instance._self_coroutine_name if instance.finished: tracker._ref_count -= 1 return result
def trace_wrap(wrapped, instance, args, kwargs): """ """ def _fxn_arg_extractor(fn, *args, **kwargs): return fn unwrapped_fxn = _fxn_arg_extractor(*args, **kwargs) wrapped_fxn = wrapped(*args, **kwargs) should_trace = callable_name(unwrapped_fxn) not in fxn_cache if should_trace: fxn_cache.append(callable_name(unwrapped_fxn)) tracker_aware_fxn = create_tracker_aware_fxn(wrapped_fxn, fxn_for_name=unwrapped_fxn, should_trace=should_trace) if tracker_aware_fxn is None: return wrapped_fxn tracker_aware_fxn._wrapped = True tracker_aware_fxn._self_tracker = obtain_current_tracker() return tracker_aware_fxn
def _increment_ref_count(callback, wrapped, instance, args, kwargs): tracker = obtain_current_tracker() if getattr(callback, '_nb_tracker', None): if current_thread_id() != callback._nb_tracker.thread_id: callback._nb_tracker = None return wrapped(*args, **kwargs) if tracker is not callback._nb_tracker: console.warning("Attempt to add callback to ioloop with different tracer attached than in the cache." "if this continue, Please report to us.") callback._nb_tracker = None return wrapped(*args, **kwargs) if tracker is None: return wrapped(*args, **kwargs) tracker._ref_count += 1 return wrapped(*args, **kwargs)
def trace_http_fetch_callback(wrapped, instance, args, kwagrs): """不捕获其执行时间,仅用于获取http传回的跨应用数据 :return: """ tracker = obtain_current_tracker() if not tracker: console.debug( "Do not get tracker from current thread, this's maybe finish before." ) def parse_reponse(response, *_args, **_kwargs): """http client(不论是异步还是同步) fetch 发生调用时, 会调用handle_response(response)作为回调函数 response 为 HTTPResponse 实例 :return: """ return response, _args, _kwargs response, _args, _kwargs = parse_reponse(*args, **kwagrs) if hasattr(response, 'headers'): tracker._called_traced_data = eval( response.headers.get("X-Tingyun-Tx-Data", '{}')) return wrapped(response, _args, _kwargs)
def _do_method_trace(wrapped, instance, args, kwargs): tracker = obtain_current_tracker() with FunctionTracker(tracker, name=callable_name(wrapped)): return wrapped(*args, **kwargs)