This library implements a range of common logging functions.
Project description
Longsight: Best Practice Logging Library
A range of common logging functions for the observability of Python AWS cloud applications
This library implements a range of best-practice logging techniques for Python AWS cloud applications. This includes FastAPI Lambda contexts.
This is a prototype Crossref Labs system. It is not guaranteed to be stable and the metadata schema and behaviour may be subject to change at any time.
longsight.instrumentation
The longsight.instrumentation module provides functionality for instrumenting a FastAPI application with AWS CloudWatch Metrics and Logs. It includes middleware to handle correlation IDs, filters for attaching correlation IDs to logs, and context managers for instrumenting routes with metrics and logging.
Installation
To install the longsight.instrumentation module, run the following command:
pip install longsight
Usage
To use the longsight.instrumentation module, import the necessary components and add them to your FastAPI application.
Decorators
Using the longsight decorators are the easiest way to start logging quickly.
from longsight.instrumentation import instrument
router = APIRouter()
@router.get("/path")
@instrument()
async def a_route(request: Request, instrumentation):
return {"message": "Hello, World!"}
Correlation ID Middleware
The AWSCorrelationIdMiddleware middleware automatically generates or loads a correlation ID for each incoming request, and attaches it to the request headers and logs. To use the middleware, create an instance of the AWSCorrelationIdMiddleware class and add it to your FastAPI application:
from fastapi import FastAPI
from longsight.instrumentation import AWSCorrelationIdMiddleware
app = FastAPI()
app.add_middleware(AWSCorrelationIdMiddleware)
By default, the middleware looks for the X-Request-ID header in the incoming request headers, or in the mangum handlers aws.context, and generates a new UUID if the header is not present.
Using Mangum and logging default Lambda stats
To configure Mangum to handle requests in an AWS Lambda context and to inject instrumentation, use:
from mangum import Mangum
handler = Mangum(app, lifespan="off")
handler = instrumentation.logger.inject_lambda_context(
lambda_handler=handler, clear_state=True
)
handler = instrumentation.metrics.log_metrics(
handler, capture_cold_start_metric=True
)
Logging Filters
The CorrelationIdFilter filter attaches the correlation ID to log records. To use the filter, create an instance of the CorrelationIdFilter class and add it to your logger:
import logging
from longsight.instrumentation import CorrelationIdFilter
logger = logging.getLogger(__name__)
logger.addFilter(CorrelationIdFilter())
This setup is done automatically if you use the decorators.
Context Managers
The Instrumentation context manager provides functionality for instrumenting routes with metrics and logging. To use the context manager, create an instance of the Instrumentation class and use it as a context manager:
from fastapi import FastAPI
from longsight.instrumentation import Instrumentation, Metrics, Logger
app = FastAPI()
@app.get("/")
async def root(request: Request):
with Instrumentation(
aws_connector=aws_connector,
fastapi_app=fastapi_app,
request=request) as instrumentation:
instrumentation.logger.info("Handling request")
instrumentation.metrics.put_metric("RequestCount", 1)
return {"message": "Hello, World!"}
The Instrumentation context manager automatically logs the start and end of the request, and provides an instance of the Logger and Metrics classes for logging and metrics. The Logger and Metrics classes are provided by the aws_lambda_powertools package.
Credits
- .gitignore from Github.
- AWS Lambda Powertools by Amazon.
© Crossref 2023
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for longsight-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 067a1c5b2efc0041d526927e3ffa1b05d1f195ec95e084564fa9733b3b2ec864 |
|
MD5 | 20a2742620034f6c2728f1588c42cb9f |
|
BLAKE2b-256 | 9c4267e463d5e8a0c29190fc1e3802771867f327510e7204d9d1517058b9082c |