Instrument your FastAPI with Prometheus metrics
Project description
Prometheus FastAPI Instrumentator
Instrument 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
of
the type Histogram. A separate http_requests_total
isn't necessary as the
total can be retrieved with the http_requests_total_count
series.
The Prometheus FastAPI Instrumentator (any idea for a short hand?) is highly configurable and has few handy features.
- Opt-out (activated by default):
- Status codes are grouped into
2xx
,3xx
and so on. - Requests without a matching template are grouped into the handler
none
. - Regex patterns to ignore certain routes.
- Status codes are grouped into
- Opt-in (Deactivated by default):
- Control instrumentation and exposition of FastAPI at runtime by setting an environment variable.
- Rounding of latencies to a certain decimal number.
- Completely ignore untemplated routes.
- Renaming of labels and the metric.
See the Example with all parameters for all possible options.
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,
should_respect_env_var=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,
env_var_name="ENABLE_METRICS",
).instrument(
app
).expose(
app,
endpoint="/prometheus_metrics",
include_in_schema=False
)
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
. For more information just
take a look at the GitHub workflow files.
Project details
Release history Release notifications | RSS feed
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
Hashes for prometheus-fastapi-instrumentator-2.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9db7c687b2807ede3b322872d2c9541dd05dbe6ce9e27d972526eb9a91afc692 |
|
MD5 | 891b3dfaff1bb6aeaf06a66d7a7403bd |
|
BLAKE2b-256 | 085d6171a027012c9c31471713a048d1f2852d79a92e5442a8fc1107e42fb2fc |
Hashes for prometheus_fastapi_instrumentator-2.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6753a12e2d3c18b3d39e75e376350327a960eef799e73be1d444990edcee361d |
|
MD5 | cca8b1be261171430a30e489460f4bd7 |
|
BLAKE2b-256 | e2533caafdf570349406b8384d61d191e140255912877f78880344f403cc08d6 |