def test_ignore(): ScoutConfig.set(ignore=["/health"]) assert ignore_path("/health") assert ignore_path("/health/foo") assert not ignore_path("/users") ScoutConfig.reset_all()
def test_ignore_multiple_prefixes(): ScoutConfig.set(ignore=["/health", "/api"]) assert ignore_path("/health") assert ignore_path("/health/foo") assert ignore_path("/api") assert ignore_path("/api/foo") assert not ignore_path("/users") ScoutConfig.reset_all()
def scout_tween(request): try: tr = TrackedRequest.instance() span = tr.start_span(operation="Controller/Pyramid") if ignore_path(request.path): tr.tag("ignore_transaction", True) # Capture what we can from the request, but never fail try: Context.add("path", request.path) Context.add("user_ip", request.remote_addr) except Exception: pass try: response = handler(request) except Exception: tr.tag("error", "true") raise # This happens further down the call chain. So time it starting # above, but only name it if it gets to here. if request.matched_route is not None: tr.mark_real_request() span.operation = "Controller/" + request.matched_route.name finally: tr.stop_span() return response
def tracing_function(original, *args, **kwargs): entry_type, detail = info operation = entry_type if detail["name"] is not None: operation = operation + "/" + detail["name"] tr = TrackedRequest.instance() tr.mark_real_request() span = tr.start_span(operation=operation) for key in detail: span.tag(key, detail[key]) if ignore_path(detail.get("path", "")): tr.tag("ignore_transaction", True) # And the custom View stuff # request = args[0] # Extract headers # regex = re.compile('^HTTP_') # headers = dict((regex.sub('', header), value) for (header, value) # in request.META.items() if header.startswith('HTTP_')) # span.tag('remote_addr', request.META['REMOTE_ADDR']) try: return original(*args, **kwargs) except Exception as e: TrackedRequest.instance().tag("error", "true") raise e finally: TrackedRequest.instance().stop_span()
def scout_tween(request): tracked_request = TrackedRequest.instance() span = tracked_request.start_span(operation="Controller/Pyramid") try: if ignore_path(request.path): tracked_request.tag("ignore_transaction", True) tracked_request.tag("path", request.path) try: # Determine a remote IP to associate with the request. The value is # spoofable by the requester so this is not suitable to use in any # security sensitive context. user_ip = (request.headers.get("x-forwarded-for", default="").split(",")[0] or request.headers.get("client-ip", default="").split(",")[0] or request.remote_addr) except Exception: pass else: tracked_request.tag("user_ip", user_ip) try: queue_time = request.headers.get( "x-queue-start", default="") or request.headers.get( "x-request-start", default="") except Exception: pass else: track_request_queue_time(queue_time, tracked_request) try: response = handler(request) except Exception: tracked_request.tag("error", "true") raise # This happens further down the call chain. So time it starting # above, but only name it if it gets to here. if request.matched_route is not None: tracked_request.mark_real_request() span.operation = "Controller/" + request.matched_route.name finally: tracked_request.stop_span() return response
def wrapper(*args, **kwargs): tracked_request = TrackedRequest.instance() tracked_request.mark_real_request() if request.route.name is not None: path = request.route.name else: path = request.route.rule if path == "/": path = "/home" if not path.startswith("/"): path = "/{}".format(path) tracked_request.tag("path", path) if ignore_path(path): tracked_request.tag("ignore_transaction", True) tracked_request.start_span(operation="Controller{}".format(path)) try: # Determine a remote IP to associate with the request. The # value is spoofable by the requester so this is not suitable # to use in any security sensitive context. user_ip = ( request.headers.get("x-forwarded-for", "").split(",")[0] or request.headers.get("client-ip", "").split(",")[0] or request.environ.get("REMOTE_ADDR")) except Exception: pass else: tracked_request.tag("user_ip", user_ip) try: queue_time = request.headers.get("x-queue-start", "") or request.headers.get( "x-request-start", "") except Exception: pass else: track_request_queue_time(queue_time, tracked_request) try: return callback(*args, **kwargs) except Exception: tracked_request.tag("error", "true") raise finally: tracked_request.stop_span()
def wrapped_dispatch_request(self, original, *args, **kwargs): request = _request_ctx_stack.top.request # Copied logic from Flask if request.routing_exception is not None: return original(*args, **kwargs) rule = request.url_rule view_func = self.app.view_functions[rule.endpoint] path = request.path name = view_func.__module__ + "." + view_func.__name__ operation = "Controller/" + name tracked_request = TrackedRequest.instance() tracked_request.mark_real_request() span = tracked_request.start_span(operation=operation) span.tag("path", path) span.tag("name", name) if ignore_path(path): tracked_request.tag("ignore_transaction", True) # Determine a remote IP to associate with the request. The value is # spoofable by the requester so this is not suitable to use in any # security sensitive context. user_ip = ( request.headers.get("x-forwarded-for", default="").split(",")[0] or request.headers.get("client-ip", default="").split(",")[0] or request.remote_addr ) tracked_request.tag("user_ip", user_ip) queue_time = request.headers.get( "x-queue-start", default="" ) or request.headers.get("x-request-start", default="") track_request_queue_time(queue_time, tracked_request) try: return original(*args, **kwargs) except Exception as exc: tracked_request.tag("error", "true") raise exc finally: tracked_request.stop_span()
def process_request(self, req, resp): tracked_request = TrackedRequest.instance() tracked_request.mark_real_request() req.context.scout_tracked_request = tracked_request tracked_request.tag("path", req.path) if ignore_path(req.path): tracked_request.tag("ignore_transaction", True) # Determine a remote IP to associate with the request. The value is # spoofable by the requester so this is not suitable to use in any # security sensitive context. user_ip = ( req.get_header("x-forwarded-for", default="").split(",")[0] or req.get_header("client-ip", default="").split(",")[0] or req.remote_addr ) tracked_request.tag("user_ip", user_ip)
def process_view(self, request, view_func, view_args, view_kwargs): """ Capture details about the view_func that is about to execute """ try: if ignore_path(request.path): TrackedRequest.instance().tag("ignore_transaction", True) view_name = request.resolver_match._func_path span = TrackedRequest.instance().current_span() if span is not None: span.operation = "Controller/" + view_name Context.add("path", request.path) Context.add("user_ip", RemoteIp.lookup_from_headers(request.META)) if getattr(request, "user", None) is not None: Context.add("username", request.user.get_username()) except Exception: pass
def track_request_view_data(request, tracked_request): tracked_request.tag("path", request.path) if ignore_path(request.path): tracked_request.tag("ignore_transaction", True) try: # Determine a remote IP to associate with the request. The value is # spoofable by the requester so this is not suitable to use in any # security sensitive context. user_ip = (request.META.get("HTTP_X_FORWARDED_FOR", "").split(",")[0] or request.META.get("HTTP_CLIENT_IP", "").split(",")[0] or request.META.get("REMOTE_ADDR", None)) tracked_request.tag("user_ip", user_ip) except Exception: pass user = getattr(request, "user", None) if user is not None: try: tracked_request.tag("username", user.get_username()) except Exception: pass
def wrapper(*args, **kwargs): try: tr = TrackedRequest.instance() tr.mark_real_request() path = "Unknown" if request.route.name is not None: path = request.route.name else: path = request.route.rule if path == "/": path = "/home" if not path.startswith("/"): path = "/{}".format(path) tr.start_span(operation="Controller{}".format(path)) if ignore_path(path): tr.tag("ignore_transaction", True) try: Context.add("path", path) Context.add("user_ip", request.remote_addr) except Exception: pass try: response = callback(*args, **kwargs) except Exception: tr.tag("error", "true") raise finally: tr.stop_span() return response
def process_view(self, request, view_func, view_func_args, view_func_kwargs): tr = TrackedRequest.instance() if ignore_path(request.path): tr.tag("ignore_transaction", True) view_name = request.resolver_match._func_path operation = "Controller/" + view_name span = tr.start_span(operation=operation) tr.mark_real_request() # Save the span into the request, so we can check # if we're matched up when stopping request.scout_view_span = span try: if getattr(request, "user", None) is not None: Context.add("username", request.user.get_username()) except Exception: pass return None