open-telemetry / open-telemetry/opentelemetry-python-contrib

Django app on K8s crashes when auto-instrumented via the opentelemetry-operator without explicitly setting the PYTHONPATH and DJANGO_SETTINGS_MODULE

Open
#2,495 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
4d 15h
Merged PRs (30d)
16

Description

Describe your environment Describe any aspect of your environment relevant to the problem, including your Python version, platform, version numbers of installed dependencies, information about your cloud hosting provider, etc. If you're reporting a problem with a specific version of a library in this repo, please check whether the problem has been fixed on main.

  • Python version: 3.11
  • Platform: Kubernetes
  • Installed dependencies:
    • Django~=5.0.4
    • requests==2.31.0
  • Hosting: hosting locally on minikube, but same issue when hosted on AWS EKS

Steps to reproduce
Sample app code is here: https://github.com/srprash/otel-python-k8s-samples/tree/main/django

  1. Install Docker and Minikube, and run both.
  2. Run eval $(minikube docker-env) so that minikube can use the docker images from local repository.
  3. Build the application docker image using docker build -t django-app .
  4. Deploy the application to minikube using kubectl apply -f k8s/deployment.yaml and kubectl apply -f k8s/service.yaml
  5. Enable port-forwarding kubectl port-forward svc/django-service 8000
  6. Make a request to the application on http://127.0.0.1:8000/outgoing-http-call endpoint

What is the expected behavior?
What did you expect to see?

  • The application should run without an issues and produce a server span for the incoming request and a client span for the outgoing http request

What is the actual behavior?
What did you see instead?

  • The application fails to start with the error:
    Defaulted container "django-app" out of: django-app, opentelemetry-auto-instrumentation-python (init)
    CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
    
  • When I uncomment these lines (that is, explicitly set the PYTHONPATH and DJANGO_SETTINGS_MODULE), only then the application works correctly and is instrumented.

Additional context

  • Another related issue: https://github.com/open-telemetry/opentelemetry-operator/issues/2302
  • Note that I have another application using Flask and this issue is not seen there.
  • The issue with Django is specifically when using the opentelemetry-operator to do the auto-instrumentation by adding the instrumentation.opentelemetry.io/inject-python: "true" annotation to the application deployment manifest. Auto-instrumentation via the opentelemetry-instrument command works fine (likely due to this code that handles the current working directory).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with the linked Django sample and k8s/deployment.yaml, first comparing the commented PYTHONPATH and DJANGO_SETTINGS_MODULE lines with the injected setup. Read the related operator issue and the linked auto_instrumentation code that handles the working directory. Done means the Django app starts and produces both incoming server and outgoing client spans without explicitly setting those variables.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, kubernetes, python
Domain
backend, devops, observability-sre
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.