Skip to content

Commit

Permalink
Avoid app to crash python agent (#1385)
Browse files Browse the repository at this point in the history
Co-authored-by: Tamir David <tamirdavid@Tamirs-MacBook-Pro.local>
  • Loading branch information
tamirdavid1 and Tamir David authored Jul 22, 2024
1 parent 0df64e9 commit 793503c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
16 changes: 12 additions & 4 deletions agents/python/configurator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# my_otel_configurator/__init__.py
import opentelemetry.sdk._configuration as sdk_config
import threading
import atexit
import os
import opentelemetry.sdk._configuration as sdk_config
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.resources import ProcessResourceDetector, OTELResourceDetector
from .lib_handling import reorder_python_path, reload_distro_modules
from .version import VERSION
from opamp.http_client import OpAMPHTTPClient

class OdigosPythonConfigurator(sdk_config._BaseConfigurator):

def _configure(self, **kwargs):
_initialize_components()

def _initialize_components():


def _initialize_components():
trace_exporters, metric_exporters, log_exporters = sdk_config._import_exporters(
sdk_config._get_exporter_names("traces"),
sdk_config._get_exporter_names("metrics"),
Expand Down Expand Up @@ -42,6 +44,12 @@ def _initialize_components():
initialize_logging_if_enabled(log_exporters, resource)


# Reorder the python sys.path to ensure that the user application's dependencies take precedence over the agent's dependencies.
# This is necessary because the user application's dependencies may be incompatible with those used by the agent.
reorder_python_path()
# Reload distro modules to ensure the new path is used.
reload_distro_modules()

def initialize_traces_if_enabled(trace_exporters, resource):
traces_enabled = os.getenv(sdk_config.OTEL_TRACES_EXPORTER, "none").strip().lower()
if traces_enabled != "none":
Expand Down
36 changes: 36 additions & 0 deletions agents/python/configurator/lib_handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sys
import importlib
from importlib import metadata as md

def reorder_python_path():
paths_to_move = [path for path in sys.path if path.startswith('/var/odigos/python')]

for path in paths_to_move:
sys.path.remove(path)
sys.path.append(path)


def reload_distro_modules() -> None:
# Reload distro modules, as they may have been imported before the path was reordered.
# Add any new distro modules to this list.
needed_modules = [
'google.protobuf',
'requests',
'charset_normalizer',
'certifi',
'asgiref'
'idna',
'deprecated',
'importlib_metadata',
'packaging',
'psutil',
'zipp',
'urllib3',
'uuid_extensions.uuid7',
'typing_extensions',
]

for module_name in needed_modules:
if module_name in sys.modules:
module = sys.modules[module_name]
importlib.reload(module)

0 comments on commit 793503c

Please sign in to comment.