Skip to main content

Prometheus metrics exporter for Starlette applications.

Project description

starlette_exporter

Prometheus exporter for Starlette and FastAPI.

The middleware collects basic metrics:

  • Counter: starlette_requests_total
  • Histogram: starlette_request_duration_seconds

Metrics include labels for the HTTP method, the path, and the response status code.

starlette_requests_total{method="GET",path="/",status_code="200"} 1.0
starlette_request_duration_seconds_bucket{le="0.01",method="GET",path="/",status_code="200"} 1.0

Use the HTTP handler handle_metrics at path /metrics to expose a metrics endpoint to Prometheus.

Usage

pip install starlette_exporter

Starlette

        from starlette.applications import Starlette
        from starlette_exporter import PrometheusMiddleware, handle_metrics

        app = Starlette()
        app.add_middleware(PrometheusMiddleware)
        app.add_route("/metrics", handle_metrics)

        ...

FastAPI

        from fastapi import FastAPI
        from starlette_exporter import PrometheusMiddleware, handle_metrics

        app = FastAPI()
        app.add_middleware(PrometheusMiddleware)
        app.add_route("/metrics", handle_metrics)

        ...

Options

app_name: Sets the value of the app_name label for exported metrics (default: starlette).

group_paths: setting this to True will populate the path label using named parameters (if any) in the router path, e.g. /api/v1/items/{item_id}. This will group requests together by endpoint (regardless of the value of item_id). This option may come with a performance hit for larger routers. Default is False, which will result in separate metrics for different URLs (e.g., /api/v1/items/42, /api/v1/items/43, etc.).

prefix: Sets the prefix of the exported metric names (default: starlette).

Example:

app.add_middleware(PrometheusMiddleware, app_name="hello_world", group_paths=True, prefix='myapp')

Developing

git clone https://github.com/stephenhillier/starlette_exporter
cd starlette_exporter
pytest tests

License

Code released under the Apache License, Version 2.0.

Dependencies

https://github.com/prometheus/client_python

https://github.com/encode/starlette

Credits

Starlette - https://github.com/encode/starlette

FastAPI - https://github.com/tiangolo/fastapi

Flask exporter - https://github.com/rycus86/prometheus_flask_exporter

Alternate Starlette exporter - https://github.com/perdy/starlette-prometheus

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_exporter-0.5.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

starlette_exporter-0.5.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file starlette_exporter-0.5.0.tar.gz.

File metadata

  • Download URL: starlette_exporter-0.5.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.5

File hashes

Hashes for starlette_exporter-0.5.0.tar.gz
Algorithm Hash digest
SHA256 13975c717de3406d0628f85383b65b72f3c44d5fa05d6799a3be573c283f62a8
MD5 61b71bbd130cd99d5d0f9ca9ce8d65c5
BLAKE2b-256 f9b97a85de3c0e2f7e52a958358acea4c52e317672d26648624659755cdc119b

See more details on using hashes here.

Provenance

File details

Details for the file starlette_exporter-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: starlette_exporter-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.5

File hashes

Hashes for starlette_exporter-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e027d91af4a042a3b36bdb84e234b12f2ae291f85c6d2b7915519a9899d1ec57
MD5 a8abeedd8ccbb2e15b4b97dfd0af06b7
BLAKE2b-256 ca9eda957b8ae729b3b58ed4bb49b3d89fc01617fb6d994757fc5433d8905fed

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page