Skip to content

Commit

Permalink
feat: handle instrumentation of sub process
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirdavid1 committed Dec 12, 2024
1 parent 0503b98 commit e8fdbbd
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions initializer/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
MINIMUM_PYTHON_SUPPORTED_VERSION = (3, 8)

def initialize_components(trace_exporters = None, metric_exporters = None, log_exporters = None , span_processor = None):

# In case of forking, the OpAMP client should be started in the child process.
# e.g when using gunicorn/celery with multiple workers.
os.register_at_fork(
after_in_child=lambda: start_opamp_client(threading.Event()),
) # pylint: disable=protected-access


handle_instrumenation_of_sub_processes()

resource_attributes_event = threading.Event()
client = None

Expand Down Expand Up @@ -119,7 +119,6 @@ def initialize_logging_if_enabled(log_exporters, resource):


def start_opamp_client(event):

if os.getenv('DISABLE_OPAMP_CLIENT', 'false').strip().lower() == 'true':
return MockOpAMPClient(event)

Expand All @@ -140,4 +139,23 @@ def shutdown():


def is_supported_python_version():
return sys.version_info >= MINIMUM_PYTHON_SUPPORTED_VERSION
return sys.version_info >= MINIMUM_PYTHON_SUPPORTED_VERSION

def handle_instrumenation_of_sub_processes():
# Resolves a path management issue introduced by OpenTelemetry's sitecustomize implementation.
# OpenTelemetry removed auto_instrumentation from the path to address a specific bug
# (ref: https://github.com/open-telemetry/opentelemetry-python-contrib/issues/1050).
# This modification does not impact our use case, as we utilize auto_instrumentation as a package
# and do not rely on opentelemetry-instrument for running instrumentations.
#
# We are reintroducing the path to enable parallel execution with other agent that inject themselves via sitecustomize.
# We've observed that agents attempt to import user-defined sitecustomize.py [OpenTelemetry one in this case] prior to their own execution
# to prevent application logic disruption.
#
# Given OpenTelemetry's path removal during Distro creation, we must manually restore the path.
#
# Note: This is a temporary solution and should be refactored when:
# - The environment override writer is removed
# - Webhook integration is implemented
# - A custom distro creation mechanism is developed
os.environ["PYTHONPATH"] = f"{os.environ['PYTHONPATH']}:/var/odigos/python/opentelemetry/instrumentation/auto_instrumentation"

0 comments on commit e8fdbbd

Please sign in to comment.