Skip to content

Commit

Permalink
chore: Potential fix
Browse files Browse the repository at this point in the history
  • Loading branch information
trallnag committed Feb 26, 2023
1 parent 397b87a commit 286ca19
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
39 changes: 21 additions & 18 deletions src/prometheus_fastapi_instrumentator/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,27 +617,30 @@ def default(
subsystem=metric_subsystem,
registry=registry,
)
except ValueError as e:
if "Duplicated timeseries in CollectorRegistry:" not in e.args[0]:
raise e

def instrumentation(info: Info) -> None:
TOTAL.labels(info.method, info.modified_status, info.modified_handler).inc()
def instrumentation(info: Info) -> None:
TOTAL.labels(info.method, info.modified_status, info.modified_handler).inc()

IN_SIZE.labels(info.modified_handler).observe(
int(info.request.headers.get("Content-Length", 0))
)

if info.response and hasattr(info.response, "headers"):
OUT_SIZE.labels(info.modified_handler).observe(
int(info.response.headers.get("Content-Length", 0))
IN_SIZE.labels(info.modified_handler).observe(
int(info.request.headers.get("Content-Length", 0))
)
else:
OUT_SIZE.labels(info.modified_handler).observe(0)

if not should_only_respect_2xx_for_highr or info.modified_status.startswith("2"):
LATENCY_HIGHR.observe(info.modified_duration)
if info.response and hasattr(info.response, "headers"):
OUT_SIZE.labels(info.modified_handler).observe(
int(info.response.headers.get("Content-Length", 0))
)
else:
OUT_SIZE.labels(info.modified_handler).observe(0)

LATENCY_LOWR.labels(info.modified_handler).observe(info.modified_duration)
if not should_only_respect_2xx_for_highr or info.modified_status.startswith(
"2"
):
LATENCY_HIGHR.observe(info.modified_duration)

return instrumentation
LATENCY_LOWR.labels(info.modified_handler).observe(info.modified_duration)

return instrumentation

except ValueError as e:
if "Duplicated timeseries in CollectorRegistry:" not in e.args[0]:
raise e
4 changes: 3 additions & 1 deletion src/prometheus_fastapi_instrumentator/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,14 @@ async def send_wrapper(event: ASGISendEvent) -> None:
)

for instrumentation in self.instrumentations:
instrumentation(info)
if instrumentation:
instrumentation(info)

await asyncio.gather(
*[
instrumentation(info)
for instrumentation in self.async_instrumentations
if instrumentation
]
)

Expand Down

0 comments on commit 286ca19

Please sign in to comment.