forked from ITISFoundation/osparc-simcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
factor out middleware to better control imports
- Loading branch information
1 parent
ed10118
commit 96553f6
Showing
2 changed files
with
63 additions
and
62 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
services/api-server/src/simcore_service_api_server/core/_profiler_middleware.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import json | ||
|
||
from fastapi import FastAPI | ||
from pyinstrument import Profiler | ||
from starlette.requests import Request | ||
|
||
|
||
def _generate_response_headers(content: bytes) -> list[tuple[bytes, bytes]]: | ||
headers: dict = dict() | ||
headers[b"content-length"] = str(len(content)).encode("utf8") | ||
headers[b"content-type"] = b"application/json" | ||
return list(headers.items()) | ||
|
||
|
||
class ApiServerProfilerMiddleware: | ||
"""Following | ||
https://www.starlette.io/middleware/#cleanup-and-error-handling | ||
https://www.starlette.io/middleware/#reusing-starlette-components | ||
https://fastapi.tiangolo.com/advanced/middleware/#advanced-middleware | ||
""" | ||
|
||
def __init__(self, app: FastAPI): | ||
self._app: FastAPI = app | ||
self._profile_header_trigger: str = "x-profile-api-server" | ||
|
||
async def __call__(self, scope, receive, send): | ||
if scope["type"] != "http": | ||
await self._app(scope, receive, send) | ||
return | ||
|
||
profiler = Profiler(async_mode="enabled") | ||
request: Request = Request(scope) | ||
headers = dict(request.headers) | ||
if self._profile_header_trigger in headers: | ||
headers.pop(self._profile_header_trigger) | ||
scope["headers"] = [ | ||
(k.encode("utf8"), v.encode("utf8")) for k, v in headers.items() | ||
] | ||
profiler.start() | ||
|
||
async def send_wrapper(message): | ||
if profiler.is_running: | ||
profiler.stop() | ||
if profiler.last_session: | ||
body: bytes = json.dumps( | ||
{"profile": profiler.output_text(unicode=True, color=True)} | ||
).encode("utf8") | ||
if message["type"] == "http.response.start": | ||
message["headers"] = _generate_response_headers(body) | ||
elif message["type"] == "http.response.body": | ||
message["body"] = body | ||
await send(message) | ||
|
||
await self._app(scope, receive, send_wrapper) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters