-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8a5681
commit b3e5daf
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,4 @@ | ||
from .logging import LoggingMiddleware | ||
from .metrics import PrometheusMiddleware | ||
|
||
__all__ = ["LoggingMiddleware", "PrometheusMiddleware"] |
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,28 @@ | ||
import logging | ||
import time | ||
|
||
from fastapi import Request | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class LoggingMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request: Request, call_next): | ||
start_time = time.perf_counter() | ||
response = await call_next(request) | ||
process_time = time.perf_counter() - start_time | ||
|
||
query_params_list = [ | ||
(key, value if key != "token" else "***") | ||
for key, value in request.query_params.items() | ||
] | ||
|
||
logger.info( | ||
{ | ||
"url": request.url.path, | ||
"params": query_params_list, | ||
"process_time": f"{process_time:.4f}", | ||
} | ||
) | ||
return response |
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,38 @@ | ||
from prometheus_client.metrics import Counter, Histogram | ||
|
||
import time | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
from fastapi import Request | ||
|
||
# Define Prometheus metrics | ||
REQUEST_COUNT = Counter( | ||
"request_count", "Total number of requests", ["method", "endpoint", "http_status"] | ||
) | ||
REQUEST_LATENCY = Histogram( | ||
"request_latency_seconds", "Latency of requests in seconds", ["method", "endpoint"] | ||
) | ||
|
||
|
||
# Middleware for Prometheus metrics logging | ||
class PrometheusMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request: Request, call_next): | ||
REQUEST_COUNT.labels( | ||
method=request.method, | ||
endpoint=request.url.path | ||
).inc() | ||
|
||
# Start timer for request latency | ||
start_time = time.perf_counter() | ||
|
||
# Process request | ||
response = await call_next(request) | ||
|
||
# Calculate request latency | ||
latency = time.perf_counter() - start_time | ||
|
||
REQUEST_LATENCY.labels( | ||
method=request.method, | ||
endpoint=request.url.path, | ||
).observe(latency) | ||
|
||
return response |