-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: backend monitoring changes (#371)
Changes to observability in backend APIs: - Include monitoring for STAC API endpoints - service dimension change for ingest api (incorrectly had "raster-api"), add monitoring to ingest handler - include path params and post body in logs - include stage dimension in metrics to distinguish between instances (i.e. dev, staging, etc.) - include metrics for each api path (i.e. collections/, collections/{collection_id}, mosaic/register, etc. ## Testing - deployed to `monitor` veda-backend stack and created tables in Grafana. See[ ticket](#349) for details. - Confirmed logs and metrics are being sent for raster, stac and ingest APIs
- Loading branch information
Showing
11 changed files
with
98 additions
and
29 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,42 +1,61 @@ | ||
"""Observability utils""" | ||
import json | ||
from typing import Callable | ||
|
||
from aws_lambda_powertools import Logger, Metrics, Tracer | ||
from aws_lambda_powertools import Logger, Metrics, Tracer, single_metric | ||
from aws_lambda_powertools.metrics import MetricUnit # noqa: F401 | ||
from src.config import ApiSettings | ||
|
||
from fastapi import Request, Response | ||
from fastapi.routing import APIRoute | ||
|
||
settings = ApiSettings() | ||
|
||
logger: Logger = Logger(service="raster-api", namespace="veda-backend") | ||
metrics: Metrics = Metrics(service="raster-api", namespace="veda-backend") | ||
metrics: Metrics = Metrics(namespace="veda-backend") | ||
metrics.set_default_dimensions(environment=settings.stage, service="raster-api") | ||
tracer: Tracer = Tracer() | ||
|
||
|
||
class LoggerRouteHandler(APIRoute): | ||
"""Extension of base APIRoute to add context to log statements, as well as record usage metricss""" | ||
"""Extension of base APIRoute to add context to log statements, as well as record usage metrics""" | ||
|
||
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 | ||
body = await request.body() | ||
try: | ||
body_json = json.loads(body) | ||
except json.decoder.JSONDecodeError: | ||
body_json = None | ||
|
||
ctx = { | ||
"path": request.url.path, | ||
"path_params": request.path_params, | ||
"body": body_json, | ||
"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 | ||
|
||
with single_metric( | ||
name="RequestCount", | ||
unit=MetricUnit.Count, | ||
value=1, | ||
) | ||
default_dimensions=metrics.default_dimensions, | ||
namespace="veda-backend", | ||
) as metric: | ||
metric.add_dimension( | ||
name="route", value=f"{request.method} {self.path}" | ||
) | ||
|
||
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 |
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
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
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
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