Skip to content

Commit e61c3ad

Browse files
committed
use urllib to extract the path
1 parent fcbcee9 commit e61c3ad

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

ddtrace/_trace/processor/resource_renaming.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import re
22
from typing import List
33
from typing import Optional
4+
from urllib.parse import urlparse
45

56
from ddtrace._trace.processor import SpanProcessor
67
from ddtrace.ext import SpanTypes
78
from ddtrace.ext import http
9+
from ddtrace.internal.logger import get_logger
810
from ddtrace.settings._config import config
911

1012

13+
log = get_logger(__name__)
14+
15+
1116
class ResourceRenamingProcessor(SpanProcessor):
1217
def __init__(self):
13-
self._URL_PATH_EXTRACTION_RE = re.compile(
14-
r"^(?P<protocol>[a-z]+://(?P<host>[^?/]+))?(?P<path>/[^?]*)(?P<query>(\?).*)?$"
15-
)
16-
1718
self._INT_RE = re.compile(r"^[1-9][0-9]+$")
1819
self._INT_ID_RE = re.compile(r"^(?=.*[0-9].*)[0-9._-]{3,}$")
1920
self._HEX_RE = re.compile(r"^(?=.*[0-9].*)[A-Fa-f0-9]{6,}$")
@@ -39,10 +40,12 @@ def _compute_simplified_endpoint(self, url: Optional[str]) -> str:
3940
if not url:
4041
return "/"
4142

42-
match = self._URL_PATH_EXTRACTION_RE.match(url)
43-
if not match:
43+
try:
44+
parsed_url = urlparse(url)
45+
except ValueError as e:
46+
log.error("Failed to parse http.url tag when processing span for resource renaming: %s", e)
4447
return "/"
45-
path = match.group("path")
48+
path = parsed_url.path
4649
if not path or path == "/":
4750
return "/"
4851

tests/tracer/test_resource_renaming.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_compute_simplified_endpoint_path_element(self, elem, expected):
5959
("https://example.com/users", "/users"),
6060
# Query and fragment handling
6161
("http://example.com/api/users?id=123", "/api/users"),
62-
("https://example.com/users/123#section", "/users/123#section"),
62+
("https://example.com/users/123#section", "/users/{param:int}"),
6363
("https://example.com/users/123?filter=active#top", "/users/{param:int}"),
6464
# Parameter replacement
6565
("/users/123", "/users/{param:int}"),
@@ -86,7 +86,6 @@ def test_compute_simplified_endpoint_path_element(self, elem, expected):
8686
),
8787
# Error cases
8888
(None, "/"),
89-
("://malformed", "/"),
9089
],
9190
)
9291
def test_compute_simplified_endpoint(self, url, expected):

0 commit comments

Comments
 (0)