Version 0.2.0
All checks were successful
Run linters on applied template / Python 3.13 lint and build (push) Successful in 54s
All checks were successful
Run linters on applied template / Python 3.13 lint and build (push) Successful in 54s
Changes: - add metrics dispencer - add basic authentication dependency - enable GZIP middleware - add !env() example to deploy section - update dependencies state attribute name
This commit is contained in:
@@ -41,7 +41,7 @@ class ObservabilityMiddleware(BaseHTTPMiddleware): # pylint: disable=too-few-pu
|
||||
def __init__(self, app: FastAPI, exception_mapper: ExceptionMapper, metrics: Metrics, urls_mapper: URLsMapper):
|
||||
super().__init__(app)
|
||||
self._exception_mapper = exception_mapper
|
||||
self._metrics = metrics
|
||||
self._http_metrics = metrics.http
|
||||
self._urls_mapper = urls_mapper
|
||||
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
@@ -49,7 +49,7 @@ class ObservabilityMiddleware(BaseHTTPMiddleware): # pylint: disable=too-few-pu
|
||||
_try_get_parent_span_id(request)
|
||||
with _tracer.start_as_current_span("http-request") as span:
|
||||
trace_id = hex(span.get_span_context().trace_id or randint(1, 1 << 63))[2:]
|
||||
span_id = span.get_span_context().span_id or randint(1, 1 << 32)
|
||||
span_id = span.get_span_context().span_id or randint(1, 1 << 31)
|
||||
span.set_attributes(
|
||||
{
|
||||
http_attributes.HTTP_REQUEST_METHOD: request.method,
|
||||
@@ -70,7 +70,7 @@ class ObservabilityMiddleware(BaseHTTPMiddleware): # pylint: disable=too-few-pu
|
||||
)
|
||||
|
||||
path_for_metric = self._urls_mapper.map(request.url.path)
|
||||
self._metrics.requests_started.add(1, {"method": request.method, "path": path_for_metric})
|
||||
self._http_metrics.requests_started.add(1, {"method": request.method, "path": path_for_metric})
|
||||
|
||||
time_begin = time.monotonic()
|
||||
try:
|
||||
@@ -94,7 +94,7 @@ class ObservabilityMiddleware(BaseHTTPMiddleware): # pylint: disable=too-few-pu
|
||||
)
|
||||
raise ObservableException(trace_id=trace_id, span_id=span_id) from exc
|
||||
finally:
|
||||
self._metrics.request_processing_duration.record(
|
||||
self._http_metrics.request_processing_duration.record(
|
||||
duration_seconds, {"method": request.method, "path": path_for_metric}
|
||||
)
|
||||
|
||||
@@ -109,7 +109,7 @@ class ObservabilityMiddleware(BaseHTTPMiddleware): # pylint: disable=too-few-pu
|
||||
duration_seconds: float,
|
||||
) -> None:
|
||||
await logger.ainfo("request handled successfully", time_consumed=round(duration_seconds, 3))
|
||||
self._metrics.requests_finished.add(
|
||||
self._http_metrics.requests_finished.add(
|
||||
1, {"method": request.method, "path": path_for_metric, "status_code": result.status_code}
|
||||
)
|
||||
|
||||
@@ -132,7 +132,7 @@ class ObservabilityMiddleware(BaseHTTPMiddleware): # pylint: disable=too-few-pu
|
||||
if exc.__cause__ is not None:
|
||||
cause = exc.__cause__
|
||||
|
||||
self._metrics.errors.add(
|
||||
self._http_metrics.errors.add(
|
||||
1,
|
||||
{
|
||||
"method": request.method,
|
||||
|
||||
Reference in New Issue
Block a user