fix
This commit is contained in:
parent
fdb4cc98e8
commit
64ef3ff776
@ -6,6 +6,7 @@ services:
|
||||
image: mathwave/sprint-repo:configurator
|
||||
networks:
|
||||
- configurator-development
|
||||
- monitoring
|
||||
environment:
|
||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||
@ -20,3 +21,5 @@ services:
|
||||
networks:
|
||||
configurator-development:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
|
@ -6,6 +6,7 @@ services:
|
||||
image: mathwave/sprint-repo:configurator
|
||||
networks:
|
||||
- configurator
|
||||
- monitoring
|
||||
environment:
|
||||
MONGO_HOST: "mongo.sprinthub.ru"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||
@ -20,3 +21,5 @@ services:
|
||||
networks:
|
||||
configurator:
|
||||
external: true
|
||||
monitoring:
|
||||
external: true
|
||||
|
0
app/middlewares/__init__.py
Normal file
0
app/middlewares/__init__.py
Normal file
14
app/middlewares/metrics.py
Normal file
14
app/middlewares/metrics.py
Normal file
@ -0,0 +1,14 @@
|
||||
import datetime
|
||||
import zoneinfo
|
||||
from fastapi import Request, Response
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
|
||||
from app.utils.monitoring import monitoring
|
||||
|
||||
class MetricsMiddleware(BaseHTTPMiddleware):
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
start = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
response: Response = await call_next(request)
|
||||
end = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
||||
monitoring.send_metric(start, end, request.url.path, response.status_code, request.method)
|
||||
return response
|
24
app/utils/monitoring.py
Normal file
24
app/utils/monitoring.py
Normal file
@ -0,0 +1,24 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import datetime
|
||||
import requests
|
||||
|
||||
|
||||
class Monitroing:
|
||||
def __init__(self):
|
||||
self.executor = ThreadPoolExecutor(max_workers=1)
|
||||
|
||||
def send_metric(self, start: datetime.datetime, end: datetime.datetime, endpoint: str, status_code: int, method: str):
|
||||
def send():
|
||||
requests.post(f'http://monitoring:1237/api/v1/metrics/endpoint', json={
|
||||
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
|
||||
'service': 'configurator',
|
||||
'endpoint': endpoint,
|
||||
'status_code': status_code,
|
||||
'response_time': (end - start).microseconds // 1000,
|
||||
'method': method,
|
||||
})
|
||||
|
||||
self.executor.submit(send)
|
||||
|
||||
|
||||
monitoring = Monitroing()
|
Loading…
Reference in New Issue
Block a user