Skip to content

fix: log middleware is not working correctly

Shinya Maeda requested to merge fix-exceptions-silently-fallback-200 into main

This is a high priority MR for Switch to Chat Agent V2 (gitlab-org#13533 - closed). Please prioritize the review and merge.

What does this merge request do and why?

This MR fixes our Access Log middleware that was not working correctly. As starlette's official documentation suggests, we use Pure ASGI Middleware instead of BaseHTTPMiddleware that is obsolete in favor of the former option.

There are no changes on the business logic. The information is same and now it has the correct values.

Related to Log error status of v2 chat agent correctly (#638 - closed)

How to set up and validate locally

  1. Apply the following patch to intentionally raise an exception during the process:
diff --git a/ai_gateway/chat/agents/react.py b/ai_gateway/chat/agents/react.py
index b274e666..a29ec4de 100644
--- a/ai_gateway/chat/agents/react.py
+++ b/ai_gateway/chat/agents/react.py
@@ -235,6 +235,7 @@ class ReActAgent(Prompt[ReActAgentInputs, TypeAgentEvent]):
         len_final_answer = 0
 
         async for event in astream:
+            raise ValueError("test error")
             if is_feature_enabled(FeatureFlag.EXPANDED_AI_LOGGING):
                 log.info("Response streaming", source=__name__, streamed_event=event)
 
  1. Request to the API.
curl -iv --raw -X 'POST' \
  'http://localhost:5052/v2/chat/agent' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "prompt": "Hi, how are you?",
  "options": {
    "chat_history": "history",
    "agent_scratchpad": {
      "agent_type": "react",
      "steps": []
    }
  }
}'

Before

{
  "url": "http://localhost:5052/v2/chat/agent",
  "path": "/v2/chat/agent",
  "status_code": 200,
  "method": "POST",
  "correlation_id": "1c708bb37c784adf8efee90f11930d04",
  "http_version": "1.1",
  "client_ip": "127.0.0.1",
  "client_port": 54052,
  "duration_s": 0.06897568400017917,
  "duration_request": -1,
  "cpu_s": 0.06904113399999989,
  "user_agent": "curl/7.81.0",
  "gitlab_language_server_version": null,
  "gitlab_instance_id": null,
  "gitlab_global_user_id": null,
  "gitlab_host_name": null,
  "gitlab_version": null,
  "gitlab_saas_duo_pro_namespace_ids": null,
  "gitlab_saas_namespace_ids": null,
  "gitlab_realm": null,
  "gitlab_duo_seat_count": null,
  "enabled_feature_flags": null,
  "meta.feature_category": "duo_chat",
  "logger": "api.access",
  "level": "info",
  "type": "mlops",
  "stage": "main",
  "timestamp": "2024-09-18T08:57:13.198730Z",
  "message": "127.0.0.1:54052 - \"POST /v2/chat/agent HTTP/1.1\" 200"
}

After

{
  "url": "http://localhost:5052/v2/chat/agent",
  "path": "/v2/chat/agent",
  "status_code": 200,
  "method": "POST",
  "correlation_id": "08b0cbe68d7c43f18e985ad7e5cc1c63",
  "http_version": "1.1",
  "client_ip": "127.0.0.1",
  "client_port": 48110,
  "duration_s": 1.3957735369995135,
  "duration_request": -1,
  "cpu_s": 0.017841350999999506,
  "user_agent": "curl/7.81.0",
  "gitlab_language_server_version": null,
  "gitlab_instance_id": null,
  "gitlab_global_user_id": null,
  "gitlab_host_name": null,
  "gitlab_version": null,
  "gitlab_saas_duo_pro_namespace_ids": null,
  "gitlab_saas_namespace_ids": null,
  "gitlab_realm": null,
  "gitlab_duo_seat_count": null,
  "enabled_feature_flags": null,
  "meta.feature_category": "duo_chat",
  "exception.message": "hoge",
  "exception.class": "ValueError",
  "exception.backtrace": "  + Exception Group Traceback (most recent call last):\n  |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 192, in __call__\n  |     await response(scope, wrapped_receive, send)\n  |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 258, in __call__\n  |     async with anyio.create_task_group() as task_group:\n  |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py\", line 680, in __aexit__\n  |     raise BaseExceptionGroup(\n  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)\n  +-+---------------- 1 ----------------\n    | Exception Group Traceback (most recent call last):\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 261, in wrap\n    |     await func()\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 217, in stream_response\n    |     return await super().stream_response(send)\n    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 250, in stream_response\n    |     async for chunk in self.body_iterator:\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 181, in body_stream\n    |     raise app_exc\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 151, in coro\n    |     await self.app(scope, receive_or_disconnect, send_no_error)\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/exceptions.py\", line 65, in __call__\n    |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py\", line 64, in wrapped_app\n    |     raise exc\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py\", line 53, in wrapped_app\n    |     await app(scope, receive, sender)\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/routing.py\", line 756, in __call__\n    |     await self.middleware_stack(scope, receive, send)\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/routing.py\", line 776, in app\n    |     await route.handle(scope, receive, send)\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/routing.py\", line 297, in handle\n    |     await self.app(scope, receive, send)\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/routing.py\", line 77, in app\n    |     await wrap_app_handling_exceptions(app, request)(scope, receive, send)\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py\", line 64, in wrapped_app\n    |     raise exc\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py\", line 53, in wrapped_app\n    |     await app(scope, receive, sender)\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/routing.py\", line 75, in app\n    |     await response(scope, receive, send)\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 258, in __call__\n    |     async with anyio.create_task_group() as task_group:\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py\", line 680, in __aexit__\n    |     raise BaseExceptionGroup(\n    | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)\n    +-+---------------- 1 ----------------\n      | Traceback (most recent call last):\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/middleware.py\", line 125, in __call__\n      |     await self.app(scope, receive, send_wrapper)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/middleware.py\", line 410, in __call__\n      |     await self.app(scope, receive, send)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/authentication.py\", line 49, in __call__\n      |     await self.app(scope, receive, send)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/middleware.py\", line 427, in __call__\n      |     await self.app(scope, receive, send)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/middleware.py\", line 334, in __call__\n      |     await self.app(scope, receive, send)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 189, in __call__\n      |     with collapse_excgroups():\n      |   File \"/home/shinya/.asdf/installs/python/3.11.10/lib/python3.11/contextlib.py\", line 158, in __exit__\n      |     self.gen.throw(typ, value, traceback)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/_utils.py\", line 93, in collapse_excgroups\n      |     raise exc\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 261, in wrap\n      |     await func()\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 250, in stream_response\n      |     async for chunk in self.body_iterator:\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/v2/chat/agent.py\", line 56, in _stream_handler\n      |     async for event in stream_events:\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/chat/executor.py\", line 66, in stream\n      |     async for action in agent.astream(inputs):\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/chat/agents/react.py\", line 238, in astream\n      |     raise ValueError(\"hoge\")\n      | ValueError: hoge\n      +------------------------------------\n\nDuring handling of the above exception, another exception occurred:\n\n  + Exception Group Traceback (most recent call last):\n  |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/_utils.py\", line 87, in collapse_excgroups\n  |     yield\n  |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 190, in __call__\n  |     async with anyio.create_task_group() as task_group:\n  |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py\", line 680, in __aexit__\n  |     raise BaseExceptionGroup(\n  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)\n  +-+---------------- 1 ----------------\n    | Traceback (most recent call last):\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 265, in __call__\n    |     await wrap(partial(self.listen_for_disconnect, receive))\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 261, in wrap\n    |     await func()\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 238, in listen_for_disconnect\n    |     message = await receive()\n    |               ^^^^^^^^^^^^^^^\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 54, in wrapped_receive\n    |     msg = await self.receive()\n    |           ^^^^^^^^^^^^^^^^^^^^\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py\", line 555, in receive\n    |     await self.message_event.wait()\n    |   File \"/home/shinya/.asdf/installs/python/3.11.10/lib/python3.11/asyncio/locks.py\", line 213, in wait\n    |     await fut\n    | asyncio.exceptions.CancelledError: Cancelled by cancel scope 74cc08491050\n    | \n    | During handling of the above exception, another exception occurred:\n    | \n    | Exception Group Traceback (most recent call last):\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 192, in __call__\n    |     await response(scope, wrapped_receive, send)\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 258, in __call__\n    |     async with anyio.create_task_group() as task_group:\n    |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py\", line 680, in __aexit__\n    |     raise BaseExceptionGroup(\n    | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)\n    +-+---------------- 1 ----------------\n      | Traceback (most recent call last):\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 265, in __call__\n      |     await wrap(partial(self.listen_for_disconnect, receive))\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 261, in wrap\n      |     await func()\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 238, in listen_for_disconnect\n      |     message = await receive()\n      |               ^^^^^^^^^^^^^^^\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 128, in receive_or_disconnect\n      |     message = await wrap(wrapped_receive)\n      |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 123, in wrap\n      |     result = await func()\n      |              ^^^^^^^^^^^^\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 54, in wrapped_receive\n      |     msg = await self.receive()\n      |           ^^^^^^^^^^^^^^^^^^^^\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py\", line 555, in receive\n      |     await self.message_event.wait()\n      |   File \"/home/shinya/.asdf/installs/python/3.11.10/lib/python3.11/asyncio/locks.py\", line 213, in wait\n      |     await fut\n      | asyncio.exceptions.CancelledError: Cancelled by cancel scope 74cc08682090\n      | \n      | During handling of the above exception, another exception occurred:\n      | \n      | Exception Group Traceback (most recent call last):\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 261, in wrap\n      |     await func()\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 217, in stream_response\n      |     return await super().stream_response(send)\n      |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 250, in stream_response\n      |     async for chunk in self.body_iterator:\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 181, in body_stream\n      |     raise app_exc\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 151, in coro\n      |     await self.app(scope, receive_or_disconnect, send_no_error)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/exceptions.py\", line 65, in __call__\n      |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py\", line 64, in wrapped_app\n      |     raise exc\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py\", line 53, in wrapped_app\n      |     await app(scope, receive, sender)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/routing.py\", line 756, in __call__\n      |     await self.middleware_stack(scope, receive, send)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/routing.py\", line 776, in app\n      |     await route.handle(scope, receive, send)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/routing.py\", line 297, in handle\n      |     await self.app(scope, receive, send)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/routing.py\", line 77, in app\n      |     await wrap_app_handling_exceptions(app, request)(scope, receive, send)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py\", line 64, in wrapped_app\n      |     raise exc\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py\", line 53, in wrapped_app\n      |     await app(scope, receive, sender)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/routing.py\", line 75, in app\n      |     await response(scope, receive, send)\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 258, in __call__\n      |     async with anyio.create_task_group() as task_group:\n      |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py\", line 680, in __aexit__\n      |     raise BaseExceptionGroup(\n      | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)\n      +-+---------------- 1 ----------------\n        | Traceback (most recent call last):\n        |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/middleware.py\", line 125, in __call__\n        |     await self.app(scope, receive, send_wrapper)\n        |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/middleware.py\", line 410, in __call__\n        |     await self.app(scope, receive, send)\n        |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/authentication.py\", line 49, in __call__\n        |     await self.app(scope, receive, send)\n        |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/middleware.py\", line 427, in __call__\n        |     await self.app(scope, receive, send)\n        |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/middleware.py\", line 334, in __call__\n        |     await self.app(scope, receive, send)\n        |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 189, in __call__\n        |     with collapse_excgroups():\n        |   File \"/home/shinya/.asdf/installs/python/3.11.10/lib/python3.11/contextlib.py\", line 158, in __exit__\n        |     self.gen.throw(typ, value, traceback)\n        |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/_utils.py\", line 93, in collapse_excgroups\n        |     raise exc\n        |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 261, in wrap\n        |     await func()\n        |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 250, in stream_response\n        |     async for chunk in self.body_iterator:\n        |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/v2/chat/agent.py\", line 56, in _stream_handler\n        |     async for event in stream_events:\n        |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/chat/executor.py\", line 66, in stream\n        |     async for action in agent.astream(inputs):\n        |   File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/chat/agents/react.py\", line 238, in astream\n        |     raise ValueError(\"hoge\")\n        | ValueError: hoge\n        +------------------------------------\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/middleware.py\", line 125, in __call__\n    await self.app(scope, receive, send_wrapper)\n  File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/middleware.py\", line 410, in __call__\n    await self.app(scope, receive, send)\n  File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/authentication.py\", line 49, in __call__\n    await self.app(scope, receive, send)\n  File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/middleware.py\", line 427, in __call__\n    await self.app(scope, receive, send)\n  File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/middleware.py\", line 334, in __call__\n    await self.app(scope, receive, send)\n  File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/middleware/base.py\", line 189, in __call__\n    with collapse_excgroups():\n  File \"/home/shinya/.asdf/installs/python/3.11.10/lib/python3.11/contextlib.py\", line 158, in __exit__\n    self.gen.throw(typ, value, traceback)\n  File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/_utils.py\", line 93, in collapse_excgroups\n    raise exc\n  File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 261, in wrap\n    await func()\n  File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/.venv/lib/python3.11/site-packages/starlette/responses.py\", line 250, in stream_response\n    async for chunk in self.body_iterator:\n  File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/api/v2/chat/agent.py\", line 56, in _stream_handler\n    async for event in stream_events:\n  File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/chat/executor.py\", line 66, in stream\n    async for action in agent.astream(inputs):\n  File \"/home/shinya/gitlab-development-kit/gitlab-ai-gateway/ai_gateway/chat/agents/react.py\", line 238, in astream\n    raise ValueError(\"hoge\")\nValueError: hoge\n",
  "logger": "api.access",
  "level": "info",
  "type": "mlops",
  "stage": "main",
  "timestamp": "2024-09-19T01:17:41.486257Z",
  "message": "127.0.0.1:48110 - \"POST /v2/chat/agent HTTP/1.1\" 200"
}

Notice that:

  • exception is set correctly. This is because the exception/finally block of the log middleware runs after the actual process of streaming response.
  • duration_s is set correctly. Same reason with above.

Merge request checklist

  • Tests added for new functionality. If not, please raise an issue to follow up.
  • Documentation added/updated, if needed.
Edited by Tan Le

Merge request reports

Loading