Skip to content

Commit

Permalink
fix: Fix regression multi app Prometheus registry (#220)
Browse files Browse the repository at this point in the history
* chore: Enter regression with upgrade

* chore: Potential fix

* fix: Check for function being None before adding

This reflects that now the metric functions can either instrumentation
function closure or None.

Also add missing registry param to metrics functions.

* test: Adjust tests for multiple apps

* chore: Update deps in lock

Relevant because things started to fail with fastapi 0.92.0.

* chore: Improve metrics

* docs: Update CHANGELOG.md
  • Loading branch information
trallnag authored Mar 8, 2023
1 parent a9e21ac commit c858592
Show file tree
Hide file tree
Showing 8 changed files with 680 additions and 444 deletions.
24 changes: 23 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,29 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0).

## Unreleased

Nothing.
### Fixed

- 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](https://github.com/alexted) and
others for reporting errors. Related to pull request
[#153](https://github.com/trallnag/prometheus-fastapi-instrumentator/pull/153)
and issue
[#214](https://github.com/trallnag/prometheus-fastapi-instrumentator/issues/214).
Closed issue
[#219](https://github.com/trallnag/prometheus-fastapi-instrumentator/issues/219).
Done in pull request
[#220](https://github.com/trallnag/prometheus-fastapi-instrumentator/pull/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](https://github.com/trallnag/prometheus-fastapi-instrumentator/pull/153).
Closed issue
[#219](https://github.com/trallnag/prometheus-fastapi-instrumentator/issues/219).
Done in pull request
[#220](https://github.com/trallnag/prometheus-fastapi-instrumentator/pull/220).

## [5.10.0](https://github.com/trallnag/prometheus-fastapi-instrumentator/compare/v5.9.1...v5.10.0) / 2023-02-26

Expand Down
497 changes: 286 additions & 211 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ keywords = ["prometheus", "instrumentation", "fastapi", "exporter", "metrics"]
python = ">= 3.7.0, < 4.0.0"
fastapi = ">= 0.38.1, < 1.0.0"
prometheus-client = ">= 0.8.0, < 1.0.0"
starlette = "0.25.0"

[tool.poetry.group.dev.dependencies]
httpx = "^0.23.1"
Expand Down
24 changes: 15 additions & 9 deletions src/prometheus_fastapi_instrumentator/instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ def metrics(request: Request):

def add(
self,
instrumentation_function: Callable[[metrics.Info], Union[None, Awaitable[None]]],
instrumentation_function: Optional[
Callable[[metrics.Info], Union[None, Awaitable[None]]]
],
):
"""Adds function to list of instrumentations.
Expand All @@ -283,14 +285,18 @@ def add(
self: Instrumentator. Builder Pattern.
"""

if asyncio.iscoroutinefunction(instrumentation_function):
self.async_instrumentations.append(
cast(Callable[[metrics.Info], Awaitable[None]], instrumentation_function)
)
else:
self.instrumentations.append(
cast(Callable[[metrics.Info], None], instrumentation_function)
)
if instrumentation_function:
if asyncio.iscoroutinefunction(instrumentation_function):
self.async_instrumentations.append(
cast(
Callable[[metrics.Info], Awaitable[None]],
instrumentation_function,
)
)
else:
self.instrumentations.append(
cast(Callable[[metrics.Info], None], instrumentation_function)
)

return self

Expand Down
Loading

0 comments on commit c858592

Please sign in to comment.