Prometheus Fastapi Instrumentator Versions Save

Instrument your FastAPI with Prometheus metrics.

v7.0.0

1 month ago

Major release with a single breaking change: Python 3.7 is not supported anymore. Beyond that, three improvements based on various pull requests.

Added

  • Instrumentator now works without FastAPI. This is possible because every FastAPI app is also a Starlette app (but not the other way around). Or to be more specific: FastAPI uses Starlette for things like routing and middleware this package relies on. The change is backwards compatible, even type checkers like mypy should continue working. Thanks to @mvanderlee for proposing this in #280 and implementing it in #288.

  • Middleware also records duration without streaming in addition to the already existing total latency (i.e. the time consumed for streaming is not included in the duration value). The differentiation can be valuable as it shows the time to first byte.

    This mode is opt-in and can be enabled / used in several ways: The Instrumentator() constructor, the metrics.default() closure, and the metrics.latency() closure now come with the flag should_exclude_streaming_duration. The attribute modified_duration_without_streaming has been added to the metrics.Info class. Instances of metrics.Info are passed to instrumentation functions, where the added value can be used to set metrics.

    Thanks to @dosuken123 for proposing this in #291 and implementing it in #290.

  • Relaxed type of get_route_name() argument to HTTPConnection. This allows developers to use the get_route_name function for getting the name of websocket routes as well. Thanks to @pajowu for proposing and implementing this feature in #276.

Removed

  • BREAKING: Dropped support for Python 3.7 which is has reached end-of-life.

v6.1.0

9 months ago

Added

  • Added label method to metric http_request_duration_seconds from default metrics. Thanks to @alcidesmig for implementing this in #251.

v6.0.0

1 year ago

Small release with a small breaking change leading to a big version change following semantic versioning.

Breaking change only affects users that have custom instrumentations that access info.response.body, a feature introduced with 5.10.0 few weeks ago. See below for more information.

Added

  • BREAKING: Disabled passing response body to instrumentation functions. Moved behind whitelist that is empty by default. Changes a feature introduced with 5.10.0. Only affects users that have custom instrumentations that access info.response.body.

    Opt-in via new parameter body_handlers added to instrumentator constructor. Parameter takes list of pattern strings to match handlers. For old behavior, pass argument [r".*"] to match all handlers:

    instrumentator = Instrumentator(body_handlers=[r".*"])
    

    Motivation for change: Collecting body negatively impacts performance of responses with largish body.

    Thanks to @bbeattie-phxlabs for raising this issue in #234 and implementing it in #233 / #238.

v5.11.2

1 year ago

Fixed

  • Fixed info.response.body in instrumentation functions being wrongfully empty if response is not streamed. Affects a feature that was introduced with release 5.10.0 few weeks ago. Closed issue #236 and implemented in pull request #237.

v5.11.1

1 year ago

Fixed

  • Improved typing hints and enabled stricter rules for MyPy. Thanks to @tomtom103 for implementing this in #231.

v5.11.0

1 year ago

Minor release containing several fixes and a small enhancement. Fixes are related to multi process mode, a regression introduced with the previous release, and errors that started to occur with current versions of Starlette and FastAPI.

Ask or discuss anything quick about the release in the discussion #221.

Added

  • Adjusted the add() method to accept an arbitrary number of instrumentation functions as arguments instead of a single one. Non-breaking change. Implemented in pull request #230.

Fixed

  • Fixed multi process mode in expose() method that handles the /metrics endpoint. Due to reusing the registry assigned to the instrumentator it could lead to duplicated metrics. Now the endpoint follows recommendation from Prometheus client library documentation. Also improved multi process unit tests. Closed issue #228 and #227. Fixed in pull request #229.

  • Fixed NameError and "Duplicated timeseries..." errors that started to occur with latest versions of Starlette / FastAPI in combination with multiple middlewares. Instrumentation closures are now optional and the instrumentator handles this accordingly. Thanks to @alexted and others for reporting errors. Thanks to @frankie567 for pointing out the change in Starlette. Related to pull request #153 and issue #214. Closed issue #219. Done in pull request #220.

  • Added missing registry parameter to remaining metrics functions. This enables passing custom registry to other metrics functions than default. Related to pull request #153. Closed issue #219. Done in pull request #220.

v5.10.0

1 year ago

First release in several months. Includes new features and fixes from various contributors. Notable changes that might have an impact on existing setups is the automatic instrumentation of mounted apps and the deprecation of the lowercase prometheus_multiproc_dir environment variable.

Ask or discuss anything quick about the release in the discussion #221.

