-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This module is only supported on Python3.5, which is the oldest supported by aiohttp.
- Loading branch information
1 parent
c2ba065
commit 958aa88
Showing
13 changed files
with
710 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
opentelemetry.ext.aiohttp_client package | ||
======================================== | ||
|
||
Module contents | ||
--------------- | ||
|
||
.. automodule:: opentelemetry.ext.aiohttp_client | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
OpenTelemetry aiohttp client Integration | ||
======================================== | ||
|
||
|pypi| | ||
|
||
.. |pypi| image:: https://badge.fury.io/py/opentelemetry-ext-aiohttp-client.svg | ||
:target: https://pypi.org/project/opentelemetry-ext-aiohttp-client/ | ||
|
||
This library allows tracing HTTP requests made by the | ||
`aiohttp client<https://docs.aiohttp.org/en/stable/client.html>`_ library. | ||
|
||
Installation | ||
------------ | ||
|
||
:: | ||
|
||
pip install opentelemetry-ext-aiohttp-client | ||
|
||
Usage | ||
----- | ||
|
||
.. code-block:: python | ||
import aiohttp | ||
import asyncio | ||
from opentelemetry.ext.aiohttp_client import create_trace_config | ||
async def fetch(session, url): | ||
async with session.get(url) as response: | ||
return await response.text() | ||
async def main(): | ||
async with aiohttp.ClientSession(trace_configs=[create_trace_config()]) as session: | ||
html = await fetch(session, 'https://www.example.org') | ||
print(html) | ||
if __name__ == '__main__': | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main()) | ||
References | ||
---------- | ||
|
||
* `OpenTelemetry Project <https://opentelemetry.io/>`_ | ||
* `aiohttp client Tracing <https://docs.aiohttp.org/en/stable/tracing_reference.html>`_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2020, OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
[metadata] | ||
name = opentelemetry-ext-aiohttp-client | ||
description = OpenTelemetry aiohttp client integration | ||
long_description = file: README.rst | ||
long_description_content_type = text/x-rst | ||
author = OpenTelemetry Authors | ||
author_email = cncf-opentelemetry-contributors@lists.cncf.io | ||
url = https://github.com/open-telemetry/opentelemetry-python/ext/opentelemetry-ext-aiohttp-client | ||
platforms = any | ||
license = Apache-2.0 | ||
classifiers = | ||
Development Status :: 3 - Alpha | ||
Intended Audience :: Developers | ||
License :: OSI Approved :: Apache Software License | ||
Programming Language :: Python | ||
Programming Language :: Python :: 3 | ||
Programming Language :: Python :: 3.5 | ||
Programming Language :: Python :: 3.6 | ||
Programming Language :: Python :: 3.7 | ||
|
||
[options] | ||
python_requires = >=3.5.3 | ||
package_dir= | ||
=src | ||
packages=find_namespace: | ||
install_requires = | ||
opentelemetry-api >= 0.5.dev0 | ||
aiohttp ~= 3.0 | ||
|
||
[options.packages.find] | ||
where = src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 2020, OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import os | ||
|
||
import setuptools | ||
|
||
BASE_DIR = os.path.dirname(__file__) | ||
VERSION_FILENAME = os.path.join( | ||
BASE_DIR, "src", "opentelemetry", "ext", "aiohttp_client", "version.py" | ||
) | ||
PACKAGE_INFO = {} | ||
with open(VERSION_FILENAME) as f: | ||
exec(f.read(), PACKAGE_INFO) | ||
|
||
setuptools.setup(version=PACKAGE_INFO["__version__"]) |
207 changes: 207 additions & 0 deletions
207
ext/opentelemetry-ext-aiohttp-client/src/opentelemetry/ext/aiohttp_client/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
# Copyright 2020, OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
The opentelemetry-ext-aiohttp-client package allows tracing HTTP requests | ||
made by the aiohttp client library. | ||
""" | ||
|
||
import contextlib | ||
import types | ||
import typing | ||
|
||
import aiohttp | ||
|
||
from opentelemetry import propagators, trace | ||
from opentelemetry.ext.aiohttp_client.version import __version__ | ||
from opentelemetry.trace import SpanKind | ||
from opentelemetry.trace.status import Status, StatusCanonicalCode | ||
|
||
|
||
# TODO: refactor this code to some common utility | ||
def http_status_to_canonical_code(status: int, allow_redirect: bool = True): | ||
# pylint:disable=too-many-branches,too-many-return-statements | ||
if status < 100: | ||
return StatusCanonicalCode.UNKNOWN | ||
if status <= 299: | ||
return StatusCanonicalCode.OK | ||
if status <= 399: | ||
if allow_redirect: | ||
return StatusCanonicalCode.OK | ||
return StatusCanonicalCode.DEADLINE_EXCEEDED | ||
if status <= 499: | ||
if status == 401: # HTTPStatus.UNAUTHORIZED: | ||
return StatusCanonicalCode.UNAUTHENTICATED | ||
if status == 403: # HTTPStatus.FORBIDDEN: | ||
return StatusCanonicalCode.PERMISSION_DENIED | ||
if status == 404: # HTTPStatus.NOT_FOUND: | ||
return StatusCanonicalCode.NOT_FOUND | ||
if status == 429: # HTTPStatus.TOO_MANY_REQUESTS: | ||
return StatusCanonicalCode.RESOURCE_EXHAUSTED | ||
return StatusCanonicalCode.INVALID_ARGUMENT | ||
if status <= 599: | ||
if status == 501: # HTTPStatus.NOT_IMPLEMENTED: | ||
return StatusCanonicalCode.UNIMPLEMENTED | ||
if status == 503: # HTTPStatus.SERVICE_UNAVAILABLE: | ||
return StatusCanonicalCode.UNAVAILABLE | ||
if status == 504: # HTTPStatus.GATEWAY_TIMEOUT: | ||
return StatusCanonicalCode.DEADLINE_EXCEEDED | ||
return StatusCanonicalCode.INTERNAL | ||
return StatusCanonicalCode.UNKNOWN | ||
|
||
|
||
def url_path_span_name(params: aiohttp.TraceRequestStartParams): | ||
return params.url.path | ||
|
||
|
||
def create_trace_config( | ||
url_filter: typing.Optional[typing.Callable[[str], str]] = None, | ||
span_name: typing.Optional[ | ||
typing.Union[ | ||
typing.Callable[[aiohttp.TraceRequestStartParams], str], str | ||
] | ||
] = None, | ||
) -> aiohttp.TraceConfig: | ||
"""Create an aiohttp-compatible trace configuration. | ||
One span is created for the entire HTTP request, including intial | ||
TCP/TLS setup if the connection doesn't exist. | ||
By default the span name is set to the HTTP request method. | ||
Example usage: | ||
.. code-block:: python | ||
import aiohttp | ||
from opentelemetry.ext.aiohttp_client import create_trace_config | ||
async with aiohttp.ClientSession(trace_configs=[create_trace_config()]) as session: | ||
async with session.get(url) as response: | ||
await response.text() | ||
:param url_filter: A callback to process the requested URL prior to adding | ||
it as a span attribute. This can be useful to remove sensitive data | ||
such as API keys or user personal information. | ||
:param str span_name: Override the default span name. | ||
:return: An object suitable for use with :py:class:`aiohttp.ClientSession`. | ||
:rtype: :py:class:`aiohttp.TraceConfig` | ||
""" | ||
# `aiohttp.TraceRequestStartParams` resolves to `aiohttp.tracing.TraceRequestStartParams` | ||
# which doesn't exist in the aiottp intersphinx inventory. | ||
# Explicitly specify the type for the `span_name` param and rtype to work | ||
# around this issue. | ||
|
||
tracer = trace.tracer_provider().get_tracer(__name__, __version__) | ||
|
||
async def on_request_start( | ||
unused_session: aiohttp.ClientSession, | ||
trace_config_ctx: types.SimpleNamespace, | ||
params: aiohttp.TraceRequestStartParams, | ||
): | ||
http_method = params.method.upper() | ||
if trace_config_ctx.span_name is None: | ||
request_span_name = http_method | ||
elif callable(trace_config_ctx.span_name): | ||
request_span_name = trace_config_ctx.span_name(params) | ||
else: | ||
request_span_name = str(trace_config_ctx.span_name) | ||
|
||
trace_config_ctx.span = trace_config_ctx.tracer.start_span( | ||
request_span_name, | ||
kind=SpanKind.CLIENT, | ||
attributes={ | ||
"component": "http", | ||
"http.method": http_method, | ||
"http.url": trace_config_ctx.url_filter(params.url) | ||
if callable(trace_config_ctx.url_filter) | ||
else str(params.url), | ||
}, | ||
) | ||
|
||
# Set the span as active via the `Tracer.use_span` context. | ||
# TODO: would be nice to have an explicit API to set a context as active. | ||
span_manager = contextlib.ExitStack() | ||
span_manager.enter_context( | ||
trace_config_ctx.tracer.use_span( | ||
trace_config_ctx.span, end_on_exit=True | ||
) | ||
) | ||
trace_config_ctx.span_manager = span_manager | ||
|
||
propagators.inject( | ||
tracer, type(params.headers).__setitem__, params.headers | ||
) | ||
|
||
async def on_request_end( | ||
unused_session: aiohttp.ClientSession, | ||
trace_config_ctx: types.SimpleNamespace, | ||
params: aiohttp.TraceRequestEndParams, | ||
): | ||
trace_config_ctx.span.set_status( | ||
# Assume redirects were allowed if any occurred (i.e. there is a history). | ||
# If redirects weren't allowed, there shouldn't be any history of them. | ||
Status( | ||
http_status_to_canonical_code( | ||
int(params.response.status), bool(params.response.history) | ||
) | ||
) | ||
) | ||
trace_config_ctx.span.set_attribute( | ||
"http.status_code", params.response.status | ||
) | ||
trace_config_ctx.span.set_attribute( | ||
"http.status_text", params.response.reason | ||
) | ||
trace_config_ctx.span_manager.close() | ||
|
||
async def on_request_exception( | ||
unused_session: aiohttp.ClientSession, | ||
trace_config_ctx: types.SimpleNamespace, | ||
params: aiohttp.TraceRequestExceptionParams, | ||
): | ||
if isinstance(params.exception, aiohttp.ServerTimeoutError): | ||
status = StatusCanonicalCode.DEADLINE_EXCEEDED | ||
elif ( | ||
# TODO: does this work on windows? | ||
isinstance(params.exception, aiohttp.ClientConnectorError) | ||
and params.exception.errno == -2 | ||
): | ||
# DNS resolution failed | ||
status = StatusCanonicalCode.UNKNOWN | ||
else: | ||
status = StatusCanonicalCode.UNAVAILABLE | ||
|
||
trace_config_ctx.span.set_status(Status(status)) | ||
trace_config_ctx.span_manager.close() | ||
|
||
def _trace_config_ctx_factory(**kwargs): | ||
if kwargs.get("trace_request_ctx", None) is None: | ||
kwargs["trace_request_ctx"] = {} | ||
return types.SimpleNamespace( | ||
span_name=span_name, tracer=tracer, url_filter=url_filter, **kwargs | ||
) | ||
|
||
trace_config = aiohttp.TraceConfig( | ||
trace_config_ctx_factory=_trace_config_ctx_factory | ||
) | ||
|
||
trace_config.on_request_start.append(on_request_start) | ||
trace_config.on_request_end.append(on_request_end) | ||
trace_config.on_request_exception.append(on_request_exception) | ||
|
||
return trace_config |
15 changes: 15 additions & 0 deletions
15
ext/opentelemetry-ext-aiohttp-client/src/opentelemetry/ext/aiohttp_client/version.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright 2020, OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
__version__ = "0.5.dev0" |
Empty file.
Oops, something went wrong.