Skip to content

Commit 5dbe8b3

Browse files
committed
Unknown host when host doesn't exist as fallback mechanism.
1 parent 1c704fc commit 5dbe8b3

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

aws_xray_sdk/ext/util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
first_cap_re = re.compile('(.)([A-Z][a-z]+)')
1616
all_cap_re = re.compile('([a-z0-9])([A-Z])')
17+
UNKNOWN_HOSTNAME = "UNKNOWN HOST"
1718

1819

1920
def inject_trace_header(headers, entity):
@@ -126,9 +127,11 @@ def strip_url(url):
126127

127128
def get_hostname(url):
128129
if url is None:
129-
return None
130+
return UNKNOWN_HOSTNAME
130131
url_parse = urlparse(url)
131132
hostname = url_parse.hostname
133+
if hostname is None:
134+
return UNKNOWN_HOSTNAME
132135
return hostname if hostname else url # If hostname is none, we return the regular URL; indication of malformed url
133136

134137

tests/ext/requests/test_requests.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_fault():
8484
assert http_meta['response']['status'] == status_code
8585

8686

87-
def test_invalid_url():
87+
def test_nonexistent_domain():
8888
try:
8989
requests.get('http://doesnt.exist')
9090
except Exception:
@@ -97,6 +97,24 @@ def test_invalid_url():
9797
assert exception.type == 'ConnectionError'
9898

9999

100+
def test_invalid_url():
101+
url = 'KLSDFJKLSDFJKLSDJF'
102+
try:
103+
requests.get(url)
104+
except Exception:
105+
# prevent uncatch exception from breaking test run
106+
pass
107+
subsegment = xray_recorder.current_segment().subsegments[0]
108+
assert subsegment.name == get_hostname(url)
109+
assert subsegment.fault
110+
111+
http_meta = subsegment.http
112+
assert http_meta['request']['url'] == strip_url(url)
113+
114+
exception = subsegment.cause['exceptions'][0]
115+
assert exception.type == 'MissingSchema'
116+
117+
100118
def test_name_uses_hostname():
101119
url1 = 'http://{}/fakepath/stuff/koo/lai/ahh'.format(BASE_URL)
102120
requests.get(url1)

tests/test_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from aws_xray_sdk.ext.util import to_snake_case, get_hostname, strip_url
22

3+
UNKNOWN_HOST = "UNKNOWN HOST"
4+
35

46
def test_to_snake_case():
57
s1 = to_snake_case('Bucket')
@@ -29,13 +31,13 @@ def test_get_hostname():
2931
assert s4 == "amazon.com"
3032

3133
s5 = get_hostname("INVALID_URL")
32-
assert s5 == "INVALID_URL"
34+
assert s5 == UNKNOWN_HOST
3335

3436
s6 = get_hostname("")
35-
assert s6 == ""
37+
assert s6 == UNKNOWN_HOST
3638

3739
s7 = get_hostname(None)
38-
assert not s7
40+
assert s7 == UNKNOWN_HOST
3941

4042

4143
def test_strip_url():

0 commit comments

Comments
 (0)