Skip to content

Commit

Permalink
add path grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
evstratbg authored May 28, 2024
1 parent f83c26f commit 46eba46
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions litestar/contrib/prometheus/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import time
from functools import wraps
import re
from typing import TYPE_CHECKING, Any, Callable, ClassVar, cast

from litestar.connection.request import Request
Expand Down Expand Up @@ -115,6 +116,19 @@ def _get_default_labels(self, request: Request[Any, Any, Any]) -> dict[str, str
A dictionary of default labels.
"""

path = request.url.path
if self._config.group_path:
path_parts = path.split("/")
if path_parts[0] == "":
path_parts = path_parts[1:]
path = ""
for path_parameter, path_parameter_value in request.scope.get("path_params", {}).items():
for path_part in path_parts:
path_parameter_value = str(path_parameter_value)
if path_part == path_parameter_value:
path += f"/[{path_parameter}]"
else:
path += f"/{path_part}"
return {
"method": request.method if request.scope["type"] == ScopeType.HTTP else request.scope["type"],
"path": request.url.path,
Expand Down

0 comments on commit 46eba46

Please sign in to comment.