Skip to content

Commit 82b2e6f

Browse files
committed
Expand ability to ignore some httplib calls.
1 parent 2b5a130 commit 82b2e6f

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

aws_xray_sdk/ext/httplib/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .patch import patch, unpatch
1+
from .patch import patch, unpatch, add_ignored, reset_ignored
22

3-
__all__ = ['patch', 'unpatch']
3+
__all__ = ['patch', 'unpatch', 'add_ignored', 'reset_ignored']

aws_xray_sdk/ext/httplib/patch.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,33 @@
2222

2323
_XRAY_PROP = '_xray_prop'
2424
_XRay_Data = namedtuple('xray_data', ['method', 'host', 'url'])
25+
_XRay_Ignore = namedtuple('xray_ignore', ['subclass', 'hostname', 'urls'])
2526
# A flag indicates whether this module is X-Ray patched or not
2627
PATCH_FLAG = '__xray_patched'
28+
# Calls that should be ignored
29+
_XRAY_IGNORE = set()
30+
31+
32+
def add_ignored(subclass=None, hostname=None, urls=None):
33+
global _XRAY_IGNORE
34+
if subclass is not None or hostname is not None or urls is not None:
35+
urls = urls if urls is None else tuple(urls)
36+
_XRAY_IGNORE.add(_XRay_Ignore(subclass=subclass, hostname=hostname, urls=urls))
37+
38+
39+
def reset_ignored():
40+
global _XRAY_IGNORE
41+
_XRAY_IGNORE.clear()
42+
_ignored_add_default()
43+
44+
45+
def _ignored_add_default():
46+
# skip httplib tracing for SDK built-in centralized sampling pollers
47+
add_ignored(subclass='botocore.awsrequest.AWSHTTPConnection', urls=['/GetSamplingRules', '/SamplingTargets'])
48+
49+
50+
# make sure we have the default rules
51+
_ignored_add_default()
2752

2853

2954
def http_response_processor(wrapped, instance, args, kwargs, return_value,
@@ -77,11 +102,21 @@ def http_send_request_processor(wrapped, instance, args, kwargs, return_value,
77102
subsegment.add_exception(exception, stack)
78103

79104

105+
def _ignore_request(subclass, hostname, url):
106+
global _XRAY_IGNORE
107+
for rule in _XRAY_IGNORE:
108+
subclass_match = subclass == rule.subclass if rule.subclass is not None else True
109+
host_match = hostname == rule.hostname if rule.hostname is not None else True
110+
url_match = url in rule.urls if rule.urls is not None else True
111+
if url_match and host_match and subclass_match:
112+
return True
113+
return False
114+
115+
80116
def _send_request(wrapped, instance, args, kwargs):
81117
def decompose_args(method, url, body, headers, encode_chunked=False):
82-
# skip httplib tracing for SDK built-in centralized sampling pollers
83-
if (('/GetSamplingRules' in args or '/SamplingTargets' in args) and
84-
type(instance).__name__ == 'botocore.awsrequest.AWSHTTPConnection'):
118+
# skip any ignored requests
119+
if _ignore_request(type(instance).__name__, instance.host, url):
85120
return wrapped(*args, **kwargs)
86121

87122
# Only injects headers when the subsegment for the outgoing

0 commit comments

Comments
 (0)