diff --git a/.flake8 b/.flake8 index acb20bc..6527ed8 100644 --- a/.flake8 +++ b/.flake8 @@ -2,4 +2,4 @@ exclude = .git,.lock max-complexity = 10 max-line-length = 90 -ignore=E501 +ignore=E501,W503 diff --git a/src/prometheus_fastapi_instrumentator/instrumentation.py b/src/prometheus_fastapi_instrumentator/instrumentation.py index c88eae3..286f07f 100644 --- a/src/prometheus_fastapi_instrumentator/instrumentation.py +++ b/src/prometheus_fastapi_instrumentator/instrumentation.py @@ -2,7 +2,7 @@ import os import re from enum import Enum -from typing import Callable, List, Optional, Pattern, Union +from typing import Callable, List, Optional, Union from fastapi import FastAPI from starlette.requests import Request @@ -23,7 +23,7 @@ def __init__( should_round_latency_decimals: bool = False, should_respect_env_var: bool = False, should_instrument_requests_inprogress: bool = False, - excluded_handlers: List[str] = [], + excluded_handlers: List[str] = None, round_latency_decimals: int = 4, env_var_name: str = "ENABLE_METRICS", inprogress_name: str = "http_requests_inprogress", @@ -90,11 +90,10 @@ def __init__( self.inprogress_name = inprogress_name self.inprogress_labels = inprogress_labels - self.excluded_handlers: List[Pattern[str]] - if excluded_handlers: - self.excluded_handlers = [re.compile(path) for path in excluded_handlers] - else: - self.excluded_handlers = [] + if excluded_handlers is None: + excluded_handlers = [] + + self.excluded_handlers = [re.compile(path) for path in excluded_handlers] self.instrumentations: List[Callable[[metrics.Info], None]] = [] @@ -207,11 +206,11 @@ def metrics(request: Request): resp = Response(content=gzip.compress(generate_latest(registry))) resp.headers["Content-Type"] = CONTENT_TYPE_LATEST resp.headers["Content-Encoding"] = "gzip" - return resp else: resp = Response(content=generate_latest(registry)) resp.headers["Content-Type"] = CONTENT_TYPE_LATEST - return resp + + return resp return self diff --git a/src/prometheus_fastapi_instrumentator/metrics.py b/src/prometheus_fastapi_instrumentator/metrics.py index b468fed..c965b9d 100644 --- a/src/prometheus_fastapi_instrumentator/metrics.py +++ b/src/prometheus_fastapi_instrumentator/metrics.py @@ -162,9 +162,10 @@ def latency( def instrumentation(info: Info) -> None: if label_names: - label_values = [] - for attribute_name in info_attribute_names: - label_values.append(getattr(info, attribute_name)) + label_values = [ + getattr(info, attribute_name) for attribute_name in info_attribute_names + ] + METRIC.labels(*label_values).observe(info.modified_duration) else: METRIC.observe(info.modified_duration) @@ -228,9 +229,10 @@ def request_size( def instrumentation(info: Info) -> None: content_length = info.request.headers.get("Content-Length", 0) if label_names: - label_values = [] - for attribute_name in info_attribute_names: - label_values.append(getattr(info, attribute_name)) + label_values = [ + getattr(info, attribute_name) for attribute_name in info_attribute_names + ] + METRIC.labels(*label_values).observe(int(content_length)) else: METRIC.observe(int(content_length)) @@ -304,9 +306,10 @@ def instrumentation(info: Info) -> None: content_length = 0 if label_names: - label_values = [] - for attribute_name in info_attribute_names: - label_values.append(getattr(info, attribute_name)) + label_values = [ + getattr(info, attribute_name) for attribute_name in info_attribute_names + ] + METRIC.labels(*label_values).observe(int(content_length)) else: METRIC.observe(int(content_length)) @@ -384,9 +387,10 @@ def instrumentation(info: Info) -> None: content_length = int(request_cl) + int(response_cl) if label_names: - label_values = [] - for attribute_name in info_attribute_names: - label_values.append(getattr(info, attribute_name)) + label_values = [ + getattr(info, attribute_name) for attribute_name in info_attribute_names + ] + METRIC.labels(*label_values).observe(int(content_length)) else: METRIC.observe(int(content_length)) @@ -453,9 +457,10 @@ def requests( def instrumentation(info: Info) -> None: if label_names: - label_values = [] - for attribute_name in info_attribute_names: - label_values.append(getattr(info, attribute_name)) + label_values = [ + getattr(info, attribute_name) for attribute_name in info_attribute_names + ] + METRIC.labels(*label_values).inc() else: METRIC.inc() diff --git a/src/prometheus_fastapi_instrumentator/middleware.py b/src/prometheus_fastapi_instrumentator/middleware.py index 36b90c5..1fa936d 100644 --- a/src/prometheus_fastapi_instrumentator/middleware.py +++ b/src/prometheus_fastapi_instrumentator/middleware.py @@ -160,7 +160,7 @@ def _is_handler_excluded(self, handler: str, is_templated: bool) -> bool: bool: `True` if excluded, `False` if not. """ - if is_templated is False and self.should_ignore_untemplated: + if not is_templated and self.should_ignore_untemplated: return True if any(pattern.search(handler) for pattern in self.excluded_handlers): diff --git a/tests/test_instrumentation.py b/tests/test_instrumentation.py index 8f6eb28..be7669f 100644 --- a/tests/test_instrumentation.py +++ b/tests/test_instrumentation.py @@ -456,11 +456,7 @@ def test_should_respect_env_var_existence_not_exists(): def calc_entropy(decimal_str: str): decimals = [int(x) for x in decimal_str] print(decimals) - entropy = 0 - for i in range(len(decimals)): - if i != 0: - entropy += abs(decimals[i] - decimals[i - 1]) - return entropy + return sum(abs(decimals[i] - decimals[i - 1]) for i in range(len(decimals)) if i != 0) def test_entropy():