Skip to main content

Prometheus Flask Instrumentator

PyPI version Maintenance downloads

release test branches codecov Code style: black

Small package to instrument your Flask app transparently.

pip install prometheus-flask-instrumentator

Fast Track

from prometheus_flask_instrumentator import Instrumentator

Instrumentator().instrument(app).expose(app)

With this the Flask app is instrumented and all Prometheus metrics can be scraped via the /metrics endpoint.

The exporter includes the single metric http_request_duration_seconds. Basically 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.
  • Decimal 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_flask_instrumentator import PrometheusFlaskInstrumentator

PrometheusFlaskInstrumentator(
    should_group_status_codes=False,
    should_ignore_untemplated=False,
    should_group_untemplated=False,
    should_round_latency_decimals=True,
    excluded_handlers=[
        "admin",            # Unanchored regex.
        "^/secret/.*$"],    # Full regex example.  
    buckets=(1, 2, 3, 4,),
    metric_name="flask_http"
    label_names=("flask_method", "flask_handler", "flask_status",),
    round_latency_decimals=3,
).instrument(app).expose(app, "/prometheus_metrics")

It is important to notice that you don't have to use the expose() method if adding the endpoint directly to the Flask app does not suit you. There are many other ways to expose the metrics.

The defaults are the following:

should_group_status_codes: bool = True,
should_ignore_untemplated: bool = False,
should_group_untemplated: bool = True,
should_round_latency_decimals: bool = False,
excluded_handlers: list = ["/metrics"],
buckets: tuple = Histogram.DEFAULT_BUCKETS,
metric_name: str = "http_request_duration_seconds",
label_names: tuple = ("method", "handler", "status",),
round_latency_decimals: int = 4,

Prerequesites

  • python = "^3.6" (tested with 3.6 and 3.8)
  • flask = "^1" (tested with 1.1.2)
  • 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.

Release files for prometheus-flask-instrumentator 4.1.1

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

Source distribution (sdist)

Source distribution for prometheus-flask-instrumentator 4.1.1
File Size Uploaded
prometheus-flask-instrumentator-4.1.1.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for prometheus-flask-instrumentator 4.1.1
File Interpreter ABI Platform
prometheus_flask_instrumentator-4.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / prometheus-flask-instrumentator-4.1.1.tar.gz

Download URL prometheus-flask-instrumentator-4.1.1.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4e64451400304c709a7c6b8b537bf7fab2bb629484b32516f780e1a8f8ef69ae
BLAKE2b-256 checksum
How to use checksums
51888b1ef127287be589e17ba3ce6b8fca71df1b9369bc7f7dd834ada7e3c175
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.5 Linux/5.3.0-1034-azure

Release files / prometheus_flask_instrumentator-4.1.1-py3-none-any.whl

Download URL prometheus_flask_instrumentator-4.1.1-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8122778b26052bf59cddb5ceb690b8f7deede3b1e9d3014dbbef08ac3f7471b9
BLAKE2b-256 checksum
How to use checksums
377e0b22a3a027b90a9f099000b89b1588f88c90ab3f85f7579d45570fceae52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.5 Linux/5.3.0-1034-azure

Release history Release notifications | RSS feed

20.7.5

2 release files

This release

4.1.1 This release

2 release files

4.1.0

2 release files

4.0.0

2 release files

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