open-telemetry / open-telemetry/opentelemetry-python-contrib
Research if Django instrumentation can auto-detect `DJANGO_SETTINGS_MODULE` when auto-instrumentation is used
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 1.1k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 16
Description
Currently users are required to set the DJANGO_SETTINGS_MODULE env var when auto-instrumenting Django. It would be convenient for the auto-instrumentation to be able to figure this out automatically.
hint: it might be possible to fix this by lazily import django.conf.settings instead of here: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/__init__.py#L80
Currenly the instrumentation fails if the env var is not setup:
2021-09-10 19:05:16,525 ERROR [/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py] [sitecustomize.py:84] [trace_id=0 span_id=0 resource.service.name=my-django-service] - Instrumenting of django failed
Traceback (most recent call last):
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py", line 81, in _load_instrumentors
distro.load_instrumentor(entry_point, skip_dep_check=True)
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/opentelemetry/instrumentation/distro.py", line 63, in load_instrumentor
instrumentor().instrument(**kwargs)
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/opentelemetry/instrumentation/instrumentor.py", line 109, in instrument
result = self._instrument( # pylint: disable=assignment-from-no-return
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/opentelemetry/instrumentation/django/__init__.py", line 132, in _instrument
settings_middleware = getattr(settings, "MIDDLEWARE", [])
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 63, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting MIDDLEWARE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
2021-09-10 19:05:16,528 ERROR [/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py] [sitecustomize.py:113] [trace_id=0 span_id=0 resource.service.name=my-django-service] - Failed to auto initialize opentelemetry
Traceback (most recent call last):
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py", line 111, in initialize
_load_instrumentors(distro)
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py", line 85, in _load_instrumentors
raise exc
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py", line 81, in _load_instrumentors
distro.load_instrumentor(entry_point, skip_dep_check=True)
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/opentelemetry/instrumentation/distro.py", line 63, in load_instrumentor
instrumentor().instrument(**kwargs)
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/opentelemetry/instrumentation/instrumentor.py", line 109, in instrument
result = self._instrument( # pylint: disable=assignment-from-no-return
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/opentelemetry/instrumentation/django/__init__.py", line 132, in _instrument
settings_middleware = getattr(settings, "MIDDLEWARE", [])
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/Users/olone/playground/django-otel/venv/lib/python3.9/site-packages/django/conf/__init__.py", line 63, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting MIDDLEWARE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
September 10, 2021 - 13:35:17
Django version 3.2.7, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/init.py around line 80 and the _instrument path shown at line 132. Compare when django.conf.settings is imported with the auto-instrumentation flow in sitecustomize.py, then reproduce startup with and without DJANGO_SETTINGS_MODULE. Done means establishing whether settings can be detected safely and recording or implementing behavior that avoids the reported initialization failure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, python
- Domain
- backend, observability
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100