Skip to content

Commit

Permalink
refactor: Improve coding style (#155)
Browse files Browse the repository at this point in the history
* 👷 Replaces use of default mutable arguments in a function

* squash! Adjust excluded_handlers handling

Co-authored-by: Tim Schwenke <[email protected]>
  • Loading branch information
yezz123 and trallnag authored Aug 23, 2022
1 parent 3f1c500 commit 623d83b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
exclude = .git,.lock
max-complexity = 10
max-line-length = 90
ignore=E501
ignore=E501,W503
17 changes: 8 additions & 9 deletions src/prometheus_fastapi_instrumentator/instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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]] = []

Expand Down Expand Up @@ -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

Expand Down
35 changes: 20 additions & 15 deletions src/prometheus_fastapi_instrumentator/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/prometheus_fastapi_instrumentator/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 1 addition & 5 deletions tests/test_instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 623d83b

Please sign in to comment.