From da344ce83c5c40b70fd7f0b2fb33905d9498ee3c Mon Sep 17 00:00:00 2001 From: Michiel <918128+mvanderlee@users.noreply.github.com> Date: Mon, 8 Jan 2024 18:05:47 +0100 Subject: [PATCH] Replace fastapi with Starlette --- pyproject.toml | 2 +- .../instrumentation.py | 18 +++++++++--------- .../metrics.py | 2 +- .../middleware.py | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f3152c4..27abdf5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,7 @@ keywords = ["prometheus", "instrumentation", "fastapi", "exporter", "metrics"] [tool.poetry.dependencies] python = ">= 3.7.0, < 4.0.0" -fastapi = ">= 0.38.1, < 1.0.0" +starlette = ">= 0.30.0, < 1.0.0" prometheus-client = ">= 0.8.0, < 1.0.0" [tool.poetry.group.dev.dependencies] diff --git a/src/prometheus_fastapi_instrumentator/instrumentation.py b/src/prometheus_fastapi_instrumentator/instrumentation.py index fcd4f75..b061805 100644 --- a/src/prometheus_fastapi_instrumentator/instrumentation.py +++ b/src/prometheus_fastapi_instrumentator/instrumentation.py @@ -6,7 +6,6 @@ from enum import Enum from typing import Any, Awaitable, Callable, List, Optional, Sequence, Union, cast -from fastapi import FastAPI from prometheus_client import ( CONTENT_TYPE_LATEST, REGISTRY, @@ -14,6 +13,7 @@ generate_latest, multiprocess, ) +from starlette.applications import Starlette from starlette.requests import Request from starlette.responses import Response @@ -40,7 +40,7 @@ def __init__( inprogress_labels: bool = False, registry: Union[CollectorRegistry, None] = None, ) -> None: - """Create a Prometheus FastAPI Instrumentator. + """Create a Prometheus Starlette Instrumentator. Args: should_group_status_codes (bool): Should status codes be grouped into @@ -60,7 +60,7 @@ def __init__( should_respect_env_var (bool): Should the instrumentator only work - for example the methods `instrument()` and `expose()` - if a certain environment variable is set to `true`? Usecase: A base - FastAPI app that is used by multiple distinct apps. The apps + Starlette app that is used by multiple distinct apps. The apps only have to set the variable to be instrumented. Defaults to `False`. @@ -149,7 +149,7 @@ def __init__( def instrument( self, - app: FastAPI, + app: Starlette, metric_namespace: str = "", metric_subsystem: str = "", should_only_respect_2xx_for_highr: bool = False, @@ -183,10 +183,10 @@ def instrument( The middleware iterates through all `instrumentations` and executes them. Args: - app (FastAPI): FastAPI app instance. + app (Starlette): Starlette app instance. Raises: - e: Only raised if FastAPI itself throws an exception. + e: Only raised if Starlette itself throws an exception. Returns: self: Instrumentator. Builder Pattern. @@ -222,7 +222,7 @@ def instrument( def expose( self, - app: FastAPI, + app: Starlette, should_gzip: bool = False, endpoint: str = "/metrics", include_in_schema: bool = True, @@ -232,7 +232,7 @@ def expose( """Exposes endpoint for metrics. Args: - app: FastAPI app instance. Endpoint will be added to this app. + app: Starlette app instance. Endpoint will be added to this app. should_gzip: Should the endpoint return compressed data? It will also check for `gzip` in the `Accept-Encoding` header. @@ -247,7 +247,7 @@ def expose( tags (List[str], optional): If you manage your routes with tags. Defaults to None. - kwargs: Will be passed to FastAPI route annotation. + kwargs: Will be passed to Starlette route annotation. Returns: self: Instrumentator. Builder Pattern. diff --git a/src/prometheus_fastapi_instrumentator/metrics.py b/src/prometheus_fastapi_instrumentator/metrics.py index abd5ec8..959e8ee 100644 --- a/src/prometheus_fastapi_instrumentator/metrics.py +++ b/src/prometheus_fastapi_instrumentator/metrics.py @@ -117,7 +117,7 @@ def latency( buckets: Sequence[Union[float, str]] = Histogram.DEFAULT_BUCKETS, registry: CollectorRegistry = REGISTRY, ) -> Optional[Callable[[Info], None]]: - """Default metric for the Prometheus FastAPI Instrumentator. + """Default metric for the Prometheus Starlette Instrumentator. Args: metric_name (str, optional): Name of the metric to be created. Must be diff --git a/src/prometheus_fastapi_instrumentator/middleware.py b/src/prometheus_fastapi_instrumentator/middleware.py index e0d9a6b..9eae350 100644 --- a/src/prometheus_fastapi_instrumentator/middleware.py +++ b/src/prometheus_fastapi_instrumentator/middleware.py @@ -6,8 +6,8 @@ from timeit import default_timer from typing import Awaitable, Callable, Optional, Sequence, Tuple, Union -from fastapi import FastAPI from prometheus_client import REGISTRY, CollectorRegistry, Gauge +from starlette.applications import Starlette from starlette.datastructures import Headers from starlette.requests import Request from starlette.responses import Response @@ -19,7 +19,7 @@ class PrometheusInstrumentatorMiddleware: def __init__( self, - app: FastAPI, + app: Starlette, *, should_group_status_codes: bool = True, should_ignore_untemplated: bool = False,