Skip to content

Commit 5279805

Browse files
authored
opentelemetry-instrumentation: add support to skip all the available instrumentations (#3967)
* opentelemetry-instrumentation: add support to skip all the available instrumentations Introduce support for skipping all the available instrumentation with a wildcard *. e.g. OTEL_PYTHON_DISABLED_INSTRUMENTATIONS="*" * Add changelog
1 parent c284e22 commit 5279805

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3434
- `opentelemetry-instrumentation-django`: improve docs for response_hook with examples of providing attributes from middlewares
3535
([#3923](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3923))
3636
- Update for Log SDK breaking changes. Rename InMemoryLogExporter to InMemoryLogRecordExporter in several tests
37-
([#3850](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3589))
37+
([#3589](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3589))
38+
- opentelemetry-instrumentation: allow to skip all instrumentations loading with a wildcard
39+
([#3967](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3967))
3840

3941
### Fixed
4042

opentelemetry-instrumentation/README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ check `here <https://opentelemetry-python.readthedocs.io/en/stable/index.html#in
106106
If set by the user, opentelemetry-instrument will read this environment variable to disable specific instrumentations.
107107
e.g OTEL_PYTHON_DISABLED_INSTRUMENTATIONS="requests,django"
108108

109+
If the variables contains ``*`` as member no instrumentation will be enabled.
110+
109111
* ``OTEL_PYTHON_AUTO_INSTRUMENTATION_EXPERIMENTAL_GEVENT_PATCH``
110112

111113
If set by the user to `patch_all` , opentelemetry instrument will call the gevent monkeypatching method ``patch_all``.

opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/_load.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
_logger = getLogger(__name__)
3737

38+
SKIPPED_INSTRUMENTATIONS_WILDCARD = "*"
39+
3840

3941
class _EntryPointDistFinder:
4042
@cached_property
@@ -94,6 +96,9 @@ def _load_instrumentors(distro):
9496
entry_point.load()()
9597

9698
for entry_point in entry_points(group="opentelemetry_instrumentor"):
99+
if SKIPPED_INSTRUMENTATIONS_WILDCARD in package_to_exclude:
100+
break
101+
97102
if entry_point.name in package_to_exclude:
98103
_logger.debug(
99104
"Instrumentation skipped for library %s", entry_point.name

opentelemetry-instrumentation/tests/auto_instrumentation/test_load.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,54 @@ def test_entry_point_dist_finder(self):
492492
)
493493
# dist are not comparable, being truthy is enough
494494
self.assertTrue(new_entry_point_dist)
495+
496+
@patch.dict(
497+
"os.environ",
498+
{OTEL_PYTHON_DISABLED_INSTRUMENTATIONS: "*"},
499+
)
500+
@patch(
501+
"opentelemetry.instrumentation.auto_instrumentation._load.get_dist_dependency_conflicts"
502+
)
503+
@patch(
504+
"opentelemetry.instrumentation.auto_instrumentation._load.entry_points"
505+
)
506+
def test_no_instrumentor_called_with_wildcard(self, iter_mock, mock_dep):
507+
# Mock opentelemetry_pre_instrument entry points
508+
# pylint: disable=too-many-locals
509+
pre_ep_mock1 = Mock()
510+
pre_ep_mock1.name = "pre1"
511+
pre_mock1 = Mock()
512+
pre_ep_mock1.load.return_value = pre_mock1
513+
514+
# Mock opentelemetry_instrumentor entry points
515+
ep_mock1 = Mock()
516+
ep_mock1.name = "instr1"
517+
518+
# Mock opentelemetry_instrumentor entry points
519+
post_ep_mock1 = Mock()
520+
post_ep_mock1.name = "post1"
521+
post_mock1 = Mock()
522+
post_ep_mock1.load.return_value = post_mock1
523+
524+
distro_mock = Mock()
525+
526+
# Mock entry points in order
527+
iter_mock.side_effect = [
528+
(pre_ep_mock1,),
529+
(ep_mock1,),
530+
(post_ep_mock1,),
531+
]
532+
_load._load_instrumentors(distro_mock)
533+
534+
self.assertEqual(iter_mock.call_count, 3)
535+
536+
# All opentelemetry_pre_instrument entry points should be loaded
537+
pre_mock1.assert_called_once()
538+
539+
# No instrumentations should be loaded
540+
mock_dep.assert_not_called()
541+
distro_mock.load_instrumentor.assert_not_called()
542+
self.assertEqual(distro_mock.load_instrumentor.call_count, 0)
543+
544+
# All opentelemetry_post_instrument entry points should be loaded
545+
post_mock1.assert_called_once()

0 commit comments

Comments
 (0)