Skip to main content

Instruments Flask API transparently

Project description

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.

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

prometheus-flask-instrumentator-4.1.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file prometheus-flask-instrumentator-4.1.1.tar.gz.

File metadata

File hashes

Hashes for prometheus-flask-instrumentator-4.1.1.tar.gz
Algorithm Hash digest
SHA256 4e64451400304c709a7c6b8b537bf7fab2bb629484b32516f780e1a8f8ef69ae
MD5 adbdd34d333e1ea750a8ed750a30c510
BLAKE2b-256 51888b1ef127287be589e17ba3ce6b8fca71df1b9369bc7f7dd834ada7e3c175

See more details on using hashes here.

File details

Details for the file prometheus_flask_instrumentator-4.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for prometheus_flask_instrumentator-4.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8122778b26052bf59cddb5ceb690b8f7deede3b1e9d3014dbbef08ac3f7471b9
MD5 06c9fd5061ad36ae920a8bea6a07ad47
BLAKE2b-256 377e0b22a3a027b90a9f099000b89b1588f88c90ab3f85f7579d45570fceae52

See more details on using hashes here.

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