longsight


Namelongsight JSON
Version 1.0.13 PyPI version JSON
download
home_pagehttps://gitlab.com/crossref/labs/distrunner
SummaryThis library implements a range of common logging functions.
upload_time2023-06-15 15:06:34
maintainer
docs_urlNone
authorMartin Paul Eve
requires_python>=3.8
licenseCopyright © 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": "",
    "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/15/f1/67ff6fd986c2f56ab97f10ce68db3f7686b0d2d5c96a32349270c931bced/longsight-1.0.13.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&copy; Crossref 2023\n",
    "bugtrack_url": null,
    "license": "Copyright &copy; 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.13",
    "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": "3da5a4bf188510963fa884b974730c5fe3f9c474a839b8ce57c0d8af3771abcd",
                "md5": "9665c6404330cbfd7f8d64b2b6cbca21",
                "sha256": "29aadbbd1eeebc32d116bc864c098d02f796a9a80a870afd4546f0343f11d268"
            },
            "downloads": -1,
            "filename": "longsight-1.0.13-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9665c6404330cbfd7f8d64b2b6cbca21",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 9036,
            "upload_time": "2023-06-15T15:06:31",
            "upload_time_iso_8601": "2023-06-15T15:06:31.685585Z",
            "url": "https://files.pythonhosted.org/packages/3d/a5/a4bf188510963fa884b974730c5fe3f9c474a839b8ce57c0d8af3771abcd/longsight-1.0.13-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "15f167ff6fd986c2f56ab97f10ce68db3f7686b0d2d5c96a32349270c931bced",
                "md5": "ea78ed7d755319dd5b5190390f548334",
                "sha256": "e8e9b133a083ca2c6e09385615ef959def405c3b3ce66092a8e4b2e2d6dcdd6a"
            },
            "downloads": -1,
            "filename": "longsight-1.0.13.tar.gz",
            "has_sig": false,
            "md5_digest": "ea78ed7d755319dd5b5190390f548334",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 12548,
            "upload_time": "2023-06-15T15:06:34",
            "upload_time_iso_8601": "2023-06-15T15:06:34.200569Z",
            "url": "https://files.pythonhosted.org/packages/15/f1/67ff6fd986c2f56ab97f10ce68db3f7686b0d2d5c96a32349270c931bced/longsight-1.0.13.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-15 15:06:34",
    "github": false,
    "gitlab": true,
    "bitbucket": false,
    "codeberg": false,
    "gitlab_user": "crossref",
    "gitlab_project": "labs",
    "lcname": "longsight"
}
        
Elapsed time: 0.07693s