Skip to content

Commit e83e336

Browse files
authored
insert CWD to sys.path for Django to find the application directory (#179)
*Issue #, if available:* open-telemetry/opentelemetry-python-contrib#2495 The issue is that users on K8s/EKS/ECS explicitly need to set their Django application container's working directory in the `PYTHONPATH` environment variable ([operator code](https://github.com/open-telemetry/opentelemetry-operator/blob/d42a0ce1166efe86c95f85268f265e338d0002f8/pkg/instrumentation/python.go#L55-L63) that handles the `PYTHONPATH`), otherwise the application fails to start with error messages related to the Django's settings module. *Analysis:* Python auto-instrumentation using the `opentelemetry-instrument` command doesn't have this issue, which is I believe is because of [this specific handling of Django app current directory](https://github.com/open-telemetry/opentelemetry-python-contrib/blob/1ee7261ea7117fbd22e2262e488402213a874125/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/__init__.py#L98-L102) in the auto-instrumentation startup. Since [this script is only run when using the `opentelemetry-instrument` command](https://github.com/open-telemetry/opentelemetry-python-contrib/blob/1ee7261ea7117fbd22e2262e488402213a874125/opentelemetry-instrumentation/pyproject.toml#L34), auto-instrumentation done by the operator (using the [sitecustomize](https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py) I believe) doesn't set the current working directory in the PYTHONPATH. *Description of changes:* Solution is to insert the current working directory to the `sys.path` within the auto-instrumentation so that Django can find the application folder. We are adding to the `sys.path` and not `PYTHONPATH` because at this point the python process has already started and mutations to `PYTHONPATH` doesn't reflect in the `sys.path` which is what Django uses. *Testing:* Manually tested by deploying a [Django sample application](https://github.com/srprash/otel-python-k8s-samples/tree/main/django) to minikube without setting the `PYTHONPATH` in the container environment. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent a4a0c33 commit e83e336

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Diff for: aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_opentelemetry_distro.py

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
33
import os
4+
import sys
5+
from logging import Logger, getLogger
46

57
from amazon.opentelemetry.distro.patches._instrumentation_patch import apply_instrumentation_patches
68
from opentelemetry.distro import OpenTelemetryDistro
@@ -10,6 +12,8 @@
1012
OTEL_EXPORTER_OTLP_PROTOCOL,
1113
)
1214

15+
_logger: Logger = getLogger(__name__)
16+
1317

1418
class AwsOpenTelemetryDistro(OpenTelemetryDistro):
1519
def _configure(self, **kwargs):
@@ -36,6 +40,21 @@ def _configure(self, **kwargs):
3640
OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION environment variable. Need to work with upstream to
3741
make it to be configurable.
3842
"""
43+
44+
# Issue: https://github.com/open-telemetry/opentelemetry-python-contrib/issues/2495
45+
# mimicking what is done here: https://tinyurl.com/54mvzmte
46+
# For handling applications like django running in containers, we are setting the current working directory
47+
# to the sys.path for the django application to find its executables.
48+
#
49+
# Note that we are updating the sys.path and not the PYTHONPATH env var, because once sys.path is
50+
# loaded upon process start, it doesn't refresh from the PYTHONPATH value.
51+
#
52+
# To be removed once the issue has been fixed in https://github.com/open-telemetry/opentelemetry-python-contrib
53+
cwd_path = os.getcwd()
54+
_logger.debug("Current working directory path: %s", cwd_path)
55+
if cwd_path not in sys.path:
56+
sys.path.insert(0, cwd_path)
57+
3958
os.environ.setdefault(OTEL_EXPORTER_OTLP_PROTOCOL, "http/protobuf")
4059

4160
super(AwsOpenTelemetryDistro, self)._configure()

0 commit comments

Comments
 (0)