Ajout du timing middleware pour le mode debug
This commit is contained in:
parent
89ec439d3e
commit
5b67d938e2
1
Pipfile
1
Pipfile
|
@ -17,6 +17,7 @@ uvicorn = ">=0.13,<1"
|
||||||
orjson = ">=3.4.7,<4"
|
orjson = ">=3.4.7,<4"
|
||||||
pyjwt = ">=2.0.1,<3"
|
pyjwt = ">=2.0.1,<3"
|
||||||
pyyaml = ">=5.3.1,<6"
|
pyyaml = ">=5.3.1,<6"
|
||||||
|
timing-asgi = ">=0.2.1,<1"
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.8"
|
python_version = "3.8"
|
||||||
|
|
|
@ -18,11 +18,14 @@ from starlette.middleware import Middleware
|
||||||
from starlette.routing import Route
|
from starlette.routing import Route
|
||||||
from starlette.middleware.authentication import AuthenticationMiddleware
|
from starlette.middleware.authentication import AuthenticationMiddleware
|
||||||
|
|
||||||
|
from timing_asgi import TimingMiddleware
|
||||||
|
from timing_asgi.integrations import StarletteScopeToName
|
||||||
|
|
||||||
# module libraries
|
# module libraries
|
||||||
from halfapi.conf import config, SECRET, PRODUCTION, DOMAINSDICT
|
from halfapi.conf import config, SECRET, PRODUCTION, DOMAINSDICT
|
||||||
|
|
||||||
from .lib.domain_middleware import DomainMiddleware
|
from .lib.domain_middleware import DomainMiddleware
|
||||||
|
from .lib.timing import HTimingClient
|
||||||
|
|
||||||
from halfapi.lib.jwt_middleware import JWTAuthenticationBackend
|
from halfapi.lib.jwt_middleware import JWTAuthenticationBackend
|
||||||
|
|
||||||
|
@ -70,3 +73,11 @@ application = Starlette(
|
||||||
501: NotImplementedResponse
|
501: NotImplementedResponse
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not PRODUCTION:
|
||||||
|
application.add_middleware(
|
||||||
|
TimingMiddleware,
|
||||||
|
client=HTimingClient(),
|
||||||
|
metric_namer=StarletteScopeToName(prefix="halfapi",
|
||||||
|
starlette_app=application)
|
||||||
|
)
|
||||||
|
|
|
@ -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))
|
Loading…
Reference in New Issue