From 370833298d51dd29f8267eca596366987fac1ac9 Mon Sep 17 00:00:00 2001 From: Vladimir Gladkii Date: Thu, 29 Feb 2024 14:56:36 +0100 Subject: [PATCH] Added prometheus_fastapi_instrumentator --- README.md | 4 ++++ app/main.py | 4 ++++ poetry.lock | 31 ++++++++++++++++++++++++++++++- pyproject.toml | 1 + 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b4f81c4..62f6e97 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,10 @@ Usage example: act push -j deploy --secret-file my.secrets ``` +## Prometheus metrics +The application includes prometheus-fastapi-instrumentator for monitoring performance and analyzing its operation. It automatically adds an endpoint `/metrics` where you can access application metrics for Prometheus. These metrics include information about request counts, request execution times, and other important indicators of application performance. +More on that at (Prometheus FastAPI Instrumentator)[https://github.com/trallnag/prometheus-fastapi-instrumentator] + ## Classy-FastAPI Classy-FastAPI allows you to easily do dependency injection of object instances that should persist between FastAPI routes invocations, diff --git a/app/main.py b/app/main.py index 63db538..3637932 100644 --- a/app/main.py +++ b/app/main.py @@ -2,6 +2,7 @@ from fastapi import FastAPI from fastapi.openapi.utils import get_openapi +from prometheus_fastapi_instrumentator import Instrumentator from . import example, items @@ -32,5 +33,8 @@ def openapi(self) -> Dict[str, Any]: app = CustomFastAPI() +Instrumentator().instrument(app).expose(app) + + app.include_router(example.router) app.include_router(items.routes.router) diff --git a/poetry.lock b/poetry.lock index e14468a..88510b5 100644 --- a/poetry.lock +++ b/poetry.lock @@ -501,6 +501,35 @@ nodeenv = ">=0.11.1" pyyaml = ">=5.1" virtualenv = ">=20.10.0" +[[package]] +name = "prometheus-client" +version = "0.20.0" +description = "Python client for the Prometheus monitoring system." +optional = false +python-versions = ">=3.8" +files = [ + {file = "prometheus_client-0.20.0-py3-none-any.whl", hash = "sha256:cde524a85bce83ca359cc837f28b8c0db5cac7aa653a588fd7e84ba061c329e7"}, + {file = "prometheus_client-0.20.0.tar.gz", hash = "sha256:287629d00b147a32dcb2be0b9df905da599b2d82f80377083ec8463309a4bb89"}, +] + +[package.extras] +twisted = ["twisted"] + +[[package]] +name = "prometheus-fastapi-instrumentator" +version = "6.1.0" +description = "Instrument your FastAPI with Prometheus metrics." +optional = false +python-versions = ">=3.7.0,<4.0.0" +files = [ + {file = "prometheus_fastapi_instrumentator-6.1.0-py3-none-any.whl", hash = "sha256:2279ac1cf5b9566a4c3a07f78c9c5ee19648ed90976ab87d73d672abc1bfa017"}, + {file = "prometheus_fastapi_instrumentator-6.1.0.tar.gz", hash = "sha256:1820d7a90389ce100f7d1285495ead388818ae0882e761c1f3e6e62a410bdf13"}, +] + +[package.dependencies] +fastapi = ">=0.38.1,<1.0.0" +prometheus-client = ">=0.8.0,<1.0.0" + [[package]] name = "pycodestyle" version = "2.11.1" @@ -897,4 +926,4 @@ test = ["covdefaults (>=2.3)", "coverage (>=7.2.7)", "coverage-enable-subprocess [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "7ff53fd4e9cf8584ce81be09adfa7380df8c4f6e8d8d49e91676304a6872609e" +content-hash = "db28b8e957cf65452afa6918284b2cd805d81d47b8ba62861b28ce8294084237" diff --git a/pyproject.toml b/pyproject.toml index b2a614b..1c8615d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,7 @@ python = "^3.10" fastapi = "^0.109" uvicorn = "^0.26" classy-fastapi = "^0.6.1" +prometheus-fastapi-instrumentator = "^6.1.0" [tool.poetry.group.dev.dependencies] pre-commit = "^3.6"