mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-28 20:52:57 +08:00
Fix HTTP instrumentation not being suppressed (#1116)
This commit is contained in:
2
.github/workflows/test.yml
vendored
2
.github/workflows/test.yml
vendored
@ -6,7 +6,7 @@ on:
|
|||||||
- 'release/*'
|
- 'release/*'
|
||||||
pull_request:
|
pull_request:
|
||||||
env:
|
env:
|
||||||
CORE_REPO_SHA: cad776a2031c84fb3c3a1af90ee2a939f3394b9a
|
CORE_REPO_SHA: c82829283d3e99aa2e089d1774ee509619650617
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
@ -17,7 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- `opentelemetry-instrumentation-grpc` narrow protobuf dependency to exclude protobuf >= 4
|
- `opentelemetry-instrumentation-grpc` narrow protobuf dependency to exclude protobuf >= 4
|
||||||
([#1109](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1109))
|
([#1109](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1109))
|
||||||
- cleanup type hints for textmap `Getter` and `Setter` classes
|
- 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))
|
||||||
- fixed typo in `system.network.io` metric configuration
|
- fixed typo in `system.network.io` metric configuration
|
||||||
([#1135](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1135))
|
([#1135](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1135))
|
||||||
|
|
||||||
|
@ -87,6 +87,9 @@ from botocore.exceptions import ClientError
|
|||||||
from wrapt import wrap_function_wrapper
|
from wrapt import wrap_function_wrapper
|
||||||
|
|
||||||
from opentelemetry import context as context_api
|
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 import _find_extension
|
||||||
from opentelemetry.instrumentation.botocore.extensions.types import (
|
from opentelemetry.instrumentation.botocore.extensions.types import (
|
||||||
_AwsSdkCallContext,
|
_AwsSdkCallContext,
|
||||||
@ -105,13 +108,6 @@ from opentelemetry.trace.span import Span
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
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
|
|
||||||
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context_api.create_key(
|
|
||||||
"suppress_http_instrumentation"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def _patched_endpoint_prepare_request(wrapped, instance, args, kwargs):
|
def _patched_endpoint_prepare_request(wrapped, instance, args, kwargs):
|
||||||
|
@ -27,7 +27,12 @@ from moto import ( # pylint: disable=import-error
|
|||||||
)
|
)
|
||||||
|
|
||||||
from opentelemetry import trace as trace_api
|
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.botocore import BotocoreInstrumentor
|
||||||
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
|
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
|
||||||
from opentelemetry.propagate import get_global_textmap, set_global_textmap
|
from opentelemetry.propagate import get_global_textmap, set_global_textmap
|
||||||
@ -326,6 +331,17 @@ class TestBotocoreInstrumentor(TestBase):
|
|||||||
detach(token)
|
detach(token)
|
||||||
self.assertEqual(0, len(self.get_finished_spans()))
|
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
|
@mock_s3
|
||||||
def test_request_hook(self):
|
def test_request_hook(self):
|
||||||
request_hook_service_attribute_name = "request_hook.service_name"
|
request_hook_service_attribute_name = "request_hook.service_name"
|
||||||
|
@ -57,6 +57,9 @@ from requests.sessions import Session
|
|||||||
from requests.structures import CaseInsensitiveDict
|
from requests.structures import CaseInsensitiveDict
|
||||||
|
|
||||||
from opentelemetry import context
|
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.instrumentor import BaseInstrumentor
|
||||||
from opentelemetry.instrumentation.requests.package import _instruments
|
from opentelemetry.instrumentation.requests.package import _instruments
|
||||||
from opentelemetry.instrumentation.requests.version import __version__
|
from opentelemetry.instrumentation.requests.version import __version__
|
||||||
@ -75,12 +78,6 @@ from opentelemetry.util.http import (
|
|||||||
)
|
)
|
||||||
from opentelemetry.util.http.httplib import set_ip_on_next_http_connection
|
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
|
|
||||||
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context.create_key(
|
|
||||||
"suppress_http_instrumentation"
|
|
||||||
)
|
|
||||||
|
|
||||||
_excluded_urls_from_env = get_excluded_urls("REQUESTS")
|
_excluded_urls_from_env = get_excluded_urls("REQUESTS")
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,6 +22,9 @@ from requests.models import Response
|
|||||||
|
|
||||||
import opentelemetry.instrumentation.requests
|
import opentelemetry.instrumentation.requests
|
||||||
from opentelemetry import context, trace
|
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.requests import RequestsInstrumentor
|
||||||
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
|
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
|
||||||
from opentelemetry.propagate import get_global_textmap, set_global_textmap
|
from opentelemetry.propagate import get_global_textmap, set_global_textmap
|
||||||
@ -246,6 +249,18 @@ class RequestsIntegrationTestBase(abc.ABC):
|
|||||||
|
|
||||||
self.assert_span(num_spans=0)
|
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):
|
def test_not_recording(self):
|
||||||
with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span:
|
with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span:
|
||||||
RequestsInstrumentor().uninstrument()
|
RequestsInstrumentor().uninstrument()
|
||||||
|
@ -75,6 +75,9 @@ from urllib.request import ( # pylint: disable=no-name-in-module,import-error
|
|||||||
)
|
)
|
||||||
|
|
||||||
from opentelemetry import context
|
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.instrumentor import BaseInstrumentor
|
||||||
from opentelemetry.instrumentation.urllib.package import _instruments
|
from opentelemetry.instrumentation.urllib.package import _instruments
|
||||||
from opentelemetry.instrumentation.urllib.version import __version__
|
from opentelemetry.instrumentation.urllib.version import __version__
|
||||||
@ -88,12 +91,6 @@ from opentelemetry.trace import Span, SpanKind, get_tracer
|
|||||||
from opentelemetry.trace.status import Status
|
from opentelemetry.trace.status import Status
|
||||||
from opentelemetry.util.http import remove_url_credentials
|
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
|
|
||||||
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context.create_key(
|
|
||||||
"suppress_http_instrumentation"
|
|
||||||
)
|
|
||||||
|
|
||||||
_RequestHookT = typing.Optional[typing.Callable[[Span, Request], None]]
|
_RequestHookT = typing.Optional[typing.Callable[[Span, Request], None]]
|
||||||
_ResponseHookT = typing.Optional[
|
_ResponseHookT = typing.Optional[
|
||||||
typing.Callable[[Span, Request, client.HTTPResponse], None]
|
typing.Callable[[Span, Request, client.HTTPResponse], None]
|
||||||
|
@ -24,6 +24,9 @@ import httpretty
|
|||||||
|
|
||||||
import opentelemetry.instrumentation.urllib # pylint: disable=no-name-in-module,import-error
|
import opentelemetry.instrumentation.urllib # pylint: disable=no-name-in-module,import-error
|
||||||
from opentelemetry import context, trace
|
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
|
from opentelemetry.instrumentation.urllib import ( # pylint: disable=no-name-in-module,import-error
|
||||||
URLLibInstrumentor,
|
URLLibInstrumentor,
|
||||||
)
|
)
|
||||||
@ -188,6 +191,18 @@ class RequestsIntegrationTestBase(abc.ABC):
|
|||||||
|
|
||||||
self.assert_span(num_spans=0)
|
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):
|
def test_not_recording(self):
|
||||||
with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span:
|
with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span:
|
||||||
URLLibInstrumentor().uninstrument()
|
URLLibInstrumentor().uninstrument()
|
||||||
|
@ -72,6 +72,9 @@ import urllib3.connectionpool
|
|||||||
import wrapt
|
import wrapt
|
||||||
|
|
||||||
from opentelemetry import context
|
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.instrumentor import BaseInstrumentor
|
||||||
from opentelemetry.instrumentation.urllib3.package import _instruments
|
from opentelemetry.instrumentation.urllib3.package import _instruments
|
||||||
from opentelemetry.instrumentation.urllib3.version import __version__
|
from opentelemetry.instrumentation.urllib3.version import __version__
|
||||||
@ -86,12 +89,6 @@ from opentelemetry.trace import Span, SpanKind, get_tracer
|
|||||||
from opentelemetry.trace.status import Status
|
from opentelemetry.trace.status import Status
|
||||||
from opentelemetry.util.http.httplib import set_ip_on_next_http_connection
|
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
|
|
||||||
_SUPPRESS_HTTP_INSTRUMENTATION_KEY = context.create_key(
|
|
||||||
"suppress_http_instrumentation"
|
|
||||||
)
|
|
||||||
|
|
||||||
_UrlFilterT = typing.Optional[typing.Callable[[str], str]]
|
_UrlFilterT = typing.Optional[typing.Callable[[str], str]]
|
||||||
_RequestHookT = typing.Optional[
|
_RequestHookT = typing.Optional[
|
||||||
typing.Callable[
|
typing.Callable[
|
||||||
|
@ -20,10 +20,10 @@ import urllib3
|
|||||||
import urllib3.exceptions
|
import urllib3.exceptions
|
||||||
|
|
||||||
from opentelemetry import context, trace
|
from opentelemetry import context, trace
|
||||||
from opentelemetry.instrumentation.urllib3 import (
|
|
||||||
_SUPPRESS_HTTP_INSTRUMENTATION_KEY,
|
# FIXME: fix the importing of this private attribute when the location of the _SUPPRESS_HTTP_INSTRUMENTATION_KEY is defined.
|
||||||
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.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
|
||||||
from opentelemetry.propagate import get_global_textmap, set_global_textmap
|
from opentelemetry.propagate import get_global_textmap, set_global_textmap
|
||||||
from opentelemetry.semconv.trace import SpanAttributes
|
from opentelemetry.semconv.trace import SpanAttributes
|
||||||
|
Reference in New Issue
Block a user