Added

  • Added smart handling of mounted apps. Previously the URL handler logic did not handle mounted apps and always returned just the prefix in that case. Based on code from elastic/apm-agent-python licensed under the permissive BSD-3-Clause License. Thanks to @LordGaav for proposing this enhancement / fix and implementing it in #208. Related to issues #31 and #121.

  • Added optional parameters metric_namespace and metric_subsystem to instrument() method to configure namespace and subsystem for all metric names. Check the README.md for more information. Thanks to @phbernardes for proposing this enhancement and implementing it in #193.

  • Added passthrough of body to info.response. This enables metrics that work based on data in the body. Thanks to everyone who brought this up in #76 and to @HadilD for implementing it in #203.

  • Allowed passing a custom registry to be used instead of using the default one. This would be useful in particular when testing multiple FastAPI apps (e.g. microservices) in the same tests run. Note that there are issues with the current implementation in certain corner cases. Thanks to @tiangolo for for proposing this enhancement and implementing it in #153.

  • Environment variable used by should_respect_env_var (default ENABLE_METRICS) now accepts truthy values like 1 and true and not just True. Thanks to @chbndrhnns for proposing this enhancement in #27 and implementing it in #28.

  • Added support for asynchronous instrumentation functions. The add() method now accepts them in addition to "normal" functions and the instrumentator middleware will await them appropriately. Thanks to @AndreasPB for for proposing this enhancement and implementing it in #61. Thanks to @Skeen for contributing to the discussion.

Changed

  • Licensed part of the project under the BSD-3-Clause License. This is due to code being used from a repo licensed under BSD-3-Clause (see the "Added" section). The default ISC License and the BSD-3-Clause License are both permissive. So there should be no user impact.

Fixed

  • Fixed status code in metric being "Hxx" when http.HTTPStatus enumeration is used in combination with grouping of status codes. Thanks to @Leem0sh and others for raising the issue in #190. Thanks to @nikstuckenbrock and @blag for fixing it in #192.

  • Fixed documentation in main README on how to use prometheus-fastapi-instrumentator with current versions of FastAPI. Related to issues #214 and #80. Thanks to @alfaro28 and @harochau.

Deprecated

  • Deprecated environment variable prometheus_multiproc_dir and replaced it with PROMETHEUS_MULTIPROC_DIR. This matches the behavior of the Prometheus Python client library. This fixes #89 and #50. Thanks to all the people who brought this up. Thanks to @michaelusner for implementing the deprecation in #42 / #217.

v5.9.1

1 year ago

5.9.1 (2022-08-23)

🍀 Summary 🍀

No bug fixes or new features. Just an important improvement of the documentation.

✨ Highlights ✨

  • Fix / Improve documentation of how to use package (#168). Instrumentation should happen in a function decorated with @app.on_event("startup") to prevent crashes on startup. Thanks to @mdczaplicki and others.

CI/CD

  • Pin poetry version and improve caching configuration (6337459)

Docs

  • Improve example in README on how to instrument app (#168) (dc36aac)

v5.9.0

1 year ago

5.9.0 (2022-08-23)

🍀 Summary 🍀

This release fixes a small but annoying bug. Beyond that the release includes small internal improvements and bigger changes to CI/CD.

✨ Highlights ✨

  • Removed print statement polluting logs (#157). Thanks to all the people raising this issue and to @nikstuckenbrock for fixing it.
  • Added py.typed file to package to improve typing annotations (#137). Thanks to @mmaslowskicc for proposing and implementing this.
  • Changed license from MIT to ISC, which is just like MIT but shorter.
  • Migrated from Semantic Release to Release Please as release management tool.
  • Overall refactoring of project structure to match my (@trallnag) template Python repo.
  • Several improvements to the documentation. Thanks to @jabertuhin, @frodrigo, and @murphp15.
  • Coding style improvements (#155). Replaced a few for loops with list comprehensions. Defaulting an argument to None instead of an empty list. Thanks to @yezz123.

Features

  • Add py.typed for enhanced typing annotations (#37) (0c67d1b)

Bug Fixes

  • Remove print statement from middleware (#157) (f89792b)

Build

  • deps-dev: bump devtools from 0.8.0 to 0.9.0 (#172) (24bb060)
  • deps-dev: bump flake8 from 4.0.1 to 5.0.4 (#179) (8f72053)
  • deps-dev: bump mypy from 0.950 to 0.971 (#174) (60e324f)

Docs

  • Add missing colon to README (#33) (faef24c)
  • Adjust changelog formatting (b8b7b3e)
  • Fix small typo in readme (#154) (a569d4e)
  • Move docs-internal to docs/devel and adjust contributing (1b446ca)
  • Remove obsolete DEVELOPMENT.md (1c18ff7)
  • Switch license from MIT to ISC (1b0294a)

CI/CD

  • Add .tool-versions (255ba97)
  • Add codecov.yaml (008ef61)
  • Add explicit codecov token (b264184)
  • Adjust commitlint to allow more subject case types (8b630aa)
  • Correct default branch name (5f141c5)
  • Improve and update scripts (e1d9982)
  • Move to Release Please and refactor overall CI approach (9977665)
  • Remove flake8 ignore W503 (6eab3b8)
  • Remove traces of semantic-release (f0ab8ff)
  • Remove unnecessary include of py.typed from pyproject.toml (#37) (bbad45e)
  • Rename poetry repo for TestPyPI (3f1c500)
  • Restructure poetry project layout (b439ceb)
  • Update gitignore (e0fa528)
  • Update pre-commit config (e725750)

Refactor

v5.8.2

1 year ago

5.8.2 (2022-06-12)

Refactored the middleware to an ASGI implementation. Thanks to @Kludex and @adriangb. #139.