def generate_tracer(request, framework='Tornado'): """ :param request: the http request for client :param framework: framework name :return: """ old_tracker = drop_current_tracer() tracker = Tracer(proxy_instance(), request_environ({}, request), framework) if not tracker.enabled: return None tracker.start_work() drop_current_tracer() # drop it from thread cache tracker._nb_request = weakref.ref(request) request._nb_tracker = tracker replace_current_tracer(old_tracker) return tracker
def generate_tracer(request, framework='Tornado'): """ :param request: the http request for client :return: """ tracer = Tracer(proxy_instance(), request_environ({}, request), framework) if not tracer.enabled: # console.debug("Agent not prepared, skip trace this request now.") return None try: tracer.start_work() tracer.drop_tracker() # drop it from thread cache request._self_request_finished = False request._self_tracer = tracer request._self_async_function_tracker = None except Exception as err: stop_request_tracer( request, *(sys.exc_info() + ("generate-tracker-exception", ))) console.exception( "Error occurred, when generate a tracker for tornado. %s", err) raise return tracer
def retrieve_tracker(headers, vendor='RabbitMQ'): """消费者调用这个接口,获取tracker,如果没有就自动生成一个并强制转换WebAction :return: tracker is_web """ # 如果未webAction则跳过,只作为其附加组件 tracker = current_tracker() if tracker: return tracker, True environ = headers if not headers: environ = {} tracker = Tracer(proxy_instance(), environ, vendor) tracker.start_work() if tracker.settings and not tracker.settings.mq.enabled: global_settings.enabled = False console.debug("Vendor %s consumer not enabled. Disable agent now", vendor) return None, False return tracker, False
def wrapper(wrapped, instance, args, kwargs): """More detail about the argument, read the wrapper doc """ tracker = current_tracker() if tracker: return wrapped(*args, **kwargs) environ, start_response = parse_wsgi_protocol(target, *args, **kwargs) disable_agent = environ.get(AGENT_REQUEST_SWITCH, None) if disable_agent: console.debug( "Current trace is disabled with http request environment. %s", environ) return wrapped(*args, **kwargs) tracker = Tracer(proxy_instance(), environ, framework) tracker.start_work() # respect the wsgi protocol def _start_response(status, response_headers, *args): # deal the response header/data process_header(tracker, response_headers) tracker.deal_response(status, response_headers, *args) _write = start_response(status, response_headers, *args) return _write result = [] try: tracker.set_tracker_name(callable_name(wrapped), priority=1) application = function_trace_wrapper(wrapped) with FunctionTracker(tracker, name='Application', group='Python.WSGI'): result = TingYunWSGIBrowserRumMiddleware( tracker, application, _start_response, environ)() # result = application(environ, start_response) except: tracker.finish_work(*sys.exc_info()) raise return WSGIApplicationResponse(tracker, result)
def generate_tracer(request, framework='Tornado'): """ :param request: the http request for client :return: """ drop_current_tracer() tracer = Tracer(proxy_instance(), request_environ({}, request), framework) if not tracer.enabled: return None tracer.start_work() drop_current_tracer() # drop it from thread cache tracer._nb_request = weakref.ref(request) # when 'finish' or 'on_connection_close' is called on server requests. tracer should be finished trace. tracer._is_finalized = False tracer._ref_count = 0 # count the reference. tracer._can_finalize = False return tracer