Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to extract span attributes from django request objects. #1154

Merged
merged 5 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Changed span name extraction from request to comply semantic convention ([#992](https://github.com/open-telemetry/opentelemetry-python/pull/992))
- Added support for `OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS` ([#1154](https://github.com/open-telemetry/opentelemetry-python/pull/1154))

## Version 0.13b0

Expand Down
15 changes: 15 additions & 0 deletions instrumentation/opentelemetry-instrumentation-django/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ For example,

will exclude requests such as ``https://site/client/123/info`` and ``https://site/xyz/healthcheck``.

Request attributes
********************
To extract certain attributes from Django's request object and use them as span attributes, set the environment variable ``OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS`` to a comma
owais marked this conversation as resolved.
Show resolved Hide resolved
delimited list of request attribute names.

For example,

::

export OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS='path_info,content_type'

will extract path_info and content_type attributes from every traced request and add them as span attritbues.

Django Request object reference: https://docs.djangoproject.com/en/3.1/ref/request-response/#attributes

References
----------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class _DjangoMiddleware(MiddlewareMixin):
else:
_excluded_urls = ExcludeList(_excluded_urls)

_traced_request_attrs = [
attr.strip()
for attr in (Configuration().DJANGO_TRACED_REQUEST_ATTRS or "").split(
","
)
]

@staticmethod
def _get_span_name(request):
try:
Expand Down Expand Up @@ -95,6 +102,10 @@ def process_request(self, request):
tracer = get_tracer(__name__, __version__)

attributes = collect_request_attributes(environ)
for attr in self._traced_request_attrs:
value = getattr(request, attr, None)
if value is not None:
attributes[attr] = str(value)

span = tracer.start_span(
self._get_span_name(request),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,30 @@ def test_span_name_404(self):

span = span_list[0]
self.assertEqual(span.name, "HTTP GET")

def test_traced_request_attrs(self):
with patch(
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs",
[],
):
Client().get("/span_name/1234/", CONTENT_TYPE="test/ct")
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)

span = span_list[0]
self.assertNotIn("path_info", span.attributes)
self.assertNotIn("content_type", span.attributes)
self.memory_exporter.clear()

with patch(
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs",
["path_info", "content_type", "non_existing_variable"],
):
Client().get("/span_name/1234/", CONTENT_TYPE="test/ct")
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)

span = span_list[0]
self.assertEqual(span.attributes["path_info"], "/span_name/1234/")
self.assertEqual(span.attributes["content_type"], "test/ct")
self.assertNotIn("non_existing_variable", span.attributes)