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

Django instrumentation - Trace request attributes also in the response

Open
#142 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature-request help wanted instrumentation
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.AuthenticationMiddleware adds the user attribute
  • django.contrib.sites.middleware.CurrentSiteMiddleware adds the site attribute

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

  1. Extract all attributes in process_response instead of process_request
  2. Include additional configuration variable so that we somehow distinguish between the two extraction places.
  3. Try extracting the attributes in process_request as well as process_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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.