-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Timing middleware: add support for alternate message formatters
- Loading branch information
Conor Heine
committed
Dec 9, 2020
1 parent
af95ff4
commit 9895ae1
Showing
7 changed files
with
181 additions
and
35 deletions.
There are no files selected for viewing
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
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
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 asyncio | ||
import logging | ||
|
||
from fastapi import FastAPI | ||
from starlette.requests import Request | ||
from starlette.staticfiles import StaticFiles | ||
from starlette.testclient import TestClient | ||
|
||
from fastapi_utils import timing | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
|
||
app = FastAPI() | ||
timing.add_timing_middleware( | ||
app, | ||
record=logger.info, | ||
prefix="app", | ||
exclude="untimed", | ||
format_message=timing.json_formatter, | ||
) | ||
static_files_app = StaticFiles(directory=".") | ||
app.mount(path="/static", app=static_files_app, name="static") | ||
|
||
|
||
@app.get("/json/timed") | ||
async def get_json_timed() -> None: | ||
await asyncio.sleep(0.05) | ||
|
||
|
||
@app.get("/json/untimed") | ||
async def get_json_untimed() -> None: | ||
await asyncio.sleep(0.1) | ||
|
||
|
||
@app.get("/json/timed-intermediate") | ||
async def get_json_with_intermediate_timing(request: Request) -> None: | ||
await asyncio.sleep(0.1) | ||
timing.record_timing(request, note="halfway") | ||
await asyncio.sleep(0.1) | ||
|
||
|
||
TestClient(app).get("/json/timed") | ||
# INFO:__main__:TIMING: {"wall_ms":53.0,"cpu_ms":1.2,"name":"app.__main__.get_json_timed","note":null} | ||
|
||
TestClient(app).get("/json/untimed") | ||
# <nothing logged> | ||
|
||
TestClient(app).get("/json/timed-intermediate") | ||
# INFO:__main__:TIMING: {"wall_ms":105.3,"cpu_ms":0.4,"name":"app.__main__.get_json_with_intermediate_timing","note":"halfway"} | ||
# INFO:__main__:TIMING: {"wall_ms":206.7,"cpu_ms":1.1,"name":"app.__main__.get_json_timed","note":null} | ||
|
||
TestClient(app).get("/static/test") | ||
# INFO:__main__:TIMING: {"wall_ms":1.6,"cpu_ms":1.6,"name":"StaticFiles<'static'>","note":null} |
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
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
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
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