Skip to main content

Instrument your FastAPI with Prometheus metrics

Project description

Prometheus FastAPI Instrumentator

PyPI version Maintenance downloads

release test branches codecov Code style: black

Instruments your FastAPI with Prometheus metrics. Install with:

pip install prometheus-fastapi-instrumentator

Fast Track

from prometheus_fastapi_instrumentator import Instrumentator
Instrumentator().instrument(app).expose(app)

With this single line FastAPI is instrumented and all Prometheus metrics used in the FastAPI app can be scraped via the added /metrics endpoint.

The exporter includes the single metric http_request_duration_seconds. Everything around it can be configured and deactivated. These options include:

  • Status codes are grouped into 2xx, 3xx and so on.
  • Requests without a matching template are grouped into the handler none.
  • Renaming of labels and the metric.
  • Regex patterns to ignore certain routes.
  • Rounding of latencies.

See the Example with all parameters for all possible options or check out the documentation itself.

Example with all parameters

from prometheus_fastapi_instrumentator import PrometheusFastApiInstrumentator
PrometheusFastApiInstrumentator(
    should_group_status_codes=False,
    should_ignore_untemplated=True,
    should_group_untemplated=False,
    should_round_latency_decimals=True,
    excluded_handlers=["/metrics", "/admin"],
    buckets=[1, 2, 3, 4, 5],
    metric_name="my_custom_metric_name",
    label_names=("method_type", "path", "status_code",),
    round_latency_decimals=3,
).instrument(app).expose(app, "/prometheus_metrics")

instrument: Instruments the given FastAPI based on the configuration in the constructur of the exporter class.

expose: Completely separate from instrument and not necessary for instrumentation. Just a simple option to expose metrics by adding an endpoint to the given FastAPI. Supports multiprocess mode.

Prerequesites

  • python = "^3.6" (tested with 3.6 and 3.8)
  • fastapi = ">=0.38.1, <=1.0.0" (tested with 0.38.1 and 0.59.0)
  • prometheus-client = "^0.8.0" (tested with 0.8.0)

Development

Developing and building this package on a local machine requires Python Poetry. I recommend to run Poetry in tandem with Pyenv. Once the repository is cloned, run poetry install and poetry shell. From here you may start the IDE of your choice.

For formatting, the black formatter is used. Run black . in the repository to reformat source files. It will respect the black configuration in the pyproject.toml.

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

Built Distribution

File details

Details for the file prometheus-fastapi-instrumentator-1.2.0.tar.gz.

File metadata

File hashes

Hashes for prometheus-fastapi-instrumentator-1.2.0.tar.gz
Algorithm Hash digest
SHA256 1e62da74d07c0602f4e23b5b0a98bb16df2894d24ee0f7ea8ea308c2ed7940e8
MD5 e43ab9d393e4fa284fc36ba2c8ddd50d
BLAKE2b-256 4d4dbe7caefd20d5fd94a3bbb79fb182f9b2e2c2f2b2f7a7540cf7c75a0afb97

See more details on using hashes here.

Provenance

File details

Details for the file prometheus_fastapi_instrumentator-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for prometheus_fastapi_instrumentator-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 15be248374bea2301fca810f94a12c3e492307bcc75cabb41955484e2c40c4bc
MD5 b5674c819659687b34db73a9126cfdaf
BLAKE2b-256 5cbbb8d260f82cdc1cb7058ba026676d2c07aa9327f4fdc3a7e8cfb500107e6e

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