Exemplo n.º 1
0
    async def __call__(self, scope, receive, send):
        """Implements the ASGI protocol.

        See details at:
            https://asgi.readthedocs.io/en/latest/specs/index.html.
        """

        assert scope["type"] == "http"

        # only use the non-root part of the path for routing
        root_path = scope["root_path"]
        route_path = scope["path"][len(root_path) :]

        self.request_counter.inc(tags={"route": route_path})

        if route_path == "/-/routes":
            return await starlette.responses.JSONResponse(self.route_info)(
                scope, receive, send
            )

        if route_path == "/-/healthz":
            return await starlette.responses.PlainTextResponse("success")(
                scope, receive, send
            )

        route_prefix, handle = self.prefix_router.match_route(route_path)
        if route_prefix is None:
            self.request_error_counter.inc(
                tags={"route": route_path, "error_code": "404"}
            )
            return await self._not_found(scope, receive, send)

        # Modify the path and root path so that reverse lookups and redirection
        # work as expected. We do this here instead of in replicas so it can be
        # changed without restarting the replicas.
        if route_prefix != "/":
            assert not route_prefix.endswith("/")
            scope["path"] = route_path.replace(route_prefix, "", 1)
            scope["root_path"] = root_path + route_prefix

        start_time = time.time()
        status_code = await _send_request_to_handle(handle, scope, receive, send)
        latency_ms = (time.time() - start_time) * 1000.0
        logger.info(
            access_log_msg(
                method=scope["method"],
                route=route_prefix,
                status=str(status_code),
                latency_ms=latency_ms,
            )
        )
        if status_code != "200":
            self.request_error_counter.inc(
                tags={"route": route_path, "error_code": status_code}
            )
            self.deployment_request_error_counter.inc(
                tags={"deployment": handle.deployment_name}
            )
Exemplo n.º 2
0
    async def handle_request(self, request: Query) -> asyncio.Future:
        async with self.rwlock.reader_lock:
            num_running_requests = self._get_handle_request_stats()["running"]
            self.num_processing_items.set(num_running_requests)

            start_time = time.time()
            result, success = await self.invoke_single(request)
            latency_ms = (time.time() - start_time) * 1000

            self.processing_latency_tracker.observe(latency_ms)

            logger.info(
                access_log_msg(
                    method="HANDLE",
                    route=request.metadata.call_method,
                    status="OK" if success else "ERROR",
                    latency_ms=latency_ms,
                ))

            # Returns a small object for router to track request status.
            return b"", result