Skip to main content

Elasticsearch Logger Middleware for FastAPI

Elasticsearch Logger Middleware for FastAPI is designed to facilitate detailed and efficient logging directly to Elasticsearch servers from FastAPI applications. It streamlines the logging processes by capturing detailed HTTP requests data and automates telemetry to provide insights into application performance.

Key Features

  1. Elasticsearch Integration: Send logs directly to an Elasticsearch server.
  2. Flexible Configuration: Configure to suit various environment needs.
  3. Automated Detailed Data Logging: Automatically captures headers, query parameters, and the request body.
  4. Effortless Telemetry: Automatically generates telemetry for monitoring request times.
  5. Enhanced Data Control: Limit payload sizes for efficient Elasticsearch indexing.

Installation

To install this middleware, ensure you have Python installed and then run:

pip install fastapi-elasticsearch-middleware

Usage

Configure the middleware by creating a config dictionary in your FastAPI application as follows:

config = {
    "url": "http://localhost:9200",
    "user": "elasticsearch_user",
    "password": "password",
    "index": "my_app_logs",
    "environment": "development",
    "limit": True,
    "debug": False,
    "request_timeout": 1,
    "timeout_max_retries": 3,
    "timeout_wait_seconds": 60,
}

The debug parameter prevents sending logs to Elasticsearch.

Integrate the middleware into your FastAPI application like this:

from fastapi import FastAPI
from fastapi_elasticsearch_middleware.elasticsearch_middleware import (
    ElasticsearchLoggerMiddleware,
)

app = FastAPI()
app.add_middleware(ElasticsearchLoggerMiddleware, config=config)

How It Works

The middleware captures each HTTP request, processes it, and sends log data to your Elasticsearch server based on the configured parameters. It includes detailed logs of request headers, body data, and response metrics.

Elasticsearch timeouts

By default, each log submission uses a one-second client request timeout with automatic client retries disabled. After three consecutive submission timeouts, the middleware pauses Elasticsearch logging for 60 seconds. Configure these optional settings in config:

Parameter Default Meaning
request_timeout 1 Client request timeout in seconds; finite positive number.
timeout_max_retries 3 Consecutive submission timeouts before pausing; positive integer. This does not retry the same log.
timeout_wait_seconds 60 Pause duration in seconds; finite positive number.

Invalid values raise ValueError when the middleware is initialized. Logs received during this pause are discarded; they are not queued or retried.

The first log after the pause probes Elasticsearch. A successful submission resets the timeout counter. Another timeout immediately starts a new pause of the configured duration. Other errors are logged locally and reset the consecutive-timeout counter without starting a pause. Logging failures do not propagate to the application.

The Elasticsearch client is synchronous, so a submission can block the application's event loop while it waits. The cooldown reduces repeated waits during timeout outages but does not make logging asynchronous. Cooldown state belongs to each middleware instance and is not shared between worker processes.

How to Contribute

We welcome contributions from the community and are pleased to have you join us. Here are some guidelines that will help you get started.

Prerequisites

Before contributing, please ensure you have the following:

  • A basic understanding of Python and FastAPI.
  • A basic understanding of Middlewares.

Setting Up Your Development Environment

  1. Fork the Repository: Start by forking the repository to your GitHub account.
  2. Clone the Repository: Clone your fork to your local machine.
    git clone https://github.com/GGontijo/fastapi-elasticsearch-middleware
    cd repository-name
    
  3. Install Dependencies: Install the required dependencies.
    pip install -r requirements.txt
    

Making Changes

  1. Create a New Branch: Create a new branch for your feature or bug fix.
    git checkout -b awesome-feature
    
  2. Make Your Changes: Implement your feature or fix a bug. Be sure to adhere to the coding standards and include comments where necessary.

Submitting a Pull Request

  1. Commit Your Changes: Once your tests pass, commit your changes.
    git commit -m 'Add some feature'
    
  2. Push to GitHub: Push your changes to your fork on GitHub.
    git push origin awesome-feature
    
  3. Open a Pull Request: Go to the original repository and click the Compare & pull request button. Then submit your pull request with a clear title and description.

Code Review

Once your pull request is opened, it will be reviewed by the maintainers. Some changes may be requested. Please be patient and responsive. Once the pull request has been approved, it will be merged into the master branch.

Thank you for contributing!

Metadata

Release files for fastapi-elasticsearch-middleware 1.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-elasticsearch-middleware 1.1.9
File Size Uploaded Provenance Actions
fastapi_elasticsearch_middleware-1.1.9.tar.gz 8.2 kB Without provenance Details for fastapi_elasticsearch_middleware-1.1.9.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-elasticsearch-middleware 1.1.9
File Interpreter ABI Platform Actions
fastapi_elasticsearch_middleware-1.1.9-py3-none-any.whl Python 3 none any Details for fastapi_elasticsearch_middleware-1.1.9-py3-none-any.whl

Total release size: 16.6 kB

Release files / fastapi_elasticsearch_middleware-1.1.9.tar.gz

Download URL fastapi_elasticsearch_middleware-1.1.9.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
004e538f01e72f6997af06c929ad49aab225fb0c3c516ca1e6604f49348c1bc2
BLAKE2b-256 checksum
How to use checksums
e640ead504b9b2d00db02b0f05b054a719c02a1c053861844ccebc5f92359436
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / fastapi_elasticsearch_middleware-1.1.9-py3-none-any.whl

Download URL fastapi_elasticsearch_middleware-1.1.9-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a9c88697fb238ec2986cf5f9309d6d0906cf75c0ca448c8084c356ba4d2f8df
BLAKE2b-256 checksum
How to use checksums
7e00afd6400def0c3acfb6a350cbdb2f1c226d5ce719d832bb64c449720adca4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Security for fastapi-elasticsearch-middleware 1.1.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page