Skip to content

Commit

Permalink
fix: add monitoring router class
Browse files Browse the repository at this point in the history
  • Loading branch information
ividito committed Mar 22, 2024
1 parent 1ebfb9e commit 3cdd145
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions workflows_api/runtime/src/monitoring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Observability utils"""
from typing import Callable

from aws_lambda_powertools import Logger, Metrics, Tracer
from aws_lambda_powertools.metrics import MetricUnit # noqa: F401

from fastapi import Request, Response
from fastapi.routing import APIRoute

logger: Logger = Logger(service="raster-api", namespace="veda-backend")
metrics: Metrics = Metrics(service="raster-api", namespace="veda-backend")
tracer: Tracer = Tracer()


class LoggerRouteHandler(APIRoute):
"""Extension of base APIRoute to add context to log statements, as well as record usage metricss"""

def get_route_handler(self) -> Callable:
"""Overide route handler method to add logs, metrics, tracing"""
original_route_handler = super().get_route_handler()

async def route_handler(request: Request) -> Response:
# Add fastapi context to logs
ctx = {
"path": request.url.path,
"route": self.path,
"method": request.method,
}
logger.append_keys(fastapi=ctx)
logger.info("Received request")
metrics.add_metric(
name="/".join(
str(request.url.path).split("/")[:2]
), # enough detail to capture search IDs, but not individual tile coords
unit=MetricUnit.Count,
value=1,
)
tracer.put_annotation(key="path", value=request.url.path)
tracer.capture_method(original_route_handler)(request)
return await original_route_handler(request)

return route_handler

0 comments on commit 3cdd145

Please sign in to comment.