From 697054ce1585c02befdcf231a5a603e033ac3de5 Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Wed, 20 Apr 2022 21:25:19 +0000 Subject: [PATCH] Prune autoinstrumentation sitecustomize module directory from PYTHONPATH immediately --- CHANGELOG.md | 3 +++ .../auto_instrumentation/sitecustomize.py | 13 +++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1da97d7502..8e7aeea907 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.11.0-0.30b0...HEAD) +- Prune autoinstrumentation sitecustomize module directory from PYTHONPATH immediately + ([#1066](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1066)) + ## [1.11.0-0.30b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.11.0-0.30b0) - 2022-04-18 ### Fixed diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py index dcd9a79fb7..25ee3fe048 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py @@ -110,6 +110,13 @@ def _load_configurators(): def initialize(): + # prevents auto-instrumentation of subprocesses if code execs another python process + environ["PYTHONPATH"] = sub( + rf"{dirname(abspath(__file__))}{pathsep}?", + "", + environ["PYTHONPATH"], + ) + try: distro = _load_distros() distro.configure() @@ -117,12 +124,6 @@ def initialize(): _load_instrumentors(distro) except Exception: # pylint: disable=broad-except logger.exception("Failed to auto initialize opentelemetry") - finally: - environ["PYTHONPATH"] = sub( - rf"{dirname(abspath(__file__))}{pathsep}?", - "", - environ["PYTHONPATH"], - ) initialize()