|  | 
|  | 1 | +# Copyright 2017, OpenCensus Authors | 
|  | 2 | +# | 
|  | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | +# you may not use this file except in compliance with the License. | 
|  | 5 | +# You may obtain a copy of the License at | 
|  | 6 | +# | 
|  | 7 | +#     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | +# | 
|  | 9 | +# Unless required by applicable law or agreed to in writing, software | 
|  | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | +# See the License for the specific language governing permissions and | 
|  | 13 | +# limitations under the License. | 
|  | 14 | +import asyncio | 
|  | 15 | +import logging | 
|  | 16 | + | 
|  | 17 | +import wrapt | 
|  | 18 | +from aiohttp import InvalidURL, ServerTimeoutError | 
|  | 19 | +from yarl import URL | 
|  | 20 | + | 
|  | 21 | +from opencensus.trace import ( | 
|  | 22 | +    attributes_helper, | 
|  | 23 | +    exceptions_status, | 
|  | 24 | +    execution_context, | 
|  | 25 | +    utils, | 
|  | 26 | +) | 
|  | 27 | +from opencensus.trace.span import SpanKind | 
|  | 28 | + | 
|  | 29 | +logger = logging.getLogger(__name__) | 
|  | 30 | + | 
|  | 31 | +MODULE_NAME = "aiohttp" | 
|  | 32 | + | 
|  | 33 | +COMPONENT = attributes_helper.COMMON_ATTRIBUTES["COMPONENT"] | 
|  | 34 | +HTTP_COMPONENT = "HTTP" | 
|  | 35 | +HTTP_HOST = attributes_helper.COMMON_ATTRIBUTES["HTTP_HOST"] | 
|  | 36 | +HTTP_METHOD = attributes_helper.COMMON_ATTRIBUTES["HTTP_METHOD"] | 
|  | 37 | +HTTP_PATH = attributes_helper.COMMON_ATTRIBUTES["HTTP_PATH"] | 
|  | 38 | +HTTP_ROUTE = attributes_helper.COMMON_ATTRIBUTES["HTTP_ROUTE"] | 
|  | 39 | +HTTP_STATUS_CODE = attributes_helper.COMMON_ATTRIBUTES["HTTP_STATUS_CODE"] | 
|  | 40 | +HTTP_URL = attributes_helper.COMMON_ATTRIBUTES["HTTP_URL"] | 
|  | 41 | + | 
|  | 42 | + | 
|  | 43 | +def trace_integration(tracer=None): | 
|  | 44 | +    """Wrap the aiohttp library to trace it.""" | 
|  | 45 | +    logger.info("Integrated module: {}".format(MODULE_NAME)) | 
|  | 46 | + | 
|  | 47 | +    if tracer is not None: | 
|  | 48 | +        # The execution_context tracer should never be None - if it has not | 
|  | 49 | +        # been set it returns a no-op tracer. Most code in this library does | 
|  | 50 | +        # not handle None being used in the execution context. | 
|  | 51 | +        execution_context.set_opencensus_tracer(tracer) | 
|  | 52 | + | 
|  | 53 | +    # Wrap Session class | 
|  | 54 | +    wrapt.wrap_function_wrapper( | 
|  | 55 | +        module=MODULE_NAME, name="ClientSession._request", wrapper=wrap_session_request | 
|  | 56 | +    ) | 
|  | 57 | + | 
|  | 58 | + | 
|  | 59 | +async def wrap_session_request(wrapped, _, args, kwargs): | 
|  | 60 | +    """Wrap the session function to trace it.""" | 
|  | 61 | +    if execution_context.is_exporter(): | 
|  | 62 | +        return await wrapped(*args, **kwargs) | 
|  | 63 | + | 
|  | 64 | +    method = kwargs.get("method") or args[0] | 
|  | 65 | +    str_or_url = kwargs.get("str_or_url") or args[1] | 
|  | 66 | +    try: | 
|  | 67 | +        url = URL(str_or_url) | 
|  | 68 | +    except ValueError as e: | 
|  | 69 | +        raise InvalidURL(str_or_url) from e | 
|  | 70 | + | 
|  | 71 | +    excludelist_hostnames = execution_context.get_opencensus_attr( | 
|  | 72 | +        "excludelist_hostnames" | 
|  | 73 | +    ) | 
|  | 74 | +    url_host_with_port = url.host + (f":{url.port}" if url.port else "") | 
|  | 75 | +    if utils.disable_tracing_hostname(url_host_with_port, excludelist_hostnames): | 
|  | 76 | +        return await wrapped(*args, **kwargs) | 
|  | 77 | + | 
|  | 78 | +    url_path = url.path or "/" | 
|  | 79 | + | 
|  | 80 | +    tracer = execution_context.get_opencensus_tracer() | 
|  | 81 | +    with tracer.span(name=url_path) as span: | 
|  | 82 | +        span.span_kind = SpanKind.CLIENT | 
|  | 83 | + | 
|  | 84 | +        try: | 
|  | 85 | +            tracer_headers = tracer.propagator.to_headers( | 
|  | 86 | +                span_context=tracer.span_context, | 
|  | 87 | +            ) | 
|  | 88 | +            kwargs.setdefault("headers", {}).update(tracer_headers) | 
|  | 89 | +        except Exception: | 
|  | 90 | +            pass | 
|  | 91 | + | 
|  | 92 | +        span.add_attribute( | 
|  | 93 | +            attribute_key=COMPONENT, | 
|  | 94 | +            attribute_value=HTTP_COMPONENT, | 
|  | 95 | +        ) | 
|  | 96 | +        span.add_attribute( | 
|  | 97 | +            attribute_key=HTTP_HOST, | 
|  | 98 | +            attribute_value=url_host_with_port, | 
|  | 99 | +        ) | 
|  | 100 | +        span.add_attribute( | 
|  | 101 | +            attribute_key=HTTP_METHOD, | 
|  | 102 | +            attribute_value=method.upper(), | 
|  | 103 | +        ) | 
|  | 104 | +        span.add_attribute( | 
|  | 105 | +            attribute_key=HTTP_PATH, | 
|  | 106 | +            attribute_value=url_path, | 
|  | 107 | +        ) | 
|  | 108 | +        span.add_attribute( | 
|  | 109 | +            attribute_key=HTTP_URL, | 
|  | 110 | +            attribute_value=str(url), | 
|  | 111 | +        ) | 
|  | 112 | + | 
|  | 113 | +        try: | 
|  | 114 | +            result = await wrapped(*args, **kwargs) | 
|  | 115 | +        except (ServerTimeoutError, asyncio.TimeoutError): | 
|  | 116 | +            span.set_status(exceptions_status.TIMEOUT) | 
|  | 117 | +            raise | 
|  | 118 | +        except InvalidURL: | 
|  | 119 | +            span.set_status(exceptions_status.INVALID_URL) | 
|  | 120 | +            raise | 
|  | 121 | +        except Exception as e: | 
|  | 122 | +            span.set_status(exceptions_status.unknown(e)) | 
|  | 123 | +            raise | 
|  | 124 | +        else: | 
|  | 125 | +            status_code = int(result.status) | 
|  | 126 | +            span.add_attribute( | 
|  | 127 | +                attribute_key=HTTP_STATUS_CODE, | 
|  | 128 | +                attribute_value=status_code, | 
|  | 129 | +            ) | 
|  | 130 | +            span.set_status(utils.status_from_http_code(http_code=status_code)) | 
|  | 131 | +            return result | 
0 commit comments