open-telemetry / open-telemetry/opentelemetry-python-contrib
Request hooks are not passed into middleware when using DjangoInstrumentor with gunicorn
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 1.1k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 16
Description
Describe your environment Running Gunicorn on Django inside of kubernetes, Python 3.10
Steps to reproduce
Define the following in gunicorn.conf.py
from opentelemetry.instrumentation.django import DjangoInstrumentor
import opentelemetry
def post_fork(server, worker):
from opentelemetry.instrumentation.auto_instrumentation import sitecustomize
server.log.info("Worker spawned (pid: %s)", worker.pid)
def request_hook(span, request):
server.log.info("Running request hook...")
span.update_name(f"{request.method} {request.path}")
# passing the hooks in this intializer does NOT work
DjangoInstrumentor().instrument(request_hook=request_hook)
# applying the hook directly to the middleware works as intended. copied from the implementation
opentelemetry.instrumentation.django.middleware.otel_middleware._DjangoMiddleware._otel_request_hook = request_hook
What is the expected behavior?
I expected to have the spans renamed and a log print out in my console.
What is the actual behavior?
The function is never called. I confirmed that it's not even in the middleware by shelling into my pod and evaluating opentelemetry.instrumentation.django.middleware.otel_middleware._DjangoMiddleware._otel_request_hook. It evaluates to None.
Additional context
Not sure if this is a gunicorn specific issue or not. My solution is incredibly hacky.
I tested this with 0.42b0 - 0.44b0
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
Start by tracing DjangoInstrumentor().instrument(request_hook=request_hook) into opentelemetry.instrumentation.django.middleware.otel_middleware._DjangoMiddleware, comparing it with the direct middleware assignment shown in the report. Confirm the request hook reaches the middleware during Gunicorn post_fork execution and add or update coverage for the expected span rename and log behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, python
- Domain
- backend, observability-sre
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100