# Path: opentracing_instrumentation/http_client.py
# class AbstractRequestWrapper(object):
# 
#     def add_header(self, key, value):
#         pass
# 
#     @property
#     def _headers(self):
#         return {}
# 
#     @property
#     def host_port(self):
#         return None, None
# 
#     @property
#     def service_name(self):
#         for header in CONFIG.callee_name_headers:
#             value = self._headers.get(header, None)
#             if value is not None:
#                 return value
#         return None
# 
#     @property
#     def operation(self):
#         for header in CONFIG.callee_endpoint_headers:
#             value = self._headers.get(header, None)
#             if value is not None:
#                 return '%s:%s' % (self.method, value)
#         return self.method
# 
#     @property
#     def method(self):
#         raise NotImplementedError
# 
#     @property
#     def full_url(self):
#         raise NotImplementedError
# 
# Path: opentracing_instrumentation/http_client.py
# def before_http_request(request, current_span_extractor):
#     """
#     A hook to be executed before HTTP request is executed.
#     It returns a Span object that can be used as a context manager around
#     the actual HTTP call implementation, or in case of async callback,
#     it needs its `finish()` method to be called explicitly.
# 
#     :param request: request must match API defined by AbstractRequestWrapper
#     :param current_span_extractor: function that extracts current span
#         from some context
#     :return: returns child tracing span encapsulating this request
#     """
# 
#     span = utils.start_child_span(
#         operation_name=request.operation,
#         parent=current_span_extractor()
#     )
# 
#     span.set_tag(tags.SPAN_KIND, tags.SPAN_KIND_RPC_CLIENT)
#     span.set_tag(tags.HTTP_URL, request.full_url)
# 
#     service_name = request.service_name
#     host, port = request.host_port
#     if service_name:
#         span.set_tag(tags.PEER_SERVICE, service_name)
#     if host:
#         span.set_tag(tags.PEER_HOST_IPV4, host)
#     if port:
#         span.set_tag(tags.PEER_PORT, port)
# 
#     # fire interceptors
#     for interceptor in ClientInterceptors.get_interceptors():
#         interceptor.process(request=request, span=span)
# 
#     try:
#         carrier = {}
#         opentracing.tracer.inject(span_context=span.context,
#                                   format=Format.HTTP_HEADERS,
#                                   carrier=carrier)
#         for key, value in six.iteritems(carrier):
#             request.add_header(key, value)
#     except opentracing.UnsupportedFormatException:
#         pass
# 
#     return span
# 
# Path: opentracing_instrumentation/http_client.py
# def split_host_and_port(host_string, scheme='http'):
#     is_secure = True if scheme == 'https' else False
#     m = HOST_PORT_RE.match(host_string)
#     if m:
#         host, port = m.groups()
#         return host, int(port)
#     elif is_secure is None:
#         return host_string, None
#     elif is_secure:
#         return host_string, 443
#     else:
#         return host_string, 80
# 
# Path: opentracing_instrumentation/client_hooks/_singleton.py
# def singleton(func):
#     """
#     This decorator allows you to make sure that a function is called once and
#     only once. Note that recursive functions will still work.
# 
#     WARNING: Not thread-safe!!!
#     """
# 
#     @functools.wraps(func)
#     def wrapper(*args, **kwargs):
#         if wrapper.__call_state__ == CALLED:
#             return
#         ret = func(*args, **kwargs)
#         wrapper.__call_state__ = CALLED
#         return ret
# 
#     def reset():
#         wrapper.__call_state__ = NOT_CALLED
# 
#     wrapper.reset = reset
#     reset()
# 
#     # save original func to be able to patch and restore multiple times from
#     # unit tests
#     wrapper.__original_func = func
#     return wrapper
# 
# Path: opentracing_instrumentation/client_hooks/_current_span.py
# def set_current_span_func(span_extractor_func):


import logging
import six
    import http.client
    import urllib.request
        import urllib2
from future import standard_library
from tornado.httputil import HTTPHeaders
from opentracing.ext import tags as ext_tags
from opentracing_instrumentation.http_client import AbstractRequestWrapper
from opentracing_instrumentation.http_client import before_http_request
from opentracing_instrumentation.http_client import split_host_and_port
from ._singleton import singleton
from ._current_span import current_span_func
                        span.set_tag(ext_tags.HTTP_STATUS_CODE, resp.code)
                return resp

        return DerivedHandler

    class Urllib2RequestWrapper(AbstractRequestWrapper):
        def __init__(self, request):
            self.request = request
            self._norm_headers = None

        def add_header(self, key, value):
            self.request.add_header(key, value)

        @property
        def method(self):
            return self.request.get_method()

        @property
        def full_url(self):
            return self.request.get_full_url()

        @property
        def _headers(self):
            if self._norm_headers is None:
                self._norm_headers = HTTPHeaders(self.request.headers)
            return self._norm_headers

        @property
        def host_port(self):
            host_string = self.request.host

