asgi-request-duration


Nameasgi-request-duration JSON
Version 1.0.3 PyPI version JSON
download
home_pagehttps://github.com/feteu/asgi-request-duration
SummaryA asgi middleware to measure the request duration
upload_time2024-12-28 09:06:27
maintainerFabio Greco
docs_urlNone
authorFabio Greco
requires_python>=3.11
licenseGNU GPLv3
keywords asgi async connexion fastapi middleware request-duration starlette timing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![PyPI - License](https://img.shields.io/pypi/l/asgi-request-duration)](https://www.gnu.org/licenses/gpl-3.0)
[![PyPI - Version](https://img.shields.io/pypi/v/asgi-request-duration.svg)](https://pypi.org/project/asgi-request-duration/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/asgi-request-duration)](https://pypi.org/project/asgi-request-duration/)
[![PyPI - Status](https://img.shields.io/pypi/status/asgi-request-duration)](https://pypi.org/project/asgi-request-duration/)
[![Dependencies](https://img.shields.io/librariesio/release/pypi/asgi-request-duration)](https://libraries.io/pypi/asgi-request-duration)
[![Last Commit](https://img.shields.io/github/last-commit/feteu/asgi-request-duration)](https://github.com/feteu/asgi-request-duration/commits/main)
[![Build Status build/testpypi](https://img.shields.io/github/actions/workflow/status/feteu/asgi-request-duration/publish-testpypi.yaml?label=publish-testpypi)](https://github.com/feteu/asgi-request-duration/actions/workflows/publish-testpypi.yaml)
[![Build Status build/pypi](https://img.shields.io/github/actions/workflow/status/feteu/asgi-request-duration/publish-pypi.yaml?label=publish-pypi)](https://github.com/feteu/asgi-request-duration/actions/workflows/publish-pypi.yaml)
[![Build Status test](https://img.shields.io/github/actions/workflow/status/feteu/asgi-request-duration/test.yaml?label=test)](https://github.com/feteu/asgi-request-duration/actions/workflows/test.yaml)

# ASGI Request Duration โฑ๏ธ

ASGI Request Duration is a middleware for ASGI applications that measures the duration of HTTP requests and integrates this information into response headers and log records. This middleware is designed to be easy to integrate and configure, providing valuable insights into the performance of your ASGI application.

## Table of Contents ๐Ÿ“š

- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
  - [Middleware](#middleware)
  - [Logging Filter](#logging-filter)
  - [Configuration](#configuration)
- [Examples](#examples)
- [Development](#development)
- [License](#license)
- [Contributing](#contributing)
- [Contact](#contact)

## Features โœจ

- Measure the duration of each HTTP request.
- Add the request duration to response headers.
- Integrate the request duration into log records.
- Configurable header name and precision.
- Exclude specific paths from timing.

## Installation ๐Ÿ› ๏ธ

You can install the package using pip:

```bash
pip install asgi-request-duration
```

## Usage ๐Ÿš€

### Middleware

To use the middleware, add it to your ASGI application:

```python
from asgi_request_duration.middleware import RequestDurationMiddleware
from starlette.applications import Starlette

app = Starlette()
app.add_middleware(RequestDurationMiddleware)
```

### Logging Filter

To use the logging filter, configure your logger to use the `RequestDurationFilter`:

```python
import logging
from asgi_request_duration.filters import RequestDurationFilter

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("myapp")
logger.addFilter(RequestDurationFilter())
```

### Configuration โš™๏ธ

#### Middleware Configuration

You can configure the middleware by passing parameters to the `RequestDurationMiddleware`:

- `excluded_paths`: List of paths to exclude from timing.
- `header_name`: The name of the header to store the request duration.
- `precision`: The precision of the recorded duration.
- `skip_validate_header_name`: Flag to skip header name validation.
- `skip_validate_precision`: Flag to skip precision validation.

Example:

```python
app.add_middleware(
    RequestDurationMiddleware,
    excluded_paths=["/health"],
    header_name="X-Request-Duration",
    precision=3,
    skip_validate_header_name=False,
    skip_validate_precision=False
)
```

#### Logging Filter Configuration

You can configure the logging filter by passing parameters to the `RequestDurationFilter`:

- `context_key`: The key to retrieve the request duration context value.
- `default_value`: The default value if the request duration context key is not found.

Example:

```python
logger.addFilter(RequestDurationFilter(context_key="request_duration", default_value="-"))
```

## Examples ๐Ÿ“–

Here are complete examples of how to use the middleware with Starlette applications. You can find the full example code in the [examples](examples) folder.

## Development ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

### Requirements

- Python 3.11+
- Poetry

### Setup

Clone the repository and install the dependencies:

```sh
git clone https://github.com/yourusername/asgi-request-duration.git
cd asgi-request-duration
poetry install
```

### Running Tests ๐Ÿงช

You can run the tests using `pytest`:

```sh
poetry run pytest
```

## License ๐Ÿ“œ

This project is licensed under the GNU GPLv3 License. See the [LICENSE](LICENSE) file for more details.

## Contributing ๐Ÿค

Contributions are welcome! Please read the [CONTRIBUTING](CONTRIBUTING.md) file for guidelines on how to contribute to this project.

## Contact ๐Ÿ“ฌ

For any questions or suggestions, please open an issue on GitHub.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/feteu/asgi-request-duration",
    "name": "asgi-request-duration",
    "maintainer": "Fabio Greco",
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": "fabio.greco.github@gmail.com",
    "keywords": "asgi, async, connexion, fastapi, middleware, request-duration, starlette, timing",
    "author": "Fabio Greco",
    "author_email": "fabio.greco.github@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/05/38/de1887ae16c3a9c7665e68ee070e600c7e40723ed3e0a864adf8de33a4c1/asgi_request_duration-1.0.3.tar.gz",
    "platform": null,
    "description": "[![PyPI - License](https://img.shields.io/pypi/l/asgi-request-duration)](https://www.gnu.org/licenses/gpl-3.0)\n[![PyPI - Version](https://img.shields.io/pypi/v/asgi-request-duration.svg)](https://pypi.org/project/asgi-request-duration/)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/asgi-request-duration)](https://pypi.org/project/asgi-request-duration/)\n[![PyPI - Status](https://img.shields.io/pypi/status/asgi-request-duration)](https://pypi.org/project/asgi-request-duration/)\n[![Dependencies](https://img.shields.io/librariesio/release/pypi/asgi-request-duration)](https://libraries.io/pypi/asgi-request-duration)\n[![Last Commit](https://img.shields.io/github/last-commit/feteu/asgi-request-duration)](https://github.com/feteu/asgi-request-duration/commits/main)\n[![Build Status build/testpypi](https://img.shields.io/github/actions/workflow/status/feteu/asgi-request-duration/publish-testpypi.yaml?label=publish-testpypi)](https://github.com/feteu/asgi-request-duration/actions/workflows/publish-testpypi.yaml)\n[![Build Status build/pypi](https://img.shields.io/github/actions/workflow/status/feteu/asgi-request-duration/publish-pypi.yaml?label=publish-pypi)](https://github.com/feteu/asgi-request-duration/actions/workflows/publish-pypi.yaml)\n[![Build Status test](https://img.shields.io/github/actions/workflow/status/feteu/asgi-request-duration/test.yaml?label=test)](https://github.com/feteu/asgi-request-duration/actions/workflows/test.yaml)\n\n# ASGI Request Duration \u23f1\ufe0f\n\nASGI Request Duration is a middleware for ASGI applications that measures the duration of HTTP requests and integrates this information into response headers and log records. This middleware is designed to be easy to integrate and configure, providing valuable insights into the performance of your ASGI application.\n\n## Table of Contents \ud83d\udcda\n\n- [Features](#features)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Middleware](#middleware)\n  - [Logging Filter](#logging-filter)\n  - [Configuration](#configuration)\n- [Examples](#examples)\n- [Development](#development)\n- [License](#license)\n- [Contributing](#contributing)\n- [Contact](#contact)\n\n## Features \u2728\n\n- Measure the duration of each HTTP request.\n- Add the request duration to response headers.\n- Integrate the request duration into log records.\n- Configurable header name and precision.\n- Exclude specific paths from timing.\n\n## Installation \ud83d\udee0\ufe0f\n\nYou can install the package using pip:\n\n```bash\npip install asgi-request-duration\n```\n\n## Usage \ud83d\ude80\n\n### Middleware\n\nTo use the middleware, add it to your ASGI application:\n\n```python\nfrom asgi_request_duration.middleware import RequestDurationMiddleware\nfrom starlette.applications import Starlette\n\napp = Starlette()\napp.add_middleware(RequestDurationMiddleware)\n```\n\n### Logging Filter\n\nTo use the logging filter, configure your logger to use the `RequestDurationFilter`:\n\n```python\nimport logging\nfrom asgi_request_duration.filters import RequestDurationFilter\n\nlogging.basicConfig(level=logging.INFO)\nlogger = logging.getLogger(\"myapp\")\nlogger.addFilter(RequestDurationFilter())\n```\n\n### Configuration \u2699\ufe0f\n\n#### Middleware Configuration\n\nYou can configure the middleware by passing parameters to the `RequestDurationMiddleware`:\n\n- `excluded_paths`: List of paths to exclude from timing.\n- `header_name`: The name of the header to store the request duration.\n- `precision`: The precision of the recorded duration.\n- `skip_validate_header_name`: Flag to skip header name validation.\n- `skip_validate_precision`: Flag to skip precision validation.\n\nExample:\n\n```python\napp.add_middleware(\n    RequestDurationMiddleware,\n    excluded_paths=[\"/health\"],\n    header_name=\"X-Request-Duration\",\n    precision=3,\n    skip_validate_header_name=False,\n    skip_validate_precision=False\n)\n```\n\n#### Logging Filter Configuration\n\nYou can configure the logging filter by passing parameters to the `RequestDurationFilter`:\n\n- `context_key`: The key to retrieve the request duration context value.\n- `default_value`: The default value if the request duration context key is not found.\n\nExample:\n\n```python\nlogger.addFilter(RequestDurationFilter(context_key=\"request_duration\", default_value=\"-\"))\n```\n\n## Examples \ud83d\udcd6\n\nHere are complete examples of how to use the middleware with Starlette applications. You can find the full example code in the [examples](examples) folder.\n\n## Development \ud83d\udc69\u200d\ud83d\udcbb\ud83d\udc68\u200d\ud83d\udcbb\n\n### Requirements\n\n- Python 3.11+\n- Poetry\n\n### Setup\n\nClone the repository and install the dependencies:\n\n```sh\ngit clone https://github.com/yourusername/asgi-request-duration.git\ncd asgi-request-duration\npoetry install\n```\n\n### Running Tests \ud83e\uddea\n\nYou can run the tests using `pytest`:\n\n```sh\npoetry run pytest\n```\n\n## License \ud83d\udcdc\n\nThis project is licensed under the GNU GPLv3 License. See the [LICENSE](LICENSE) file for more details.\n\n## Contributing \ud83e\udd1d\n\nContributions are welcome! Please read the [CONTRIBUTING](CONTRIBUTING.md) file for guidelines on how to contribute to this project.\n\n## Contact \ud83d\udcec\n\nFor any questions or suggestions, please open an issue on GitHub.\n\n",
    "bugtrack_url": null,
    "license": "GNU GPLv3",
    "summary": "A asgi middleware to measure the request duration",
    "version": "1.0.3",
    "project_urls": {
        "Homepage": "https://github.com/feteu/asgi-request-duration",
        "Repository": "https://github.com/feteu/asgi-request-duration"
    },
    "split_keywords": [
        "asgi",
        " async",
        " connexion",
        " fastapi",
        " middleware",
        " request-duration",
        " starlette",
        " timing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "06da064565812a1cf562bec6659c8f89e718724c208137ef871a19c1689c9c33",
                "md5": "b79c6ee7fd172861ad5aeff00c4e7256",
                "sha256": "ac582fc8a3b03137f013dac36d891e0d29da8c6d28b8a8a72676069121b62081"
            },
            "downloads": -1,
            "filename": "asgi_request_duration-1.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b79c6ee7fd172861ad5aeff00c4e7256",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 20131,
            "upload_time": "2024-12-28T09:06:25",
            "upload_time_iso_8601": "2024-12-28T09:06:25.319730Z",
            "url": "https://files.pythonhosted.org/packages/06/da/064565812a1cf562bec6659c8f89e718724c208137ef871a19c1689c9c33/asgi_request_duration-1.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0538de1887ae16c3a9c7665e68ee070e600c7e40723ed3e0a864adf8de33a4c1",
                "md5": "25b6129760827eeaf0d01a1bf6f316fd",
                "sha256": "2869041d4a800329d9e47af8685c951b0d9531329d2d1c5d1c4e4a75d754e0ce"
            },
            "downloads": -1,
            "filename": "asgi_request_duration-1.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "25b6129760827eeaf0d01a1bf6f316fd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 19146,
            "upload_time": "2024-12-28T09:06:27",
            "upload_time_iso_8601": "2024-12-28T09:06:27.879702Z",
            "url": "https://files.pythonhosted.org/packages/05/38/de1887ae16c3a9c7665e68ee070e600c7e40723ed3e0a864adf8de33a4c1/asgi_request_duration-1.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-28 09:06:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "feteu",
    "github_project": "asgi-request-duration",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "asgi-request-duration"
}
        
Elapsed time: 0.52185s