def install_patches():
if six.PY3:
# The old urllib does not exist in Py3, so delegate to urllib2 patcher
from . import urllib2
urllib2.install_patches()
return
import urllib
import urlparse
log.info('Instrumenting urllib methods for tracing')
class TracedURLOpener(urllib.FancyURLopener):
def open(self, fullurl, data=None):
parsed_url = urlparse.urlparse(fullurl)
host = parsed_url.hostname or None
port = parsed_url.port or None
span = utils.start_child_span(
operation_name='urllib', parent=get_current_span())
span.set_tag(ext_tags.SPAN_KIND, ext_tags.SPAN_KIND_RPC_CLIENT)
# use span as context manager so that its finish() method is called
with span:
span.set_tag(ext_tags.HTTP_URL, fullurl)
if host:
span.set_tag(ext_tags.PEER_HOST_IPV4, host)
if port:
span.set_tag(ext_tags.PEER_PORT, port)
# TODO add callee service name
# TODO add headers to propagate trace
# cannot use super here, this is an old style class
fileobj = urllib.FancyURLopener.open(self, fullurl, data)
if fileobj.getcode() is not None:
span.set_tag('http.status_code', fileobj.getcode())
return fileobj
def retrieve(self, url, filename=None, reporthook=None, data=None):
raise NotImplementedError
urllib._urlopener = TracedURLOpener()
urllib.py 文件源码
python
阅读 26
收藏 0
点赞 0
评论 0
评论列表
文章目录