opentelemetry-python-contrib icon indicating copy to clipboard operation
opentelemetry-python-contrib copied to clipboard

Django instrumentation - Trace request attributes also in the response

Open Zajozor opened this issue 5 years ago • 3 comments
trafficstars

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.

Zajozor avatar Oct 27 '20 21:10 Zajozor