Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information