open-telemetry / open-telemetry/opentelemetry-python-contrib
Django instrumentation - Trace request attributes also in the response
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 1.1k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 16
Description
Sorry, but I'm slightly confused whether I should create an issue in this repository or https://github.com/open-telemetry/opentelemetry-python-contrib. If it's the other one, i'll gladly reopen it there.
Is your feature request related to a problem?
According to the docs and the implementation, you can use OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS to specify what request attributes should be traced.
The middleware uses these options during process_request in https://github.com/open-telemetry/opentelemetry-python/blob/master/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py#L147
The problem is that some of the request attributes that one could wish to trace are available only after some other middlewares run. For example (pretty common setups):
django.contrib.auth.middleware.AuthenticationMiddlewareadds theuserattributedjango.contrib.sites.middleware.CurrentSiteMiddlewareadds thesiteattribute
Of course, we cannot mess with the middleware order, but these attributes are available in the process_response part of the middleware.
Describe the solution you'd like
It would be nice if we could specify attributes that would be extracted in the process_response, so that we can extract these as well using the instrumentation library.
I do not have a preference, and I am describing the options that come to my mind in the next section.
Describe alternatives you've considered
- Extract all attributes in
process_responseinstead ofprocess_request - Include additional configuration variable so that we somehow distinguish between the two extraction places.
- Try extracting the attributes in
process_requestas well asprocess_response(maybe only try to extract the unextracted ones again?)
Additional context
I am willing to create a PR for this upon the decision to do it one way or another.
Thanks.
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 resolving the repository ownership and design questions in the issue thread. Then read instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py around process_request and the corresponding process_response path. Done means the agreed approach allows configured request attributes available only during response processing to be traced.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, python
- Domain
- backend, observability
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100