Skip to content

Commit 9d0834d

Browse files
authored
Auto-instrumentation should exclude packages mentioned in OTEL_PYTHON_DISABLED_INSTRUMENTATIONS env variable (open-telemetry#1461)
1 parent d8c3c24 commit 9d0834d

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
([#1441](https://github.com/open-telemetry/opentelemetry-python/pull/1441))
1111

1212
### Added
13+
- Added the ability to disable instrumenting libraries specified by OTEL_PYTHON_DISABLED_INSTRUMENTATIONS env variable, when using opentelemetry-instrument command.
14+
([#1461](https://github.com/open-telemetry/opentelemetry-python/pull/1461))
1315
- Add `fields` to propagators
1416
([#1374](https://github.com/open-telemetry/opentelemetry-python/pull/1374))
1517
- Add local/remote samplers to parent based sampler

opentelemetry-instrumentation/README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ The code in ``program.py`` needs to use one of the packages for which there is
8282
an OpenTelemetry integration. For a list of the available integrations please
8383
check `here <https://opentelemetry-python.readthedocs.io/en/stable/index.html#integrations>`_
8484

85+
* ``OTEL_PYTHON_DISABLED_INSTRUMENTATIONS``
86+
87+
If set by the user, opentelemetry-instrument will read this environment variable to disable specific instrumentations.
88+
e.g OTEL_PYTHON_DISABLED_INSTRUMENTATIONS = "requests,django"
89+
90+
8591
Examples
8692
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8793

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
from pkg_resources import iter_entry_points
2020

21+
from opentelemetry.configuration import Configuration
22+
2123
logger = getLogger(__file__)
2224

2325

@@ -27,8 +29,19 @@ def _load_distros():
2729

2830

2931
def _load_instrumentors():
32+
package_to_exclude = Configuration().get("DISABLED_INSTRUMENTATIONS", [])
33+
if isinstance(package_to_exclude, str):
34+
package_to_exclude = package_to_exclude.split(",")
35+
# to handle users entering "requests , flask" or "requests, flask" with spaces
36+
package_to_exclude = [x.strip() for x in package_to_exclude]
37+
3038
for entry_point in iter_entry_points("opentelemetry_instrumentor"):
3139
try:
40+
if entry_point.name in package_to_exclude:
41+
logger.debug(
42+
"Instrumentation skipped for library %s", entry_point.name
43+
)
44+
continue
3245
entry_point.load()().instrument() # type: ignore
3346
logger.debug("Instrumented %s", entry_point.name)
3447
except Exception as exc: # pylint: disable=broad-except

0 commit comments

Comments
 (0)