Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/prometheus_fastapi_instrumentator/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from this module.
"""

from operator import attrgetter
from typing import Callable, List, Optional, Sequence, Tuple, Union

from prometheus_client import REGISTRY, CollectorRegistry, Counter, Histogram, Summary
Expand Down Expand Up @@ -163,9 +164,9 @@ def latency(
label_names, info_attribute_names = _build_label_attribute_names(
should_include_handler, should_include_method, should_include_status
)
for key in custom_labels:
for key, value in custom_labels.items():
label_names.append(key)
info_attribute_names.append(key)
info_attribute_names.append(value)

# Starlette will call app.build_middleware_stack() with every new middleware
# added, which will call all this again, which will make the registry
Expand Down Expand Up @@ -204,7 +205,7 @@ def instrumentation(info: Info) -> None:

if label_names:
label_values = [
getattr(info, attribute_name)
attrgetter(attribute_name)(info)
for attribute_name in info_attribute_names
]

Expand Down Expand Up @@ -258,9 +259,9 @@ def request_size(
label_names, info_attribute_names = _build_label_attribute_names(
should_include_handler, should_include_method, should_include_status
)
for key in custom_labels:
for key, value in custom_labels.items():
label_names.append(key)
info_attribute_names.append(key)
info_attribute_names.append(value)

# Starlette will call app.build_middleware_stack() with every new middleware
# added, which will call all this again, which will make the registry
Expand Down Expand Up @@ -292,7 +293,7 @@ def instrumentation(info: Info) -> None:
content_length = info.request.headers.get("Content-Length", 0)
if label_names:
label_values = [
getattr(info, attribute_name)
attrgetter(attribute_name)(info)
for attribute_name in info_attribute_names
]

Expand Down Expand Up @@ -352,9 +353,9 @@ def response_size(
label_names, info_attribute_names = _build_label_attribute_names(
should_include_handler, should_include_method, should_include_status
)
for key in custom_labels:
for key, value in custom_labels.items():
label_names.append(key)
info_attribute_names.append(key)
info_attribute_names.append(value)

# Starlette will call app.build_middleware_stack() with every new middleware
# added, which will call all this again, which will make the registry
Expand Down Expand Up @@ -390,7 +391,7 @@ def instrumentation(info: Info) -> None:

if label_names:
label_values = [
getattr(info, attribute_name)
attrgetter(attribute_name)(info)
for attribute_name in info_attribute_names
]

Expand Down Expand Up @@ -450,9 +451,9 @@ def combined_size(
label_names, info_attribute_names = _build_label_attribute_names(
should_include_handler, should_include_method, should_include_status
)
for key in custom_labels:
for key, value in custom_labels.items():
label_names.append(key)
info_attribute_names.append(key)
info_attribute_names.append(value)
# Starlette will call app.build_middleware_stack() with every new middleware
# added, which will call all this again, which will make the registry
# complain about duplicated metrics.
Expand Down Expand Up @@ -491,7 +492,7 @@ def instrumentation(info: Info) -> None:

if label_names:
label_values = [
getattr(info, attribute_name)
attrgetter(attribute_name)(info)
for attribute_name in info_attribute_names
]

Expand Down Expand Up @@ -549,9 +550,9 @@ def requests(
label_names, info_attribute_names = _build_label_attribute_names(
should_include_handler, should_include_method, should_include_status
)
for key in custom_labels:
for key, value in custom_labels.items():
label_names.append(key)
info_attribute_names.append(key)
info_attribute_names.append(value)

# Starlette will call app.build_middleware_stack() with every new middleware
# added, which will call all this again, which will make the registry
Expand Down Expand Up @@ -582,7 +583,7 @@ def requests(
def instrumentation(info: Info) -> None:
if label_names:
label_values = [
getattr(info, attribute_name)
attrgetter(attribute_name)(info)
for attribute_name in info_attribute_names
]

Expand Down