# Path: opentracing_instrumentation/http_client.py
# class AbstractRequestWrapper(object):
# 
#     def add_header(self, key, value):
#         pass
# 
#     @property
#     def _headers(self):
#         return {}
# 
#     @property
#     def host_port(self):
#         return None, None
# 
#     @property
#     def service_name(self):
#         for header in CONFIG.callee_name_headers:
#             value = self._headers.get(header, None)
#             if value is not None:
#                 return value
#         return None
# 
#     @property
#     def operation(self):
#         for header in CONFIG.callee_endpoint_headers:
#             value = self._headers.get(header, None)
#             if value is not None:
#                 return '%s:%s' % (self.method, value)
#         return self.method
# 
#     @property
#     def method(self):
#         raise NotImplementedError
# 
#     @property
#     def full_url(self):
#         raise NotImplementedError
# 
# Path: opentracing_instrumentation/http_client.py
# def before_http_request(request, current_span_extractor):
#     """
#     A hook to be executed before HTTP request is executed.
#     It returns a Span object that can be used as a context manager around
#     the actual HTTP call implementation, or in case of async callback,
#     it needs its `finish()` method to be called explicitly.
# 
#     :param request: request must match API defined by AbstractRequestWrapper
#     :param current_span_extractor: function that extracts current span
#         from some context
#     :return: returns child tracing span encapsulating this request
#     """
# 
#     span = utils.start_child_span(
#         operation_name=request.operation,
#         parent=current_span_extractor()
#     )
# 
#     span.set_tag(tags.SPAN_KIND, tags.SPAN_KIND_RPC_CLIENT)
#     span.set_tag(tags.HTTP_URL, request.full_url)
# 
#     service_name = request.service_name
#     host, port = request.host_port
#     if service_name:
#         span.set_tag(tags.PEER_SERVICE, service_name)
#     if host:
#         span.set_tag(tags.PEER_HOST_IPV4, host)
#     if port:
#         span.set_tag(tags.PEER_PORT, port)
# 
#     # fire interceptors
#     for interceptor in ClientInterceptors.get_interceptors():
#         interceptor.process(request=request, span=span)
# 
#     try:
#         carrier = {}
#         opentracing.tracer.inject(span_context=span.context,
#                                   format=Format.HTTP_HEADERS,
#                                   carrier=carrier)
#         for key, value in six.iteritems(carrier):
#             request.add_header(key, value)
#     except opentracing.UnsupportedFormatException:
#         pass
# 
#     return span
# 
# Path: opentracing_instrumentation/http_client.py
# def split_host_and_port(host_string, scheme='http'):
#     is_secure = True if scheme == 'https' else False
#     m = HOST_PORT_RE.match(host_string)
#     if m:
#         host, port = m.groups()
#         return host, int(port)
#     elif is_secure is None:
#         return host_string, None
#     elif is_secure:
#         return host_string, 443
#     else:
#         return host_string, 80
# 
# Path: opentracing_instrumentation/client_hooks/_singleton.py
# def singleton(func):
#     """
#     This decorator allows you to make sure that a function is called once and
#     only once. Note that recursive functions will still work.
# 
#     WARNING: Not thread-safe!!!
#     """
# 
#     @functools.wraps(func)
#     def wrapper(*args, **kwargs):
#         if wrapper.__call_state__ == CALLED:
#             return
#         ret = func(*args, **kwargs)
#         wrapper.__call_state__ = CALLED
#         return ret
# 
#     def reset():
#         wrapper.__call_state__ = NOT_CALLED
# 
#     wrapper.reset = reset
#     reset()
# 
#     # save original func to be able to patch and restore multiple times from
#     # unit tests
#     wrapper.__original_func = func
#     return wrapper
# 
# Path: opentracing_instrumentation/client_hooks/_current_span.py
# def set_current_span_func(span_extractor_func):


# Path: opentracing_instrumentation/client_hooks/urllib2.py
import logging
import six
    import http.client
    import urllib.request
        import urllib2
from future import standard_library
from tornado.httputil import HTTPHeaders
from opentracing.ext import tags as ext_tags
from opentracing_instrumentation.http_client import AbstractRequestWrapper
from opentracing_instrumentation.http_client import before_http_request
from opentracing_instrumentation.http_client import split_host_and_port
from ._singleton import singleton
from ._current_span import current_span_func

                        span.set_tag(ext_tags.HTTP_STATUS_CODE, resp.code)
                return resp

        return DerivedHandler

    class Urllib2RequestWrapper(AbstractRequestWrapper):
        def __init__(self, request):
            self.request = request
            self._norm_headers = None

        def add_header(self, key, value):
            self.request.add_header(key, value)

        @property
        def method(self):
            return self.request.get_method()

        @property
        def full_url(self):
            return self.request.get_full_url()

        @property
        def _headers(self):
            if self._norm_headers is None:
                self._norm_headers = HTTPHeaders(self.request.headers)
            return self._norm_headers

        @property
        def host_port(self):
            host_string = self.request.host

            return split_host_and_port(host_string=host_string,