Name | longsight JSON |
Version |
1.0.15
JSON |
| download |
home_page | https://gitlab.com/crossref/labs/distrunner |
Summary | This library implements a range of common logging functions. |
upload_time | 2024-05-21 10:39:55 |
maintainer | None |
docs_url | None |
author | Martin Paul Eve |
requires_python | >=3.8 |
license | Copyright © 2023 Crossref Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
distributed
computing
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Longsight: Best Practice Logging Library
A range of common logging functions for the observability of Python AWS cloud applications
![license](https://img.shields.io/gitlab/license/crossref/labs/longsight) ![activity](https://img.shields.io/gitlab/last-commit/crossref/labs/longsight)
![AWS](https://img.shields.io/badge/AWS-%23FF9900.svg?style=for-the-badge&logo=amazon-aws&logoColor=white) ![Linux](https://img.shields.io/badge/Linux-FCC624?style=for-the-badge&logo=linux&logoColor=black) ![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)
This library implements a range of best-practice logging techniques for Python AWS cloud applications. This includes [FastAPI Lambda contexts](https://www.eliasbrange.dev/posts/observability-with-fastapi-aws-lambda-powertools/).
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 locally (or in Lambda contexts) quickly.
from longsight.instrumentation import instrument
router = APIRouter()
@router.get("/path")
@instrument()
async def a_route(request: Request, instrumentation=None):
instrumentation.logger.info("Hello, World!")
return {"message": "Hello, World!"}
Note that, in FastAPI contexts, you must specify "instrumentation=None" to avoid FastAPI thinking this is an unfilled parameter.
Alternatively, you can also log to CloudWatch instead of locally from any function (note also that the decorator works on both async and synchronous functions and is _not_ limited to FastAPI functions):
from longsight.instrumentation import instrument
@instrumentation.instrument(
cloudwatch_push=True,
log_stream_name="martin-test-stream-name",
log_group_name="martin-test-group-name",
namespace="martin-test-namespace",
)
def a_function(instrumentation):
instrumentation.logger.info("Hello, World!")
instrumentation.logger.info("A second log line")
instrumentation.add_metric_point(
metric_name="test_metric",
dimensions=[{"Name": "Environment", "Value": "Production"}],
metric_value=1,
unit="Count",
time_stamp=datetime.now(),
)
Longsight can also create AWS objects for you to reuse throughout your project, centralizing AWS code:
from longsight.instrumentation import instrument
@instrument(create_aws=True, bucket="my-bucket")
def a_function(instrumentation):
instrumentation.logger.info("Hello, World!")
s3_client = instrumentation.aws_connector.s3_client
return
By default, the AWS interaction is anonymous. To write to S3 buckets or access protected buckets, pass sign_aws_requests=True.
## Easy counters
The instrumentation class also provides a simple counter function to increment a counter in CloudWatch:
from longsight.instrumentation import instrument
@instrumentation.instrument(
cloudwatch_push=True,
log_stream_name="martin-test-stream-name",
log_group_name="martin-test-group-name",
namespace="martin-test-namespace",
)
def a_function(instrumentation):
instrumentation.add_counter(
metric_name="test_counter", status_code=200, member_id=None,
additional_dimensions=[{"Name": "Environment", "Value": "Production"}]
)
## 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
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")
return {"message": "Hello, World!"}
The Instrumentation context manager automatically logs the start and end of the request, and provides an instance of the Logger classes for logging and metrics. The Logger classes are provided by the aws_lambda_powertools package.
# Credits
* [.gitignore](https://github.com/github/gitignore) from Github.
* [AWS Lambda Powertools](https://awslabs.github.io/aws-lambda-powertools-python/2.10.0/) by Amazon.
© Crossref 2023
Raw data
{
"_id": null,
"home_page": "https://gitlab.com/crossref/labs/distrunner",
"name": "longsight",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "Martin Paul Eve <meve@crossref.org>",
"keywords": "distributed computing",
"author": "Martin Paul Eve",
"author_email": "meve@crossref.org",
"download_url": "https://files.pythonhosted.org/packages/01/10/ea092f9c68be6e1c6fa22a0c054aaaf5e943bfbeedf5191d7c115e9b948a/longsight-1.0.15.tar.gz",
"platform": null,
"description": "# Longsight: Best Practice Logging Library\nA range of common logging functions for the observability of Python AWS cloud applications\n\n\n![license](https://img.shields.io/gitlab/license/crossref/labs/longsight) ![activity](https://img.shields.io/gitlab/last-commit/crossref/labs/longsight)\n\n![AWS](https://img.shields.io/badge/AWS-%23FF9900.svg?style=for-the-badge&logo=amazon-aws&logoColor=white) ![Linux](https://img.shields.io/badge/Linux-FCC624?style=for-the-badge&logo=linux&logoColor=black) ![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54)\n\nThis library implements a range of best-practice logging techniques for Python AWS cloud applications. This includes [FastAPI Lambda contexts](https://www.eliasbrange.dev/posts/observability-with-fastapi-aws-lambda-powertools/). \n\nThis 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.\n\n# longsight.instrumentation\nThe 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.\n\n## Installation\nTo install the longsight.instrumentation module, run the following command:\n\n pip install longsight\n\n## Usage\nTo use the longsight.instrumentation module, import the necessary components and add them to your FastAPI application.\n\n## Decorators\nUsing the longsight decorators are the easiest way to start logging locally (or in Lambda contexts) quickly.\n\n from longsight.instrumentation import instrument\n\n router = APIRouter()\n \n @router.get(\"/path\")\n @instrument()\n async def a_route(request: Request, instrumentation=None):\n instrumentation.logger.info(\"Hello, World!\")\n return {\"message\": \"Hello, World!\"}\n\nNote that, in FastAPI contexts, you must specify \"instrumentation=None\" to avoid FastAPI thinking this is an unfilled parameter.\n\nAlternatively, you can also log to CloudWatch instead of locally from any function (note also that the decorator works on both async and synchronous functions and is _not_ limited to FastAPI functions):\n\n from longsight.instrumentation import instrument\n\n @instrumentation.instrument(\n cloudwatch_push=True,\n log_stream_name=\"martin-test-stream-name\",\n log_group_name=\"martin-test-group-name\",\n namespace=\"martin-test-namespace\",\n )\n def a_function(instrumentation):\n instrumentation.logger.info(\"Hello, World!\")\n instrumentation.logger.info(\"A second log line\")\n instrumentation.add_metric_point(\n metric_name=\"test_metric\",\n dimensions=[{\"Name\": \"Environment\", \"Value\": \"Production\"}],\n metric_value=1,\n unit=\"Count\",\n time_stamp=datetime.now(),\n )\n\nLongsight can also create AWS objects for you to reuse throughout your project, centralizing AWS code:\n\n from longsight.instrumentation import instrument\n\n @instrument(create_aws=True, bucket=\"my-bucket\")\n def a_function(instrumentation):\n instrumentation.logger.info(\"Hello, World!\")\n s3_client = instrumentation.aws_connector.s3_client\n return\n\nBy default, the AWS interaction is anonymous. To write to S3 buckets or access protected buckets, pass sign_aws_requests=True. \n\n## Easy counters\nThe instrumentation class also provides a simple counter function to increment a counter in CloudWatch:\n\n from longsight.instrumentation import instrument\n\n @instrumentation.instrument(\n cloudwatch_push=True,\n log_stream_name=\"martin-test-stream-name\",\n log_group_name=\"martin-test-group-name\",\n namespace=\"martin-test-namespace\",\n )\n def a_function(instrumentation):\n instrumentation.add_counter(\n metric_name=\"test_counter\", status_code=200, member_id=None, \n additional_dimensions=[{\"Name\": \"Environment\", \"Value\": \"Production\"}]\n )\n\n## Correlation ID Middleware\nThe 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:\n\n from fastapi import FastAPI\n from longsight.instrumentation import AWSCorrelationIdMiddleware\n \n app = FastAPI()\n app.add_middleware(AWSCorrelationIdMiddleware)\n\nBy 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.\n\n## Using Mangum and logging default Lambda stats\n\nTo configure Mangum to handle requests in an AWS Lambda context and to inject instrumentation, use:\n\n from mangum import Mangum\n handler = Mangum(app, lifespan=\"off\")\n handler = instrumentation.logger.inject_lambda_context(\n lambda_handler=handler, clear_state=True\n )\n handler = instrumentation.metrics.log_metrics(\n handler, capture_cold_start_metric=True\n )\n\n## Logging Filters\nThe 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:\n\n import logging\n from longsight.instrumentation import CorrelationIdFilter\n \n logger = logging.getLogger(__name__)\n logger.addFilter(CorrelationIdFilter())\n\nThis setup is done automatically if you use the decorators.\n\n## Context Managers\nThe 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:\n\n from fastapi import FastAPI\n from longsight.instrumentation import Instrumentation\n \n app = FastAPI()\n \n @app.get(\"/\")\n async def root(request: Request):\n with Instrumentation(\n aws_connector=aws_connector,\n fastapi_app=fastapi_app,\n request=request) as instrumentation:\n instrumentation.logger.info(\"Handling request\")\n return {\"message\": \"Hello, World!\"}\n\nThe Instrumentation context manager automatically logs the start and end of the request, and provides an instance of the Logger classes for logging and metrics. The Logger classes are provided by the aws_lambda_powertools package.\n\n# Credits\n* [.gitignore](https://github.com/github/gitignore) from Github.\n* [AWS Lambda Powertools](https://awslabs.github.io/aws-lambda-powertools-python/2.10.0/) by Amazon.\n\n© Crossref 2023\n",
"bugtrack_url": null,
"license": "Copyright © 2023 Crossref Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
"summary": "This library implements a range of common logging functions.",
"version": "1.0.15",
"project_urls": {
"Homepage": "https://gitlab.com/crossref/labs/distrunner",
"changelog": "https://gitlab.com/crossref/labs/longsight/-/blob/main/CHANGELOG.md",
"documentation": "https://labs.crossref.org",
"homepage": "https://labs.crossref.org",
"repository": "https://gitlab.com/crossref/labs/longsight"
},
"split_keywords": [
"distributed",
"computing"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3b40eaba93b1503bfdb18cf3c44e2c41502dcc1c21874833f1f26df659f1cc73",
"md5": "f10cf5d662a9a941d6078ed106d35dcc",
"sha256": "b470fbc76b34b1b11da7cee0ad93e3870d753953ccae59a296945d35c0cbc259"
},
"downloads": -1,
"filename": "longsight-1.0.15-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f10cf5d662a9a941d6078ed106d35dcc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 9180,
"upload_time": "2024-05-21T10:39:50",
"upload_time_iso_8601": "2024-05-21T10:39:50.595858Z",
"url": "https://files.pythonhosted.org/packages/3b/40/eaba93b1503bfdb18cf3c44e2c41502dcc1c21874833f1f26df659f1cc73/longsight-1.0.15-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0110ea092f9c68be6e1c6fa22a0c054aaaf5e943bfbeedf5191d7c115e9b948a",
"md5": "bab8c3825c05324fa497938a6fad926a",
"sha256": "07a99777e4a808f998d248c38b55a854ec8f204edb37147c949c58964d766fe6"
},
"downloads": -1,
"filename": "longsight-1.0.15.tar.gz",
"has_sig": false,
"md5_digest": "bab8c3825c05324fa497938a6fad926a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 12679,
"upload_time": "2024-05-21T10:39:55",
"upload_time_iso_8601": "2024-05-21T10:39:55.158562Z",
"url": "https://files.pythonhosted.org/packages/01/10/ea092f9c68be6e1c6fa22a0c054aaaf5e943bfbeedf5191d7c115e9b948a/longsight-1.0.15.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-21 10:39:55",
"github": false,
"gitlab": true,
"bitbucket": false,
"codeberg": false,
"gitlab_user": "crossref",
"gitlab_project": "labs",
"lcname": "longsight"
}