|
| 1 | +import logging |
| 2 | + |
| 3 | +import httpx |
| 4 | +import wrapt |
| 5 | + |
| 6 | +from opencensus.trace import ( |
| 7 | + attributes_helper, |
| 8 | + exceptions_status, |
| 9 | + execution_context, |
| 10 | + integrations, |
| 11 | +) |
| 12 | +from opencensus.trace import span as span_module |
| 13 | +from opencensus.trace import utils |
| 14 | + |
| 15 | +try: |
| 16 | + from urllib.parse import urlparse |
| 17 | +except ImportError: |
| 18 | + from urlparse import urlparse |
| 19 | + |
| 20 | + |
| 21 | +log = logging.getLogger(__name__) |
| 22 | + |
| 23 | +MODULE_NAME = "httpx" |
| 24 | + |
| 25 | +HTTP_HOST = attributes_helper.COMMON_ATTRIBUTES["HTTP_HOST"] |
| 26 | +HTTP_METHOD = attributes_helper.COMMON_ATTRIBUTES["HTTP_METHOD"] |
| 27 | +HTTP_PATH = attributes_helper.COMMON_ATTRIBUTES["HTTP_PATH"] |
| 28 | +HTTP_ROUTE = attributes_helper.COMMON_ATTRIBUTES["HTTP_ROUTE"] |
| 29 | +HTTP_STATUS_CODE = attributes_helper.COMMON_ATTRIBUTES["HTTP_STATUS_CODE"] |
| 30 | +HTTP_URL = attributes_helper.COMMON_ATTRIBUTES["HTTP_URL"] |
| 31 | + |
| 32 | + |
| 33 | +def trace_integration(tracer=None): |
| 34 | + """Wrap the requests library to trace it.""" |
| 35 | + log.info("Integrated module: {}".format(MODULE_NAME)) |
| 36 | + |
| 37 | + if tracer is not None: |
| 38 | + # The execution_context tracer should never be None - if it has not |
| 39 | + # been set it returns a no-op tracer. Most code in this library does |
| 40 | + # not handle None being used in the execution context. |
| 41 | + execution_context.set_opencensus_tracer(tracer) |
| 42 | + |
| 43 | + wrapt.wrap_function_wrapper( |
| 44 | + MODULE_NAME, "Client.request", wrap_client_request |
| 45 | + ) |
| 46 | + # pylint: disable=protected-access |
| 47 | + integrations.add_integration(integrations._Integrations.HTTPX) |
| 48 | + |
| 49 | + |
| 50 | +def wrap_client_request(wrapped, instance, args, kwargs): |
| 51 | + """Wrap the session function to trace it.""" |
| 52 | + # Check if request was sent from an exporter. If so, do not wrap. |
| 53 | + if execution_context.is_exporter(): |
| 54 | + return wrapped(*args, **kwargs) |
| 55 | + |
| 56 | + method = kwargs.get("method") or args[0] |
| 57 | + url = kwargs.get("url") or args[1] |
| 58 | + |
| 59 | + excludelist_hostnames = execution_context.get_opencensus_attr( |
| 60 | + "excludelist_hostnames" |
| 61 | + ) |
| 62 | + parsed_url = urlparse(url) |
| 63 | + if parsed_url.port is None: |
| 64 | + dest_url = parsed_url.hostname |
| 65 | + else: |
| 66 | + dest_url = "{}:{}".format(parsed_url.hostname, parsed_url.port) |
| 67 | + if utils.disable_tracing_hostname(dest_url, excludelist_hostnames): |
| 68 | + return wrapped(*args, **kwargs) |
| 69 | + |
| 70 | + path = parsed_url.path if parsed_url.path else "/" |
| 71 | + |
| 72 | + _tracer = execution_context.get_opencensus_tracer() |
| 73 | + _span = _tracer.start_span() |
| 74 | + |
| 75 | + _span.name = "{}".format(path) |
| 76 | + _span.span_kind = span_module.SpanKind.CLIENT |
| 77 | + |
| 78 | + try: |
| 79 | + tracer_headers = _tracer.propagator.to_headers(_tracer.span_context) |
| 80 | + kwargs.setdefault("headers", {}).update(tracer_headers) |
| 81 | + except Exception: # pragma: NO COVER |
| 82 | + pass |
| 83 | + |
| 84 | + # Add the component type to attributes |
| 85 | + _tracer.add_attribute_to_current_span("component", "HTTP") |
| 86 | + |
| 87 | + # Add the requests host to attributes |
| 88 | + _tracer.add_attribute_to_current_span(HTTP_HOST, dest_url) |
| 89 | + |
| 90 | + # Add the requests method to attributes |
| 91 | + _tracer.add_attribute_to_current_span(HTTP_METHOD, method.upper()) |
| 92 | + |
| 93 | + # Add the requests path to attributes |
| 94 | + _tracer.add_attribute_to_current_span(HTTP_PATH, path) |
| 95 | + |
| 96 | + # Add the requests url to attributes |
| 97 | + _tracer.add_attribute_to_current_span(HTTP_URL, url) |
| 98 | + |
| 99 | + try: |
| 100 | + result = wrapped(*args, **kwargs) |
| 101 | + except httpx.TimeoutException: |
| 102 | + _span.set_status(exceptions_status.TIMEOUT) |
| 103 | + raise |
| 104 | + except httpx.InvalidURL: |
| 105 | + _span.set_status(exceptions_status.INVALID_URL) |
| 106 | + raise |
| 107 | + except Exception as e: |
| 108 | + _span.set_status(exceptions_status.unknown(e)) |
| 109 | + raise |
| 110 | + else: |
| 111 | + # Add the status code to attributes |
| 112 | + _tracer.add_attribute_to_current_span( |
| 113 | + HTTP_STATUS_CODE, result.status_code |
| 114 | + ) |
| 115 | + _span.set_status(utils.status_from_http_code(result.status_code)) |
| 116 | + return result |
| 117 | + finally: |
| 118 | + _tracer.end_span() |
0 commit comments