Skip to content

Commit

Permalink
Fix crash in response callback when path is invalid unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Nov 4, 2024
1 parent 07c2d3d commit feb9489
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions kinto/core/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,13 @@ def on_new_response(event):
request = event.request
metrics_service = config.registry.metrics

try:
endpoint = utils.strip_uri_prefix(request.path)
except UnicodeDecodeError as e:
# This `on_new_response` callback is also called when a HTTP 400
# is returned because of an invalid UTF-8 path. We still want metrics.
endpoint = str(e)

# Count unique users.
user_id = request.prefixed_userid
if user_id:
Expand Down Expand Up @@ -499,7 +506,7 @@ def on_new_response(event):
"request_summary",
unique=[
("method", request.method.lower()),
("endpoint", utils.strip_uri_prefix(request.path)),
("endpoint", endpoint),
("status", str(request.response.status_code)),
]
+ metrics_matchdict_labels,
Expand All @@ -511,8 +518,7 @@ def on_new_response(event):
metrics_service.observe(
"request_duration",
duration,
labels=[("endpoint", utils.strip_uri_prefix(request.path))]
+ metrics_matchdict_labels,
labels=[("endpoint", endpoint)] + metrics_matchdict_labels,
)
except AttributeError: # pragma: no cover
# Logging was not setup in this Kinto app (unlikely but possible)
Expand All @@ -522,7 +528,7 @@ def on_new_response(event):
metrics_service.observe(
"request_size",
len(request.response.body or b""),
labels=[("endpoint", utils.strip_uri_prefix(request.path))] + metrics_matchdict_labels,
labels=[("endpoint", endpoint)] + metrics_matchdict_labels,
)

# Count authentication verifications.
Expand Down

0 comments on commit feb9489

Please sign in to comment.