From 4adf1bbf011e4c793e06c1104ae54f25e1ee2084 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 8 Apr 2020 22:49:11 -0600 Subject: [PATCH] Fix the auto instrumentation command Fixes #566 --- docs/examples/auto-instrumentation/README.md | 2 +- .../auto_instrumentation.py | 20 ++++++------- .../auto_instrumentation/sitecustomize.py | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/sitecustomize.py diff --git a/docs/examples/auto-instrumentation/README.md b/docs/examples/auto-instrumentation/README.md index 46b0b44b2c8..c3c2728489a 100644 --- a/docs/examples/auto-instrumentation/README.md +++ b/docs/examples/auto-instrumentation/README.md @@ -92,7 +92,7 @@ Span(name="serv_request", context=SpanContext(trace_id=0x9c0e0ce8f7b7dbb51d1d6e7 Now, kill the execution of `server_instrumented.py` with `ctrl + c` and run this instead: ```sh -$ opentelemetry-auto-instrumentation opentelemetry-python/opentelemetry-auto-instrumentation/example/server_uninstrumented.py +$ opentelemetry-auto-instrumentation python opentelemetry-python/opentelemetry-auto-instrumentation/example/server_uninstrumented.py ``` In the console where you previously executed `client.py`, run again this again: diff --git a/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/auto_instrumentation.py b/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/auto_instrumentation.py index 00ccf6a0ea9..086bf1dabbe 100644 --- a/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/auto_instrumentation.py +++ b/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/auto_instrumentation.py @@ -15,22 +15,22 @@ # limitations under the License. from logging import getLogger -from runpy import run_path +from os import environ, execl +from os.path import abspath, dirname, pathsep +from shutil import which from sys import argv -from pkg_resources import iter_entry_points - logger = getLogger(__file__) def run() -> None: - for entry_point in iter_entry_points("opentelemetry_instrumentor"): - try: - entry_point.load()().instrument() # type: ignore - logger.debug("Instrumented %s", entry_point.name) + python_path = environ.get("PYTHONPATH", "") + filedir_path = dirname(abspath(__file__)) + + if filedir_path not in python_path: + environ["PYTHONPATH"] = pathsep.join([filedir_path, python_path]) - except Exception: # pylint: disable=broad-except - logger.exception("Instrumenting of %s failed", entry_point.name) + executable = which(argv[1]) - run_path(argv[1], run_name="__main__") # type: ignore + execl(executable, executable, *argv[2:]) diff --git a/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/sitecustomize.py b/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/sitecustomize.py new file mode 100644 index 00000000000..b070bf5d773 --- /dev/null +++ b/opentelemetry-auto-instrumentation/src/opentelemetry/auto_instrumentation/sitecustomize.py @@ -0,0 +1,28 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from logging import getLogger + +from pkg_resources import iter_entry_points + +logger = getLogger(__file__) + + +for entry_point in iter_entry_points("opentelemetry_instrumentor"): + try: + entry_point.load()().instrument() # type: ignore + logger.debug("Instrumented %s", entry_point.name) + + except Exception: # pylint: disable=broad-except + logger.exception("Instrumenting of %s failed", entry_point.name)