Skip to content

Commit e24d3d7

Browse files
committed
Preserve ServiceName (segment name) when tracing external http services with dynamic urls
1 parent 324d185 commit e24d3d7

File tree

8 files changed

+35
-26
lines changed

8 files changed

+35
-26
lines changed

aws_xray_sdk/ext/aiohttp/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from aws_xray_sdk.core import xray_recorder
99
from aws_xray_sdk.core.models import http
1010
from aws_xray_sdk.core.utils import stacktrace
11-
from aws_xray_sdk.ext.util import inject_trace_header, strip_url
11+
from aws_xray_sdk.ext.util import inject_trace_header, parse_hostname
1212

1313
# All aiohttp calls will entail outgoing HTTP requests, only in some ad-hoc
1414
# exceptions the namespace will be flip back to local.
@@ -22,7 +22,7 @@
2222

2323

2424
async def begin_subsegment(session, trace_config_ctx, params):
25-
name = trace_config_ctx.name if trace_config_ctx.name else strip_url(str(params.url))
25+
name = trace_config_ctx.name if trace_config_ctx.name else parse_hostname(str(params.url))
2626
subsegment = xray_recorder.begin_subsegment(name, REMOTE_NAMESPACE)
2727

2828
# No-op if subsegment is `None` due to `LOG_ERROR`.

aws_xray_sdk/ext/httplib/patch.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from aws_xray_sdk.core.models import http
99
from aws_xray_sdk.core.exceptions.exceptions import SegmentNotFoundException
1010
from aws_xray_sdk.core.patcher import _PATCHED_MODULES
11-
from aws_xray_sdk.ext.util import inject_trace_header, strip_url, unwrap
11+
from aws_xray_sdk.ext.util import inject_trace_header, parse_hostname, unwrap
1212

1313
if sys.version_info >= (3, 0, 0):
1414
PY2 = False
@@ -57,7 +57,7 @@ def _xray_traced_http_getresponse(wrapped, instance, args, kwargs):
5757

5858
return xray_recorder.record_subsegment(
5959
wrapped, instance, args, kwargs,
60-
name=strip_url(xray_data.url),
60+
name=parse_hostname(xray_data.url),
6161
namespace='remote',
6262
meta_processor=http_response_processor,
6363
)
@@ -111,7 +111,7 @@ def decompose_args(method, url, body, headers, encode_chunked=False):
111111
# we add a segment here in case connect fails
112112
return xray_recorder.record_subsegment(
113113
wrapped, instance, args, kwargs,
114-
name=strip_url(xray_data.url),
114+
name=parse_hostname(xray_data.url),
115115
namespace='remote',
116116
meta_processor=http_send_request_processor
117117
)
@@ -141,7 +141,7 @@ def _xray_traced_http_client_read(wrapped, instance, args, kwargs):
141141

142142
return xray_recorder.record_subsegment(
143143
wrapped, instance, args, kwargs,
144-
name=strip_url(xray_data.url),
144+
name=parse_hostname(xray_data.url),
145145
namespace='remote',
146146
meta_processor=http_read_processor
147147
)

aws_xray_sdk/ext/requests/patch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from aws_xray_sdk.core import xray_recorder
44
from aws_xray_sdk.core.models import http
5-
from aws_xray_sdk.ext.util import inject_trace_header, strip_url
5+
from aws_xray_sdk.ext.util import inject_trace_header, parse_hostname
66

77

88
def patch():
@@ -26,7 +26,7 @@ def _xray_traced_requests(wrapped, instance, args, kwargs):
2626

2727
return xray_recorder.record_subsegment(
2828
wrapped, instance, args, kwargs,
29-
name=strip_url(url),
29+
name=parse_hostname(url),
3030
namespace='remote',
3131
meta_processor=requests_processor,
3232
)

aws_xray_sdk/ext/util.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import re
22

3-
from aws_xray_sdk.core.models.trace_header import TraceHeader
4-
from aws_xray_sdk.core.models import http
5-
63
import wrapt
4+
from six.moves.urllib.parse import urlparse
75

6+
from aws_xray_sdk.core.models import http
7+
from aws_xray_sdk.core.models.trace_header import TraceHeader
88

99
first_cap_re = re.compile('(.)([A-Z][a-z]+)')
1010
all_cap_re = re.compile('([a-z0-9])([A-Z])')
@@ -118,6 +118,16 @@ def strip_url(url):
118118
return url.partition('?')[0] if url else url
119119

120120

