From 5905853c953598b6eb92cfbfe3f1034bf126f633 Mon Sep 17 00:00:00 2001 From: Carol Abadeer Date: Wed, 1 Jun 2022 22:50:11 -0700 Subject: [PATCH 01/10] Fix HTTP instrumentation not being suppressed --- .../instrumentation/requests/__init__.py | 12 +++++++----- .../instrumentation/urllib3/__init__.py | 9 ++++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index bd14c504e6..3a0f13e4ef 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -56,14 +56,16 @@ from requests.sessions import Session from requests.structures import CaseInsensitiveDict -from opentelemetry import context +from opentelemetry import context, trace from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.requests.package import _instruments from opentelemetry.instrumentation.requests.version import __version__ from opentelemetry.instrumentation.utils import ( _SUPPRESS_INSTRUMENTATION_KEY, + #_SUPPRESS_HTTP_INSTRUMENTATION_KEY, http_status_to_status_code, ) +from opentelemetry.context import (_SUPPRESS_HTTP_INSTRUMENTATION_KEY) from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import SpanKind, get_tracer @@ -77,8 +79,8 @@ # A key to a context variable to avoid creating duplicate spans when instrumenting # both, Session.request and Session.send, since Session.request calls into Session.send -_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context.create_key( - "suppress_http_instrumentation" +_REQUESTS_INSTRUMENTATION_KEY = context.create_key( + "requests_instrumentation" ) _excluded_urls_from_env = get_excluded_urls("REQUESTS") @@ -176,7 +178,7 @@ def _instrumented_requests_call( inject(headers) token = context.attach( - context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True) + context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True) ) try: result = call_wrapped() # *** PROCEED @@ -185,7 +187,7 @@ def _instrumented_requests_call( result = getattr(exc, "response", None) finally: context.detach(token) - + if isinstance(result, Response): if span.is_recording(): span.set_attribute( diff --git a/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py b/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py index ef04b7d0c2..d75449e46b 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py @@ -77,9 +77,12 @@ def response_hook(span, request, response): from opentelemetry.instrumentation.urllib3.version import __version__ from opentelemetry.instrumentation.utils import ( _SUPPRESS_INSTRUMENTATION_KEY, + # _SUPPRESS_HTTP_INSTRUMENTATION_KEY, http_status_to_status_code, unwrap, ) +from opentelemetry.context import (_SUPPRESS_HTTP_INSTRUMENTATION_KEY) + from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import Span, SpanKind, get_tracer @@ -88,8 +91,8 @@ def response_hook(span, request, response): # A key to a context variable to avoid creating duplicate spans when instrumenting # both, Session.request and Session.send, since Session.request calls into Session.send -_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context.create_key( - "suppress_http_instrumentation" +_URLLIB3_INSTRUMENTATION_KEY = context.create_key( + "urllib3_instrumentation" ) _UrlFilterT = typing.Optional[typing.Callable[[str], str]] @@ -144,7 +147,7 @@ def _instrument(self, **kwargs): response_hook=kwargs.get("response_hook"), url_filter=kwargs.get("url_filter"), ) - + def _uninstrument(self, **kwargs): _uninstrument() From 6558b1e2e366d6e4cbc3ac56d29627ee9366a89c Mon Sep 17 00:00:00 2001 From: Carol Abadeer Date: Wed, 1 Jun 2022 23:08:15 -0700 Subject: [PATCH 02/10] Update suppress http instrumentation key in all libraries --- .../src/opentelemetry/instrumentation/botocore/__init__.py | 7 ++++--- .../src/opentelemetry/instrumentation/requests/__init__.py | 2 +- .../src/opentelemetry/instrumentation/urllib/__init__.py | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py index cf64e65d83..d03a2c6f03 100644 --- a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py @@ -98,6 +98,7 @@ def response_hook(span, service_name, operation_name, result): _SUPPRESS_INSTRUMENTATION_KEY, unwrap, ) +from opentelemetry.context import (_SUPPRESS_HTTP_INSTRUMENTATION_KEY) from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import get_tracer @@ -108,10 +109,10 @@ def response_hook(span, service_name, operation_name, result): # A key to a context variable to avoid creating duplicate spans when instrumenting # both botocore.client and urllib3.connectionpool.HTTPConnectionPool.urlopen since # botocore calls urlopen -_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context_api.create_key( - "suppress_http_instrumentation" -) +_BOTOCORE_INSTRUMENTATION_KEY = context_api.create_key( + "botocore_instrumentation" +) # pylint: disable=unused-argument def _patched_endpoint_prepare_request(wrapped, instance, args, kwargs): diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index 3a0f13e4ef..b2fe33bb57 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -56,7 +56,7 @@ from requests.sessions import Session from requests.structures import CaseInsensitiveDict -from opentelemetry import context, trace +from opentelemetry import context from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.requests.package import _instruments from opentelemetry.instrumentation.requests.version import __version__ diff --git a/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py b/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py index 3e38999ace..0a42b2740f 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py @@ -82,6 +82,7 @@ def response_hook(span, request_obj, response) _SUPPRESS_INSTRUMENTATION_KEY, http_status_to_status_code, ) +from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import Span, SpanKind, get_tracer @@ -90,10 +91,9 @@ def response_hook(span, request_obj, response) # A key to a context variable to avoid creating duplicate spans when instrumenting # both, Session.request and Session.send, since Session.request calls into Session.send -_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context.create_key( - "suppress_http_instrumentation" +_URLLIB_INSTRUMENTATION_KEY = context.create_key( + "urllib_instrumentation" ) - _RequestHookT = typing.Optional[typing.Callable[[Span, Request], None]] _ResponseHookT = typing.Optional[ typing.Callable[[Span, Request, client.HTTPResponse], None] From 7d35f17f93055a3ec036eb08b6d7259e44521f00 Mon Sep 17 00:00:00 2001 From: Carol Abadeer Date: Thu, 2 Jun 2022 15:55:28 -0700 Subject: [PATCH 03/10] Removed library specific keys and updated core repo SHA --- .github/workflows/test.yml | 2 +- .../instrumentation/botocore/__init__.py | 12 +++--------- .../instrumentation/requests/__init__.py | 10 +++------- .../instrumentation/urllib/__init__.py | 9 +++------ .../instrumentation/urllib3/__init__.py | 13 ++++--------- .../tests/test_urllib3_integration.py | 4 +++- 6 files changed, 17 insertions(+), 33 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 97db8312c6..e6b04a3e9b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,7 +6,7 @@ on: - 'release/*' pull_request: env: - CORE_REPO_SHA: e4494ea9cdd420390a36f6e6bbd9f15cd29873e9 + CORE_REPO_SHA: c82829283d3e99aa2e089d1774ee509619650617 jobs: build: diff --git a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py index d03a2c6f03..18085e5a31 100644 --- a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py @@ -98,7 +98,9 @@ def response_hook(span, service_name, operation_name, result): _SUPPRESS_INSTRUMENTATION_KEY, unwrap, ) -from opentelemetry.context import (_SUPPRESS_HTTP_INSTRUMENTATION_KEY) +from opentelemetry.context import ( + _SUPPRESS_HTTP_INSTRUMENTATION_KEY +) from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import get_tracer @@ -106,14 +108,6 @@ def response_hook(span, service_name, operation_name, result): logger = logging.getLogger(__name__) -# A key to a context variable to avoid creating duplicate spans when instrumenting -# both botocore.client and urllib3.connectionpool.HTTPConnectionPool.urlopen since -# botocore calls urlopen - -_BOTOCORE_INSTRUMENTATION_KEY = context_api.create_key( - "botocore_instrumentation" -) - # pylint: disable=unused-argument def _patched_endpoint_prepare_request(wrapped, instance, args, kwargs): request = args[0] diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index b2fe33bb57..21d15da8ba 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -62,10 +62,11 @@ from opentelemetry.instrumentation.requests.version import __version__ from opentelemetry.instrumentation.utils import ( _SUPPRESS_INSTRUMENTATION_KEY, - #_SUPPRESS_HTTP_INSTRUMENTATION_KEY, http_status_to_status_code, ) -from opentelemetry.context import (_SUPPRESS_HTTP_INSTRUMENTATION_KEY) +from opentelemetry.context import ( + _SUPPRESS_HTTP_INSTRUMENTATION_KEY +) from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import SpanKind, get_tracer @@ -77,11 +78,6 @@ ) from opentelemetry.util.http.httplib import set_ip_on_next_http_connection -# A key to a context variable to avoid creating duplicate spans when instrumenting -# both, Session.request and Session.send, since Session.request calls into Session.send -_REQUESTS_INSTRUMENTATION_KEY = context.create_key( - "requests_instrumentation" -) _excluded_urls_from_env = get_excluded_urls("REQUESTS") diff --git a/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py b/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py index 0a42b2740f..920374ddfe 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py @@ -82,18 +82,15 @@ def response_hook(span, request_obj, response) _SUPPRESS_INSTRUMENTATION_KEY, http_status_to_status_code, ) -from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY +from opentelemetry.context import ( + _SUPPRESS_HTTP_INSTRUMENTATION_KEY +) from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import Span, SpanKind, get_tracer from opentelemetry.trace.status import Status from opentelemetry.util.http import remove_url_credentials -# A key to a context variable to avoid creating duplicate spans when instrumenting -# both, Session.request and Session.send, since Session.request calls into Session.send -_URLLIB_INSTRUMENTATION_KEY = context.create_key( - "urllib_instrumentation" -) _RequestHookT = typing.Optional[typing.Callable[[Span, Request], None]] _ResponseHookT = typing.Optional[ typing.Callable[[Span, Request, client.HTTPResponse], None] diff --git a/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py b/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py index d75449e46b..11c25118a5 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py @@ -77,23 +77,18 @@ def response_hook(span, request, response): from opentelemetry.instrumentation.urllib3.version import __version__ from opentelemetry.instrumentation.utils import ( _SUPPRESS_INSTRUMENTATION_KEY, - # _SUPPRESS_HTTP_INSTRUMENTATION_KEY, http_status_to_status_code, unwrap, ) -from opentelemetry.context import (_SUPPRESS_HTTP_INSTRUMENTATION_KEY) - +from opentelemetry.context import ( + _SUPPRESS_HTTP_INSTRUMENTATION_KEY +) from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import Span, SpanKind, get_tracer from opentelemetry.trace.status import Status from opentelemetry.util.http.httplib import set_ip_on_next_http_connection -# A key to a context variable to avoid creating duplicate spans when instrumenting -# both, Session.request and Session.send, since Session.request calls into Session.send -_URLLIB3_INSTRUMENTATION_KEY = context.create_key( - "urllib3_instrumentation" -) _UrlFilterT = typing.Optional[typing.Callable[[str], str]] _RequestHookT = typing.Optional[ @@ -147,7 +142,7 @@ def _instrument(self, **kwargs): response_hook=kwargs.get("response_hook"), url_filter=kwargs.get("url_filter"), ) - + def _uninstrument(self, **kwargs): _uninstrument() diff --git a/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py b/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py index a79ee8f941..9ac757b595 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py +++ b/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py @@ -21,10 +21,12 @@ from opentelemetry import context, trace from opentelemetry.instrumentation.urllib3 import ( - _SUPPRESS_HTTP_INSTRUMENTATION_KEY, URLLib3Instrumentor, ) from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY +from opentelemetry.context import ( + _SUPPRESS_HTTP_INSTRUMENTATION_KEY +) from opentelemetry.propagate import get_global_textmap, set_global_textmap from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.mock_textmap import MockTextMapPropagator From 10ddea27f1d5c7b2d9af2f11b1755768c4bc0121 Mon Sep 17 00:00:00 2001 From: Carol Abadeer Date: Thu, 2 Jun 2022 17:06:53 -0700 Subject: [PATCH 04/10] Ran "tox -e generate" to fix failing generate workflow test --- .../opentelemetry/instrumentation/botocore/__init__.py | 4 +--- .../opentelemetry/instrumentation/requests/__init__.py | 9 +++------ .../src/opentelemetry/instrumentation/urllib/__init__.py | 4 +--- .../opentelemetry/instrumentation/urllib3/__init__.py | 5 +---- .../tests/test_urllib3_integration.py | 8 ++------ 5 files changed, 8 insertions(+), 22 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py index 18085e5a31..e06338ae3f 100644 --- a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py @@ -87,6 +87,7 @@ def response_hook(span, service_name, operation_name, result): from wrapt import wrap_function_wrapper from opentelemetry import context as context_api +from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.botocore.extensions import _find_extension from opentelemetry.instrumentation.botocore.extensions.types import ( _AwsSdkCallContext, @@ -98,9 +99,6 @@ def response_hook(span, service_name, operation_name, result): _SUPPRESS_INSTRUMENTATION_KEY, unwrap, ) -from opentelemetry.context import ( - _SUPPRESS_HTTP_INSTRUMENTATION_KEY -) from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import get_tracer diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index 21d15da8ba..c01d3659e4 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -57,6 +57,7 @@ from requests.structures import CaseInsensitiveDict from opentelemetry import context +from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.requests.package import _instruments from opentelemetry.instrumentation.requests.version import __version__ @@ -64,9 +65,6 @@ _SUPPRESS_INSTRUMENTATION_KEY, http_status_to_status_code, ) -from opentelemetry.context import ( - _SUPPRESS_HTTP_INSTRUMENTATION_KEY -) from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import SpanKind, get_tracer @@ -78,7 +76,6 @@ ) from opentelemetry.util.http.httplib import set_ip_on_next_http_connection - _excluded_urls_from_env = get_excluded_urls("REQUESTS") @@ -174,7 +171,7 @@ def _instrumented_requests_call( inject(headers) token = context.attach( - context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True) + context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True) ) try: result = call_wrapped() # *** PROCEED @@ -183,7 +180,7 @@ def _instrumented_requests_call( result = getattr(exc, "response", None) finally: context.detach(token) - + if isinstance(result, Response): if span.is_recording(): span.set_attribute( diff --git a/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py b/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py index 920374ddfe..f1a6b52043 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py @@ -75,6 +75,7 @@ def response_hook(span, request_obj, response) ) from opentelemetry import context +from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.urllib.package import _instruments from opentelemetry.instrumentation.urllib.version import __version__ @@ -82,9 +83,6 @@ def response_hook(span, request_obj, response) _SUPPRESS_INSTRUMENTATION_KEY, http_status_to_status_code, ) -from opentelemetry.context import ( - _SUPPRESS_HTTP_INSTRUMENTATION_KEY -) from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import Span, SpanKind, get_tracer diff --git a/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py b/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py index 11c25118a5..5b663936c4 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py @@ -72,6 +72,7 @@ def response_hook(span, request, response): import wrapt from opentelemetry import context +from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.urllib3.package import _instruments from opentelemetry.instrumentation.urllib3.version import __version__ @@ -80,16 +81,12 @@ def response_hook(span, request, response): http_status_to_status_code, unwrap, ) -from opentelemetry.context import ( - _SUPPRESS_HTTP_INSTRUMENTATION_KEY -) from opentelemetry.propagate import inject from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import Span, SpanKind, get_tracer from opentelemetry.trace.status import Status from opentelemetry.util.http.httplib import set_ip_on_next_http_connection - _UrlFilterT = typing.Optional[typing.Callable[[str], str]] _RequestHookT = typing.Optional[ typing.Callable[ diff --git a/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py b/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py index 9ac757b595..19d68492f5 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py +++ b/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py @@ -20,13 +20,9 @@ import urllib3.exceptions from opentelemetry import context, trace -from opentelemetry.instrumentation.urllib3 import ( - URLLib3Instrumentor, -) +from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY +from opentelemetry.instrumentation.urllib3 import URLLib3Instrumentor from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY -from opentelemetry.context import ( - _SUPPRESS_HTTP_INSTRUMENTATION_KEY -) from opentelemetry.propagate import get_global_textmap, set_global_textmap from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.mock_textmap import MockTextMapPropagator From 16004fbbeb8e15b036cd851d2ea52fbbf1d4d70b Mon Sep 17 00:00:00 2001 From: Carol Abadeer Date: Mon, 6 Jun 2022 14:09:55 -0700 Subject: [PATCH 05/10] Update core repo SHA --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e6b04a3e9b..dffd83a7a8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,7 +6,7 @@ on: - 'release/*' pull_request: env: - CORE_REPO_SHA: c82829283d3e99aa2e089d1774ee509619650617 + CORE_REPO_SHA: c82829283d3e99aa2e089d1774ee509619650617 jobs: build: From 130690fa3a4c39a82f0efd87cbc1a569b97c353e Mon Sep 17 00:00:00 2001 From: Carol Abadeer Date: Tue, 7 Jun 2022 16:34:28 -0700 Subject: [PATCH 06/10] Updated changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb4f59e119..8a5c72468b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([1109](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1109)) - cleanup type hints for textmap `Getter` and `Setter` classes ([1106](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1106)) +- Suppressing downstream HTTP instrumentation to avoid [extra spans](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/930) + ([1116](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1116)) ### Added - `opentelemetry-instrumentation-remoulade` Initial release From 2135e532387c4e8d3b1f5943e7d17eb7c9814584 Mon Sep 17 00:00:00 2001 From: Carol Abadeer Date: Thu, 9 Jun 2022 23:53:52 -0700 Subject: [PATCH 07/10] Added unit tests for botocore, requests, and urllib --- .../tests/test_botocore_instrumentation.py | 12 ++++++++++++ .../tests/test_requests_integration.py | 13 +++++++++++++ .../tests/test_urllib_integration.py | 13 +++++++++++++ 3 files changed, 38 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py b/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py index c7249c9afb..3fe6538fe8 100644 --- a/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py @@ -30,6 +30,7 @@ from opentelemetry.context import attach, detach, set_value from opentelemetry.instrumentation.botocore import BotocoreInstrumentor from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY +from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.propagate import get_global_textmap, set_global_textmap from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.mock_textmap import MockTextMapPropagator @@ -325,6 +326,17 @@ def test_suppress_instrumentation_xray_client(self): finally: detach(token) self.assertEqual(0, len(self.get_finished_spans())) + + @mock_xray + def test_suppress_http_instrumentation_xray_client(self): + xray_client = self._make_client("xray") + token = attach(set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True)) + try: + xray_client.put_trace_segments(TraceSegmentDocuments=["str1"]) + xray_client.put_trace_segments(TraceSegmentDocuments=["str2"]) + finally: + detach(token) + self.assertEqual(2, len(self.get_finished_spans())) @mock_s3 def test_request_hook(self): diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py index 1375333b67..32f9ec0bd2 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py +++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py @@ -24,6 +24,7 @@ from opentelemetry import context, trace from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY +from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.propagate import get_global_textmap, set_global_textmap from opentelemetry.sdk import resources from opentelemetry.semconv.trace import SpanAttributes @@ -245,6 +246,18 @@ def test_suppress_instrumentation(self): context.detach(token) self.assert_span(num_spans=0) + + def test_suppress_http_instrumentation(self): + token = context.attach( + context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True) + ) + try: + result = self.perform_request(self.URL) + self.assertEqual(result.text, "Hello!") + finally: + context.detach(token) + + self.assert_span(num_spans=0) def test_not_recording(self): with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span: diff --git a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py index 405ff43c1e..4654fb3d83 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py +++ b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py @@ -28,6 +28,7 @@ URLLibInstrumentor, ) from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY +from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.propagate import get_global_textmap, set_global_textmap from opentelemetry.sdk import resources from opentelemetry.semconv.trace import SpanAttributes @@ -188,6 +189,18 @@ def test_suppress_instrumentation(self): self.assert_span(num_spans=0) + def test_suppress_http_instrumentation(self): + token = context.attach( + context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True) + ) + try: + result = self.perform_request(self.URL) + self.assertEqual(result.read(), b"Hello!") + finally: + context.detach(token) + + self.assert_span(num_spans=0) + def test_not_recording(self): with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span: URLLibInstrumentor().uninstrument() From 2079f17a0d23986524992a9c2390c5b39fb7ab7e Mon Sep 17 00:00:00 2001 From: Carol Abadeer Date: Thu, 16 Jun 2022 12:38:27 -0700 Subject: [PATCH 08/10] fix formatting / lint issues with black --- .../tests/test_botocore_instrumentation.py | 10 +++++++--- .../tests/test_requests_integration.py | 4 ++-- .../tests/test_urllib_integration.py | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py b/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py index 3fe6538fe8..9a5d8429b5 100644 --- a/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py @@ -27,10 +27,14 @@ ) from opentelemetry import trace as trace_api -from opentelemetry.context import attach, detach, set_value +from opentelemetry.context import ( + _SUPPRESS_HTTP_INSTRUMENTATION_KEY, + attach, + detach, + set_value, +) from opentelemetry.instrumentation.botocore import BotocoreInstrumentor from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY -from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.propagate import get_global_textmap, set_global_textmap from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.mock_textmap import MockTextMapPropagator @@ -326,7 +330,7 @@ def test_suppress_instrumentation_xray_client(self): finally: detach(token) self.assertEqual(0, len(self.get_finished_spans())) - + @mock_xray def test_suppress_http_instrumentation_xray_client(self): xray_client = self._make_client("xray") diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py index 32f9ec0bd2..7c0a17f263 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py +++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py @@ -22,9 +22,9 @@ import opentelemetry.instrumentation.requests from opentelemetry import context, trace +from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY -from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.propagate import get_global_textmap, set_global_textmap from opentelemetry.sdk import resources from opentelemetry.semconv.trace import SpanAttributes @@ -246,7 +246,7 @@ def test_suppress_instrumentation(self): context.detach(token) self.assert_span(num_spans=0) - + def test_suppress_http_instrumentation(self): token = context.attach( context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True) diff --git a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py index 4654fb3d83..e004b90e25 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py +++ b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py @@ -24,11 +24,11 @@ import opentelemetry.instrumentation.urllib # pylint: disable=no-name-in-module,import-error from opentelemetry import context, trace +from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.urllib import ( # pylint: disable=no-name-in-module,import-error URLLibInstrumentor, ) from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY -from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.propagate import get_global_textmap, set_global_textmap from opentelemetry.sdk import resources from opentelemetry.semconv.trace import SpanAttributes From 1a3fed6324fdb46e56ccb7e536d1a437238d1a4c Mon Sep 17 00:00:00 2001 From: Carol Abadeer Date: Thu, 16 Jun 2022 17:13:44 -0700 Subject: [PATCH 09/10] fixed lint issues in botocore instrumentor --- .../src/opentelemetry/instrumentation/botocore/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py index e06338ae3f..37aa6734ad 100644 --- a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py @@ -106,6 +106,7 @@ def response_hook(span, service_name, operation_name, result): logger = logging.getLogger(__name__) + # pylint: disable=unused-argument def _patched_endpoint_prepare_request(wrapped, instance, args, kwargs): request = args[0] From 01a2bee973b2cb1de609677352876674b581dc4a Mon Sep 17 00:00:00 2001 From: Carol Abadeer Date: Fri, 17 Jun 2022 14:55:34 -0700 Subject: [PATCH 10/10] add comment to fix _SUPPRESS_HTTP_INSTRUMENTATION_KEY import --- .../src/opentelemetry/instrumentation/botocore/__init__.py | 2 ++ .../src/opentelemetry/instrumentation/requests/__init__.py | 2 ++ .../tests/test_requests_integration.py | 2 ++ .../src/opentelemetry/instrumentation/urllib/__init__.py | 2 ++ .../tests/test_urllib_integration.py | 2 ++ .../src/opentelemetry/instrumentation/urllib3/__init__.py | 2 ++ .../tests/test_urllib3_integration.py | 2 ++ 7 files changed, 14 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py index 37aa6734ad..40a760d525 100644 --- a/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py @@ -87,6 +87,8 @@ def response_hook(span, service_name, operation_name, result): from wrapt import wrap_function_wrapper from opentelemetry import context as context_api + +# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined. from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.botocore.extensions import _find_extension from opentelemetry.instrumentation.botocore.extensions.types import ( diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index c01d3659e4..1f66ddeac3 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -57,6 +57,8 @@ from requests.structures import CaseInsensitiveDict from opentelemetry import context + +# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined. from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.requests.package import _instruments diff --git a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py index 7c0a17f263..626337b67c 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py +++ b/instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py @@ -22,6 +22,8 @@ import opentelemetry.instrumentation.requests from opentelemetry import context, trace + +# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined. from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY diff --git a/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py b/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py index f1a6b52043..04244989f3 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py @@ -75,6 +75,8 @@ def response_hook(span, request_obj, response) ) from opentelemetry import context + +# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined. from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.urllib.package import _instruments diff --git a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py index e004b90e25..d819d481cc 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py +++ b/instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py @@ -24,6 +24,8 @@ import opentelemetry.instrumentation.urllib # pylint: disable=no-name-in-module,import-error from opentelemetry import context, trace + +# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined. from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.urllib import ( # pylint: disable=no-name-in-module,import-error URLLibInstrumentor, diff --git a/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py b/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py index 5b663936c4..e4973613d1 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py @@ -72,6 +72,8 @@ def response_hook(span, request, response): import wrapt from opentelemetry import context + +# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined. from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.instrumentor import BaseInstrumentor from opentelemetry.instrumentation.urllib3.package import _instruments diff --git a/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py b/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py index 19d68492f5..2e70a9d2ab 100644 --- a/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py +++ b/instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py @@ -20,6 +20,8 @@ import urllib3.exceptions from opentelemetry import context, trace + +# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined. from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY from opentelemetry.instrumentation.urllib3 import URLLib3Instrumentor from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY