Skip to content

Commit f93976a

Browse files
authored
fix(requests): set correct service name when using split_by_domain (backport #4067) (#4091)
This is an automatic backport of pull request #4067 done by [Mergify](https://mergify.com). --- <details> <summary>Mergify commands and options</summary> <br /> More conditions and actions can be found in the [documentation](https://docs.mergify.com/). You can also trigger Mergify actions by commenting on this pull request: - `@Mergifyio refresh` will re-evaluate the rules - `@Mergifyio rebase` will rebase this PR on its base branch - `@Mergifyio update` will merge the base branch into this PR - `@Mergifyio backport <destination>` will backport this PR on `<destination>` branch Additionally, on Mergify [dashboard](https://dashboard.mergify.com/) you can: - look at your merge queues - generate the Mergify configuration with the config editor. Finally, you can contact us on https://mergify.com </details>
1 parent 1949d45 commit f93976a

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

ddtrace/contrib/requests/connection.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ...constants import ANALYTICS_SAMPLE_RATE_KEY
88
from ...constants import SPAN_MEASURED_KEY
99
from ...ext import SpanTypes
10+
from ...internal.compat import parse
1011
from ...internal.logger import get_logger
1112
from ...internal.utils import get_argument_value
1213
from ...propagation.http import HTTPPropagator
@@ -17,23 +18,17 @@
1718

1819
def _extract_hostname(uri):
1920
# type: (str) -> str
20-
end = len(uri)
21-
j = uri.rfind("#", 0, end)
22-
if j != -1:
23-
end = j
24-
j = uri.rfind("&", 0, end)
25-
if j != -1:
26-
end = j
27-
28-
start = uri.find("://", 0, end) + 3
29-
i = uri.find("@", start, end) + 1
30-
if i != 0:
31-
start = i
32-
j = uri.find("/", start, end)
33-
if j != -1:
34-
end = j
35-
36-
return uri[start:end]
21+
parsed_uri = parse.urlparse(uri)
22+
port = None
23+
try:
24+
port = parsed_uri.port
25+
except ValueError:
26+
# ValueError is raised in PY>3.5 when parsed_uri.port < 0 or parsed_uri.port > 65535
27+
return "%s:?" % (parsed_uri.hostname,)
28+
29+
if port is not None:
30+
return "%s:%s" % (parsed_uri.hostname, str(port))
31+
return parsed_uri.hostname
3732

3833

3934
def _extract_query_string(uri):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
requests: fix split-by-domain service name when multiple ``@`` signs are present in the url

tests/contrib/requests/test_requests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,24 @@ def test_user_service_name_precedence(self):
302302
assert s.name == "requests.request"
303303
assert s.service == "clients"
304304

305+
def test_split_by_domain_with_ampersat(self):
306+
# Regression test for: https://github.com/DataDog/dd-trace-py/issues/4062
307+
# ensure a service name is generated by the domain name
308+
cfg = config.get_from(self.session)
309+
cfg["split_by_domain"] = True
310+
# domain name should take precedence over monkey_service
311+
cfg["service_name"] = "monkey_service"
312+
url = URL_200 + "?email=monkey_monkey@zoo_mail.ca"
313+
314+
out = self.session.get(url)
315+
assert out.status_code == 200
316+
317+
spans = self.pop_spans()
318+
assert len(spans) == 1
319+
s = spans[0]
320+
321+
assert s.service == "httpbin.org"
322+
305323
def test_split_by_domain(self):
306324
# ensure a service name is generated by the domain name
307325
# of the ongoing call
@@ -594,6 +612,13 @@ def test_extract_hostname(uri, hostname):
594612
assert _extract_hostname(uri) == hostname
595613

596614

615+
def test_extract_hostname_invalid_port():
616+
if sys.version_info < (3, 6):
617+
assert _extract_hostname("http://localhost:-1/") == "localhost"
618+
else:
619+
assert _extract_hostname("http://localhost:-1/") == "localhost:?"
620+
621+
597622
@pytest.mark.parametrize(
598623
"uri,qs",
599624
[

0 commit comments

Comments
 (0)