Skip to content

Commit b4ce083

Browse files
committed
Added ability to extract span attributes from django request objects.
OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS env var can be set to a command separated list of attributes names that will be extracted from Django's request object and set as attributes on spans.
1 parent 7636547 commit b4ce083

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-django/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

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

78
## Version 0.13b0
89

Diff for: instrumentation/opentelemetry-instrumentation-django/README.rst

+13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ For example,
3030

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

33+
Request attributes
34+
********************
35+
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
36+
delimited list of request attribute names.
37+
38+
For example,
39+
40+
::
41+
42+
export OTEL_PYTHON_DJANGO_TRACED_REQUEST_ATTRS='path_info,content_type'
43+
44+
will extract path_info and content_type attributes from every traced request and add them as span attritbues.
45+
3346
References
3447
----------
3548

Diff for: instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py

+12
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ class _DjangoMiddleware(MiddlewareMixin):
5858
else:
5959
_excluded_urls = ExcludeList(_excluded_urls)
6060

61+
_traced_request_attrs = [
62+
attr.strip()
63+
for attr in (Configuration().DJANGO_TRACED_REQUEST_ATTRS or "").split(
64+
","
65+
)
66+
]
67+
6168
@staticmethod
6269
def _get_span_name(request):
6370
try:
@@ -95,6 +102,11 @@ def process_request(self, request):
95102
tracer = get_tracer(__name__, __version__)
96103

97104
attributes = collect_request_attributes(environ)
105+
for attr in self._traced_request_attrs:
106+
if hasattr(request, attr):
107+
value = str(getattr(request, attr))
108+
if value:
109+
attributes[attr] = value
98110

99111
span = tracer.start_span(
100112
self._get_span_name(request),

Diff for: instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py

+27
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,30 @@ def test_span_name_404(self):
174174

175175
span = span_list[0]
176176
self.assertEqual(span.name, "HTTP GET")
177+
178+
def test_traced_request_attrs(self):
179+
with patch(
180+
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs",
181+
[],
182+
):
183+
Client().get("/span_name/1234/", CONTENT_TYPE="test/ct")
184+
span_list = self.memory_exporter.get_finished_spans()
185+
self.assertEqual(len(span_list), 1)
186+
187+
span = span_list[0]
188+
self.assertNotIn("path_info", span.attributes)
189+
self.assertNotIn("content_type", span.attributes)
190+
self.memory_exporter.clear()
191+
192+
with patch(
193+
"opentelemetry.instrumentation.django.middleware._DjangoMiddleware._traced_request_attrs",
194+
["path_info", "content_type", "non_existing_variable"],
195+
):
196+
Client().get("/span_name/1234/", CONTENT_TYPE="test/ct")
197+
span_list = self.memory_exporter.get_finished_spans()
198+
self.assertEqual(len(span_list), 1)
199+
200+
span = span_list[0]
201+
self.assertEqual(span.attributes["path_info"], "/span_name/1234/")
202+
self.assertEqual(span.attributes["content_type"], "test/ct")
203+
self.assertNotIn("non_existing_variable", span.attributes)

0 commit comments

Comments
 (0)