Commit e83e336
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- aws-opentelemetry-distro/src/amazon/opentelemetry/distro
1 file changed
+19
-0
lines changedLines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
| |||
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| 15 | + | |
| 16 | + | |
13 | 17 | | |
14 | 18 | | |
15 | 19 | | |
| |||
36 | 40 | | |
37 | 41 | | |
38 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
39 | 58 | | |
40 | 59 | | |
41 | 60 | | |
| |||
0 commit comments