Ajout du timing middleware pour le mode debug

This commit is contained in:
Maxime Alves LIRMM@home 2021-04-22 22:09:05 +02:00
parent 89ec439d3e
commit 5b67d938e2
4 changed files with 31 additions and 1 deletions

View File

@ -17,6 +17,7 @@ uvicorn = ">=0.13,<1"
orjson = ">=3.4.7,<4"
pyjwt = ">=2.0.1,<3"
pyyaml = ">=5.3.1,<6"
timing-asgi = ">=0.2.1,<1"
[requires]
python_version = "3.8"

View File

@ -18,11 +18,14 @@ from starlette.middleware import Middleware
from starlette.routing import Route
from starlette.middleware.authentication import AuthenticationMiddleware
from timing_asgi import TimingMiddleware
from timing_asgi.integrations import StarletteScopeToName
# module libraries
from halfapi.conf import config, SECRET, PRODUCTION, DOMAINSDICT
from .lib.domain_middleware import DomainMiddleware
from .lib.timing import HTimingClient
from halfapi.lib.jwt_middleware import JWTAuthenticationBackend
@ -70,3 +73,11 @@ application = Starlette(
501: NotImplementedResponse
}
)
if not PRODUCTION:
application.add_middleware(
TimingMiddleware,
client=HTimingClient(),
metric_namer=StarletteScopeToName(prefix="halfapi",
starlette_app=application)
)

17
halfapi/lib/timing.py Normal file
View File

@ -0,0 +1,17 @@
import logging
from timing_asgi import TimingClient
logger = logging.getLogger('uvicorn.asgi')
class HTimingClient(TimingClient):
def timing(self, metric_name, timing, tags):
tags_d = {
key: val
for key, val in map(
lambda elt: elt.split(':'), tags)
}
logger.debug('[TIME:%s][%s] %s %s - %sms',
tags_d['time'], metric_name,
tags_d['http_method'], tags_d['http_status'],
round(timing*1000, 2))

View File

@ -48,7 +48,8 @@ setup(
"click>=7.1,<8",
"uvicorn>=0.13,<1",
"orjson>=3.4.7,<4",
"pyyaml>=5.3.1,<6"
"pyyaml>=5.3.1,<6",
"timing-asgi>=0.2.1,<1"
],
extras_require={
"tests":[