121+
def parse_hostname(url):
122+
"""
123+
Parses the hostname from the given url.
124+
The hostname is used as a segment name for external http requests.
125+
:param url:
126+
:return: hostname string
127+
"""
128+
return urlparse(url).hostname
129+
130+
121131
def unwrap(obj, attr):
122132
"""
123133
Will unwrap a `wrapt` attribute

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
'enum34;python_version<"3.4"',
4747
'wrapt',
4848
'future',
49+
'six',
4950
'botocore>=1.11.3',
5051
],
5152

tests/ext/aiohttp/test_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async def test_error(loop, recorder):
6666
pass
6767

6868
subsegment = xray_recorder.current_segment().subsegments[0]
69-
assert subsegment.name == url
69+
assert subsegment.name == BASE_URL
7070
assert subsegment.error
7171

7272
http_meta = subsegment.http
@@ -85,7 +85,7 @@ async def test_throttle(loop, recorder):
8585
pass
8686

8787
subsegment = xray_recorder.current_segment().subsegments[0]
88-
assert subsegment.name == url
88+
assert subsegment.name == BASE_URL
8989
assert subsegment.error
9090
assert subsegment.throttle
9191

@@ -105,7 +105,7 @@ async def test_fault(loop, recorder):
105105
pass
106106

107107
subsegment = xray_recorder.current_segment().subsegments[0]
108-
assert subsegment.name == url
108+
assert subsegment.name == BASE_URL
109109
assert subsegment.fault
110110

111111
http_meta = subsegment.http

tests/ext/httplib/test_httplib.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from aws_xray_sdk.core import patch
55
from aws_xray_sdk.core import xray_recorder
66
from aws_xray_sdk.core.context import Context
7-
from aws_xray_sdk.ext.util import strip_url
87

98
if sys.version_info >= (3, 0, 0):
109
import http.client as httplib
@@ -57,7 +56,7 @@ def test_ok():
5756
url = 'https://{}/status/{}?foo=bar&baz=foo'.format(BASE_URL, status_code)
5857
_do_req(url)
5958
subsegment = xray_recorder.current_segment().subsegments[1]
60-
assert subsegment.name == strip_url(url)
59+
assert subsegment.name == BASE_URL
6160

6261
http_meta = subsegment.http
6362
assert http_meta['request']['url'] == url
@@ -70,7 +69,7 @@ def test_error():
7069
url = 'https://{}/status/{}'.format(BASE_URL, status_code)
7170
_do_req(url, 'POST')
7271
subsegment = xray_recorder.current_segment().subsegments[1]
73-
assert subsegment.name == url
72+
assert subsegment.name == BASE_URL
7473
assert subsegment.error
7574

7675
http_meta = subsegment.http
@@ -84,7 +83,7 @@ def test_throttle():
8483
url = 'https://{}/status/{}'.format(BASE_URL, status_code)
8584
_do_req(url, 'HEAD')
8685
subsegment = xray_recorder.current_segment().subsegments[1]
87-
assert subsegment.name == url
86+
assert subsegment.name == BASE_URL
8887
assert subsegment.error
8988
assert subsegment.throttle
9089

@@ -99,7 +98,7 @@ def test_fault():
9998
url = 'https://{}/status/{}'.format(BASE_URL, status_code)
10099
_do_req(url, 'PUT')
101100
subsegment = xray_recorder.current_segment().subsegments[1]
102-
assert subsegment.name == url
101+
assert subsegment.name == BASE_URL
103102
assert subsegment.fault
104103

105104
http_meta = subsegment.http
@@ -126,7 +125,7 @@ def test_correct_identify_http():
126125
url = 'http://{}/status/{}?foo=bar&baz=foo'.format(BASE_URL, status_code)
127126
_do_req(url, use_https=False)
128127
subsegment = xray_recorder.current_segment().subsegments[0]
129-
assert subsegment.name == strip_url(url)
128+
assert subsegment.name == BASE_URL
130129

131130
http_meta = subsegment.http
132131
assert http_meta['request']['url'].split(":")[0] == 'http'
@@ -137,7 +136,7 @@ def test_correct_identify_https():
137136
url = 'https://{}/status/{}?foo=bar&baz=foo'.format(BASE_URL, status_code)
138137
_do_req(url, use_https=True)
139138
subsegment = xray_recorder.current_segment().subsegments[0]
140-
assert subsegment.name == strip_url(url)
139+
assert subsegment.name == BASE_URL
141140

142141
https_meta = subsegment.http
143142
assert https_meta['request']['url'].split(":")[0] == 'https'

tests/ext/requests/test_requests.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from aws_xray_sdk.core import patch
55
from aws_xray_sdk.core import xray_recorder
66
from aws_xray_sdk.core.context import Context
7-
from aws_xray_sdk.ext.util import strip_url
87

98

109
patch(('requests',))
@@ -32,7 +31,7 @@ def test_ok():
3231
url = 'http://{}/status/{}?foo=bar'.format(BASE_URL, status_code)
3332
requests.get(url)
3433
subsegment = xray_recorder.current_segment().subsegments[0]
35-
assert subsegment.name == strip_url(url)
34+
assert subsegment.name == BASE_URL
3635

3736
http_meta = subsegment.http
3837
assert http_meta['request']['url'] == url
@@ -45,7 +44,7 @@ def test_error():
4544
url = 'http://{}/status/{}'.format(BASE_URL, status_code)
4645
requests.post(url)
4746
subsegment = xray_recorder.current_segment().subsegments[0]
48-
assert subsegment.name == url
47+
assert subsegment.name == BASE_URL
4948
assert subsegment.error
5049

5150
http_meta = subsegment.http
@@ -59,7 +58,7 @@ def test_throttle():
5958
url = 'http://{}/status/{}'.format(BASE_URL, status_code)
6059
requests.head(url)
6160
subsegment = xray_recorder.current_segment().subsegments[0]
62-
assert subsegment.name == url
61+
assert subsegment.name == BASE_URL
6362
assert subsegment.error
6463
assert subsegment.throttle
6564

@@ -74,7 +73,7 @@ def test_fault():
7473
url = 'http://{}/status/{}'.format(BASE_URL, status_code)
7574
requests.put(url)
7675
subsegment = xray_recorder.current_segment().subsegments[0]
77-
assert subsegment.name == url
76+
assert subsegment.name == BASE_URL
7877
assert subsegment.fault
7978

8079
http_meta = subsegment.http

0 commit comments

Comments
 (0)