Skip to content

Commit 6503cdf

Browse files
authored
Fix HTTP instrumentation not being suppressed (#1116)
1 parent 62e0a31 commit 6503cdf

File tree

10 files changed

+66
-32
lines changed

10 files changed

+66
-32
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- 'release/*'
77
pull_request:
88
env:
9-
CORE_REPO_SHA: cad776a2031c84fb3c3a1af90ee2a939f3394b9a
9+
CORE_REPO_SHA: c82829283d3e99aa2e089d1774ee509619650617
1010

1111
jobs:
1212
build:

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- `opentelemetry-instrumentation-grpc` narrow protobuf dependency to exclude protobuf >= 4
1818
([#1109](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1109))
1919
- cleanup type hints for textmap `Getter` and `Setter` classes
20-
([#1106](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1106))
20+
- Suppressing downstream HTTP instrumentation to avoid [extra spans](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/930)
21+
([#1116](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1116))
2122
- fixed typo in `system.network.io` metric configuration
2223
([#1135](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1135))
2324

instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/__init__.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ def response_hook(span, service_name, operation_name, result):
8787
from wrapt import wrap_function_wrapper
8888

8989
from opentelemetry import context as context_api
90+
91+
# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined.
92+
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
9093
from opentelemetry.instrumentation.botocore.extensions import _find_extension
9194
from opentelemetry.instrumentation.botocore.extensions.types import (
9295
_AwsSdkCallContext,
@@ -105,13 +108,6 @@ def response_hook(span, service_name, operation_name, result):
105108

106109
logger = logging.getLogger(__name__)
107110

108-
# A key to a context variable to avoid creating duplicate spans when instrumenting
109-
# both botocore.client and urllib3.connectionpool.HTTPConnectionPool.urlopen since
110-
# botocore calls urlopen
111-
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context_api.create_key(
112-
"suppress_http_instrumentation"
113-
)
114-
115111

116112
# pylint: disable=unused-argument
117113
def _patched_endpoint_prepare_request(wrapped, instance, args, kwargs):

instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
)
2828

2929
from opentelemetry import trace as trace_api
30-
from opentelemetry.context import attach, detach, set_value
30+
from opentelemetry.context import (
31+
_SUPPRESS_HTTP_INSTRUMENTATION_KEY,
32+
attach,
33+
detach,
34+
set_value,
35+
)
3136
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
3237
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
3338
from opentelemetry.propagate import get_global_textmap, set_global_textmap
@@ -326,6 +331,17 @@ def test_suppress_instrumentation_xray_client(self):
326331
detach(token)
327332
self.assertEqual(0, len(self.get_finished_spans()))
328333

334+
@mock_xray
335+
def test_suppress_http_instrumentation_xray_client(self):
336+
xray_client = self._make_client("xray")
337+
token = attach(set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True))
338+
try:
339+
xray_client.put_trace_segments(TraceSegmentDocuments=["str1"])
340+
xray_client.put_trace_segments(TraceSegmentDocuments=["str2"])
341+
finally:
342+
detach(token)
343+
self.assertEqual(2, len(self.get_finished_spans()))
344+
329345
@mock_s3
330346
def test_request_hook(self):
331347
request_hook_service_attribute_name = "request_hook.service_name"

instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
from requests.structures import CaseInsensitiveDict
5858

5959
from opentelemetry import context
60+
61+
# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined.
62+
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
6063
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
6164
from opentelemetry.instrumentation.requests.package import _instruments
6265
from opentelemetry.instrumentation.requests.version import __version__
@@ -75,12 +78,6 @@
7578
)
7679
from opentelemetry.util.http.httplib import set_ip_on_next_http_connection
7780

78-
# A key to a context variable to avoid creating duplicate spans when instrumenting
79-
# both, Session.request and Session.send, since Session.request calls into Session.send
80-
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context.create_key(
81-
"suppress_http_instrumentation"
82-
)
83-
8481
_excluded_urls_from_env = get_excluded_urls("REQUESTS")
8582

8683

instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
import opentelemetry.instrumentation.requests
2424
from opentelemetry import context, trace
25+
26+
# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined.
27+
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
2528
from opentelemetry.instrumentation.requests import RequestsInstrumentor
2629
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
2730
from opentelemetry.propagate import get_global_textmap, set_global_textmap
@@ -246,6 +249,18 @@ def test_suppress_instrumentation(self):
246249

247250
self.assert_span(num_spans=0)
248251

252+
def test_suppress_http_instrumentation(self):
253+
token = context.attach(
254+
context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True)
255+
)
256+
try:
257+
result = self.perform_request(self.URL)
258+
self.assertEqual(result.text, "Hello!")
259+
finally:
260+
context.detach(token)
261+
262+
self.assert_span(num_spans=0)
263+
249264
def test_not_recording(self):
250265
with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span:
251266
RequestsInstrumentor().uninstrument()

instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def response_hook(span, request_obj, response)
7575
)
7676

7777
from opentelemetry import context
78+
79+
# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined.
80+
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
7881
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
7982
from opentelemetry.instrumentation.urllib.package import _instruments
8083
from opentelemetry.instrumentation.urllib.version import __version__
@@ -88,12 +91,6 @@ def response_hook(span, request_obj, response)
8891
from opentelemetry.trace.status import Status
8992
from opentelemetry.util.http import remove_url_credentials
9093

91-
# A key to a context variable to avoid creating duplicate spans when instrumenting
92-
# both, Session.request and Session.send, since Session.request calls into Session.send
93-
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context.create_key(
94-
"suppress_http_instrumentation"
95-
)
96-
9794
_RequestHookT = typing.Optional[typing.Callable[[Span, Request], None]]
9895
_ResponseHookT = typing.Optional[
9996
typing.Callable[[Span, Request, client.HTTPResponse], None]

instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
import opentelemetry.instrumentation.urllib # pylint: disable=no-name-in-module,import-error
2626
from opentelemetry import context, trace
27+
28+
# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined.
29+
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
2730
from opentelemetry.instrumentation.urllib import ( # pylint: disable=no-name-in-module,import-error
2831
URLLibInstrumentor,
2932
)
@@ -188,6 +191,18 @@ def test_suppress_instrumentation(self):
188191

189192
self.assert_span(num_spans=0)
190193

194+
def test_suppress_http_instrumentation(self):
195+
token = context.attach(
196+
context.set_value(_SUPPRESS_HTTP_INSTRUMENTATION_KEY, True)
197+
)
198+
try:
199+
result = self.perform_request(self.URL)
200+
self.assertEqual(result.read(), b"Hello!")
201+
finally:
202+
context.detach(token)
203+
204+
self.assert_span(num_spans=0)
205+
191206
def test_not_recording(self):
192207
with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span:
193208
URLLibInstrumentor().uninstrument()

instrumentation/opentelemetry-instrumentation-urllib3/src/opentelemetry/instrumentation/urllib3/__init__.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ def response_hook(span, request, response):
7272
import wrapt
7373

7474
from opentelemetry import context
75+
76+
# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined.
77+
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
7578
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
7679
from opentelemetry.instrumentation.urllib3.package import _instruments
7780
from opentelemetry.instrumentation.urllib3.version import __version__
@@ -86,12 +89,6 @@ def response_hook(span, request, response):
8689
from opentelemetry.trace.status import Status
8790
from opentelemetry.util.http.httplib import set_ip_on_next_http_connection
8891

89-
# A key to a context variable to avoid creating duplicate spans when instrumenting
90-
# both, Session.request and Session.send, since Session.request calls into Session.send
91-
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context.create_key(
92-
"suppress_http_instrumentation"
93-
)
94-
9592
_UrlFilterT = typing.Optional[typing.Callable[[str], str]]
9693
_RequestHookT = typing.Optional[
9794
typing.Callable[

instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
import urllib3.exceptions
2121

2222
from opentelemetry import context, trace
23-
from opentelemetry.instrumentation.urllib3 import (
24-
_SUPPRESS_HTTP_INSTRUMENTATION_KEY,
25-
URLLib3Instrumentor,
26-
)
23+
24+
# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined.
25+
from opentelemetry.context import _SUPPRESS_HTTP_INSTRUMENTATION_KEY
26+
from opentelemetry.instrumentation.urllib3 import URLLib3Instrumentor
2727
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
2828
from opentelemetry.propagate import get_global_textmap, set_global_textmap
2929
from opentelemetry.semconv.trace import SpanAttributes

0 commit comments

Comments
 (0)