diff --git a/CHANGELOG.md b/CHANGELOG.md index dad442d2522..f6bd54ab483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1441](https://github.com/open-telemetry/opentelemetry-python/pull/1441)) ### Added +- Added the ability to disable instrumenting libraries specified by OTEL_PYTHON_DISABLED_INSTRUMENTATIONS env variable, when using opentelemetry-instrument command. + ([#1461](https://github.com/open-telemetry/opentelemetry-python/pull/1461)) - Add `fields` to propagators ([#1374](https://github.com/open-telemetry/opentelemetry-python/pull/1374)) - Add local/remote samplers to parent based sampler diff --git a/opentelemetry-instrumentation/README.rst b/opentelemetry-instrumentation/README.rst index aed9909a041..97ebc5e7c49 100644 --- a/opentelemetry-instrumentation/README.rst +++ b/opentelemetry-instrumentation/README.rst @@ -82,6 +82,12 @@ The code in ``program.py`` needs to use one of the packages for which there is an OpenTelemetry integration. For a list of the available integrations please check `here `_ +* ``OTEL_PYTHON_DISABLED_INSTRUMENTATIONS`` + +If set by the user, opentelemetry-instrument will read this environment variable to disable specific instrumentations. +e.g OTEL_PYTHON_DISABLED_INSTRUMENTATIONS = "requests,django" + + Examples ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py index 2a51d0b8034..3465619ea3c 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py @@ -18,6 +18,8 @@ from pkg_resources import iter_entry_points +from opentelemetry.configuration import Configuration + logger = getLogger(__file__) @@ -27,8 +29,19 @@ def _load_distros(): def _load_instrumentors(): + package_to_exclude = Configuration().get("DISABLED_INSTRUMENTATIONS", []) + if isinstance(package_to_exclude, str): + package_to_exclude = package_to_exclude.split(",") + # to handle users entering "requests , flask" or "requests, flask" with spaces + package_to_exclude = [x.strip() for x in package_to_exclude] + for entry_point in iter_entry_points("opentelemetry_instrumentor"): try: + if entry_point.name in package_to_exclude: + logger.debug( + "Instrumentation skipped for library %s", entry_point.name + ) + continue entry_point.load()().instrument() # type: ignore logger.debug("Instrumented %s", entry_point.name) except Exception as exc: # pylint: disable=broad-except