From 12da4f6388d1142d6569c89a55d36bb671624f81 Mon Sep 17 00:00:00 2001 From: stschenk Date: Thu, 8 Oct 2020 15:15:40 -0700 Subject: [PATCH 01/12] Add capture of http.route to DjangoInstrumentor middleware --- .../opentelemetry/instrumentation/django/middleware.py | 9 +++++++++ .../tests/test_middleware.py | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py index 11991413eb..191b6b4067 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py @@ -87,6 +87,13 @@ def _get_span_name(request): except Resolver404: return "HTTP {}".format(request.method) + + @staticmethod + def _set_http_route(span, request): + # Use the resolved fuction path for the http.route + # Note: resolved_match is not available during the process_request phase. + if request.resolver_match is not None: + span.set_attribute("http.route", request.resolver_match._func_path) def process_request(self, request): # request.META is a dictionary containing all available HTTP headers @@ -133,6 +140,7 @@ def process_exception(self, request, exception): return if self._environ_activation_key in request.META.keys(): + self._set_http_route(request.META[self._environ_span_key], request) request.META[self._environ_activation_key].__exit__( type(exception), exception, @@ -151,6 +159,7 @@ def process_response(self, request, response): self._environ_activation_key in request.META.keys() and self._environ_span_key in request.META.keys() ): + self._set_http_route(request.META[self._environ_span_key], request) add_response_attributes( request.META[self._environ_span_key], "{} {}".format(response.status_code, response.reason_phrase), diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index 4db6c485de..8ded12cfbe 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -85,6 +85,9 @@ def test_traced_get(self): self.assertEqual( span.attributes["http.url"], "http://testserver/traced/" ) + self.assertEqual( + span.attributes["http.route"], "tests.views.traced" + ) self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK") @@ -121,6 +124,9 @@ def test_traced_post(self): self.assertEqual( span.attributes["http.url"], "http://testserver/traced/" ) + self.assertEqual( + span.attributes["http.route"], "tests.views.traced" + ) self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK") @@ -145,6 +151,9 @@ def test_error(self): self.assertEqual( span.attributes["http.url"], "http://testserver/error/" ) + self.assertEqual( + span.attributes["http.route"], "tests.views.error" + ) self.assertEqual(span.attributes["http.scheme"], "http") @patch( From 7362f73df06a0070cf9e5700a415aac7ae06c8fc Mon Sep 17 00:00:00 2001 From: stschenk Date: Thu, 8 Oct 2020 15:29:37 -0700 Subject: [PATCH 02/12] Update changelog --- .../opentelemetry-instrumentation-django/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/instrumentation/opentelemetry-instrumentation-django/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-django/CHANGELOG.md index 30b13c4a22..0a4ed89739 100644 --- a/instrumentation/opentelemetry-instrumentation-django/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-django/CHANGELOG.md @@ -4,6 +4,7 @@ - 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)) +- Added capture of http.route ([#1213](https://github.com/open-telemetry/opentelemetry-python/issues/1213)) ## Version 0.13b0 From e3979de97cfbf195855bd278a2cda050b20c0244 Mon Sep 17 00:00:00 2001 From: stschenk Date: Thu, 8 Oct 2020 15:37:37 -0700 Subject: [PATCH 03/12] Fix lint error --- .../instrumentation/django/middleware.py | 2 +- .../tests/test_middleware.py | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py index 191b6b4067..6c9c9b403b 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py @@ -87,7 +87,7 @@ def _get_span_name(request): except Resolver404: return "HTTP {}".format(request.method) - + @staticmethod def _set_http_route(span, request): # Use the resolved fuction path for the http.route diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index 8ded12cfbe..c3558cfce2 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -85,9 +85,7 @@ def test_traced_get(self): self.assertEqual( span.attributes["http.url"], "http://testserver/traced/" ) - self.assertEqual( - span.attributes["http.route"], "tests.views.traced" - ) + self.assertEqual(span.attributes["http.route"], "tests.views.traced") self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK") @@ -124,9 +122,7 @@ def test_traced_post(self): self.assertEqual( span.attributes["http.url"], "http://testserver/traced/" ) - self.assertEqual( - span.attributes["http.route"], "tests.views.traced" - ) + self.assertEqual(span.attributes["http.route"], "tests.views.traced") self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK") @@ -151,9 +147,7 @@ def test_error(self): self.assertEqual( span.attributes["http.url"], "http://testserver/error/" ) - self.assertEqual( - span.attributes["http.route"], "tests.views.error" - ) + self.assertEqual(span.attributes["http.route"], "tests.views.error") self.assertEqual(span.attributes["http.scheme"], "http") @patch( From f070259fcd720f77dcba392f632abb5bc7f153e0 Mon Sep 17 00:00:00 2001 From: stschenk Date: Thu, 8 Oct 2020 15:49:59 -0700 Subject: [PATCH 04/12] Switch from using hidden _func_path property to calculating the function path --- .../instrumentation/django/middleware.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py index 6c9c9b403b..c3d62ee73b 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py @@ -93,7 +93,17 @@ def _set_http_route(span, request): # Use the resolved fuction path for the http.route # Note: resolved_match is not available during the process_request phase. if request.resolver_match is not None: - span.set_attribute("http.route", request.resolver_match._func_path) + func = request.resolver_match.func + func_path = None + if not hasattr(func, "__name__"): + # A class-based view + func_path = ( + func.__class__.__module__ + "." + func.__class__.__name__ + ) + else: + # A function-based view + func_path = func.__module__ + "." + func.__name__ + span.set_attribute("http.route", func_path) def process_request(self, request): # request.META is a dictionary containing all available HTTP headers From 9190a7db36b225a9c5ffcb8b85d5388aacf56089 Mon Sep 17 00:00:00 2001 From: stschenk <72463443+stschenk@users.noreply.github.com> Date: Mon, 12 Oct 2020 08:58:36 -0700 Subject: [PATCH 05/12] Update instrumentation/opentelemetry-instrumentation-django/CHANGELOG.md Co-authored-by: Leighton Chen --- .../opentelemetry-instrumentation-django/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-django/CHANGELOG.md b/instrumentation/opentelemetry-instrumentation-django/CHANGELOG.md index 0a4ed89739..bd6bf8d237 100644 --- a/instrumentation/opentelemetry-instrumentation-django/CHANGELOG.md +++ b/instrumentation/opentelemetry-instrumentation-django/CHANGELOG.md @@ -4,7 +4,7 @@ - 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)) -- Added capture of http.route ([#1213](https://github.com/open-telemetry/opentelemetry-python/issues/1213)) +- Added capture of http.route ([#1226](https://github.com/open-telemetry/opentelemetry-python/issues/1226)) ## Version 0.13b0 From 5f7927b7ee8705594c2eba217e4d8e156902bbe0 Mon Sep 17 00:00:00 2001 From: stschenk Date: Mon, 12 Oct 2020 09:09:33 -0700 Subject: [PATCH 06/12] Switch to using request.resolver_match.route for http.route --- .../instrumentation/django/middleware.py | 12 +----------- .../tests/test_middleware.py | 6 +++--- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py index c3d62ee73b..5a3522d569 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py @@ -93,17 +93,7 @@ def _set_http_route(span, request): # Use the resolved fuction path for the http.route # Note: resolved_match is not available during the process_request phase. if request.resolver_match is not None: - func = request.resolver_match.func - func_path = None - if not hasattr(func, "__name__"): - # A class-based view - func_path = ( - func.__class__.__module__ + "." + func.__class__.__name__ - ) - else: - # A function-based view - func_path = func.__module__ + "." + func.__name__ - span.set_attribute("http.route", func_path) + span.set_attribute("http.route", request.resolver_match.route) def process_request(self, request): # request.META is a dictionary containing all available HTTP headers diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index c3558cfce2..a337c2d9d6 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -85,7 +85,7 @@ def test_traced_get(self): self.assertEqual( span.attributes["http.url"], "http://testserver/traced/" ) - self.assertEqual(span.attributes["http.route"], "tests.views.traced") + self.assertEqual(span.attributes["http.route"], "^traced/") self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK") @@ -122,7 +122,7 @@ def test_traced_post(self): self.assertEqual( span.attributes["http.url"], "http://testserver/traced/" ) - self.assertEqual(span.attributes["http.route"], "tests.views.traced") + self.assertEqual(span.attributes["http.route"], "^traced/") self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK") @@ -147,7 +147,7 @@ def test_error(self): self.assertEqual( span.attributes["http.url"], "http://testserver/error/" ) - self.assertEqual(span.attributes["http.route"], "tests.views.error") + self.assertEqual(span.attributes["http.route"], "^error/") self.assertEqual(span.attributes["http.scheme"], "http") @patch( From d919de2f2cc0776768fc4295733682610574b6ef Mon Sep 17 00:00:00 2001 From: stschenk Date: Mon, 12 Oct 2020 15:20:42 -0700 Subject: [PATCH 07/12] Switch to using request.path for http.route --- .../opentelemetry/instrumentation/django/middleware.py | 10 +--------- .../tests/test_middleware.py | 6 +++--- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py index 5a3522d569..065a04466d 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py @@ -88,13 +88,6 @@ def _get_span_name(request): except Resolver404: return "HTTP {}".format(request.method) - @staticmethod - def _set_http_route(span, request): - # Use the resolved fuction path for the http.route - # Note: resolved_match is not available during the process_request phase. - if request.resolver_match is not None: - span.set_attribute("http.route", request.resolver_match.route) - def process_request(self, request): # request.META is a dictionary containing all available HTTP headers # Read more about request.META here: @@ -124,6 +117,7 @@ def process_request(self, request): ) for key, value in attributes.items(): span.set_attribute(key, value) + span.set_attribute("http.route", request.path) activation = tracer.use_span(span, end_on_exit=True) activation.__enter__() @@ -140,7 +134,6 @@ def process_exception(self, request, exception): return if self._environ_activation_key in request.META.keys(): - self._set_http_route(request.META[self._environ_span_key], request) request.META[self._environ_activation_key].__exit__( type(exception), exception, @@ -159,7 +152,6 @@ def process_response(self, request, response): self._environ_activation_key in request.META.keys() and self._environ_span_key in request.META.keys() ): - self._set_http_route(request.META[self._environ_span_key], request) add_response_attributes( request.META[self._environ_span_key], "{} {}".format(response.status_code, response.reason_phrase), diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index a337c2d9d6..9eb58585d5 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -85,7 +85,7 @@ def test_traced_get(self): self.assertEqual( span.attributes["http.url"], "http://testserver/traced/" ) - self.assertEqual(span.attributes["http.route"], "^traced/") + self.assertEqual(span.attributes["http.route"], "/traced/") self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK") @@ -122,7 +122,7 @@ def test_traced_post(self): self.assertEqual( span.attributes["http.url"], "http://testserver/traced/" ) - self.assertEqual(span.attributes["http.route"], "^traced/") + self.assertEqual(span.attributes["http.route"], "/traced/") self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK") @@ -147,7 +147,7 @@ def test_error(self): self.assertEqual( span.attributes["http.url"], "http://testserver/error/" ) - self.assertEqual(span.attributes["http.route"], "^error/") + self.assertEqual(span.attributes["http.route"], "/error/") self.assertEqual(span.attributes["http.scheme"], "http") @patch( From 3bd216d8cfd984548605660020d3320ebab45e63 Mon Sep 17 00:00:00 2001 From: stschenk Date: Tue, 13 Oct 2020 08:46:50 -0700 Subject: [PATCH 08/12] capture request.resolver_match.route as http.route --- .../instrumentation/django/middleware.py | 22 ++++++++++++++++++- .../tests/test_middleware.py | 6 ++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py index 065a04466d..6178deea7f 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py @@ -117,7 +117,7 @@ def process_request(self, request): ) for key, value in attributes.items(): span.set_attribute(key, value) - span.set_attribute("http.route", request.path) + span.set_attribute("http.path", request.path) activation = tracer.use_span(span, end_on_exit=True) activation.__enter__() @@ -126,6 +126,26 @@ def process_request(self, request): request.META[self._environ_span_key] = span request.META[self._environ_token] = token + def process_view(self, request, view_func, *args, **kwargs): + # Process view is executed before the view function, here we get the + # route template from request.resolver_match. It is not set yet in process_request + if self._excluded_urls.url_disabled(request.build_absolute_uri("?")): + return + + if ( + self._environ_activation_key in request.META.keys() + and self._environ_span_key in request.META.keys() + ): + span = request.META[self._environ_span_key] + + if span.is_recording(): + if getattr(request, "resolver_match") and getattr( + request.resolver_match, "route" + ): + span.set_attribute( + "http.route", request.resolver_match.route + ) + def process_exception(self, request, exception): # Django can call this method and process_response later. In order # to avoid __exit__ and detach from being called twice then, the diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index 9eb58585d5..a337c2d9d6 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -85,7 +85,7 @@ def test_traced_get(self): self.assertEqual( span.attributes["http.url"], "http://testserver/traced/" ) - self.assertEqual(span.attributes["http.route"], "/traced/") + self.assertEqual(span.attributes["http.route"], "^traced/") self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK") @@ -122,7 +122,7 @@ def test_traced_post(self): self.assertEqual( span.attributes["http.url"], "http://testserver/traced/" ) - self.assertEqual(span.attributes["http.route"], "/traced/") + self.assertEqual(span.attributes["http.route"], "^traced/") self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK") @@ -147,7 +147,7 @@ def test_error(self): self.assertEqual( span.attributes["http.url"], "http://testserver/error/" ) - self.assertEqual(span.attributes["http.route"], "/error/") + self.assertEqual(span.attributes["http.route"], "^error/") self.assertEqual(span.attributes["http.scheme"], "http") @patch( From a5889bbaacb8db697f6a16b350e2189b6e721e38 Mon Sep 17 00:00:00 2001 From: stschenk Date: Tue, 13 Oct 2020 13:29:22 -0700 Subject: [PATCH 09/12] add test for templated route --- .../instrumentation/django/middleware.py | 12 +++---- .../tests/test_middleware.py | 32 +++++++++++++++++++ .../tests/views.py | 4 +++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py index 6178deea7f..8c13182056 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py @@ -126,6 +126,7 @@ def process_request(self, request): request.META[self._environ_span_key] = span request.META[self._environ_token] = token + # pylint: disable=unused-argument def process_view(self, request, view_func, *args, **kwargs): # Process view is executed before the view function, here we get the # route template from request.resolver_match. It is not set yet in process_request @@ -139,12 +140,11 @@ def process_view(self, request, view_func, *args, **kwargs): span = request.META[self._environ_span_key] if span.is_recording(): - if getattr(request, "resolver_match") and getattr( - request.resolver_match, "route" - ): - span.set_attribute( - "http.route", request.resolver_match.route - ) + match = getattr(request, "resolver_match") + if match: + route = getattr(match, "route") + if route: + span.set_attribute("http.route", route) def process_exception(self, request, exception): # Django can call this method and process_response later. In order diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index a337c2d9d6..31fc5188d8 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -36,12 +36,14 @@ excluded_noarg2, route_span_name, traced, + traced_template, ) DJANGO_2_2 = VERSION >= (2, 2) urlpatterns = [ url(r"^traced/", traced), + url(r"^route/(?P[0-9]{4})/template/$", traced_template), url(r"^error/", error), url(r"^excluded_arg/", excluded), url(r"^excluded_noarg/", excluded_noarg), @@ -68,6 +70,36 @@ def tearDown(self): teardown_test_environment() _django_instrumentor.uninstrument() + def test_templated_route_get(self): + Client().get("/route/2020/template/") + + spans = self.memory_exporter.get_finished_spans() + self.assertEqual(len(spans), 1) + + span = spans[0] + + self.assertEqual( + span.name, + "^route/(?P[0-9]{4})/template/$" + if DJANGO_2_2 + else "tests.views.traced", + ) + self.assertEqual(span.kind, SpanKind.SERVER) + self.assertEqual(span.status.canonical_code, StatusCanonicalCode.OK) + self.assertEqual(span.attributes["http.method"], "GET") + self.assertEqual( + span.attributes["http.url"], + "http://testserver/route/2020/template/", + ) + self.assertEqual( + span.attributes["http.route"], + "^route/(?P[0-9]{4})/template/$", + ) + self.assertEqual(span.attributes["http.path"], "/route/2020/template/") + self.assertEqual(span.attributes["http.scheme"], "http") + self.assertEqual(span.attributes["http.status_code"], 200) + self.assertEqual(span.attributes["http.status_text"], "OK") + def test_traced_get(self): Client().get("/traced/") diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/views.py b/instrumentation/opentelemetry-instrumentation-django/tests/views.py index e286841011..872222a842 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/views.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/views.py @@ -5,6 +5,10 @@ def traced(request): # pylint: disable=unused-argument return HttpResponse() +def traced_template(request, year): # pylint: disable=unused-argument + return HttpResponse() + + def error(request): # pylint: disable=unused-argument raise ValueError("error") From aa4e35bab26ea83738998b40236924063ee1ba5d Mon Sep 17 00:00:00 2001 From: stschenk Date: Tue, 13 Oct 2020 16:06:55 -0700 Subject: [PATCH 10/12] switch http.path to http.target --- .../src/opentelemetry/instrumentation/django/middleware.py | 2 +- .../tests/test_middleware.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py index 8c13182056..c28b690282 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py @@ -117,7 +117,7 @@ def process_request(self, request): ) for key, value in attributes.items(): span.set_attribute(key, value) - span.set_attribute("http.path", request.path) + span.set_attribute("http.target", request.path) activation = tracer.use_span(span, end_on_exit=True) activation.__enter__() diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index 31fc5188d8..0fa2e2a924 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -95,7 +95,7 @@ def test_templated_route_get(self): span.attributes["http.route"], "^route/(?P[0-9]{4})/template/$", ) - self.assertEqual(span.attributes["http.path"], "/route/2020/template/") + self.assertEqual(span.attributes["http.target"], "/route/2020/template/") self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK") From afadb6675f803d5c762b1bc5f3109b437f10df74 Mon Sep 17 00:00:00 2001 From: stschenk Date: Tue, 13 Oct 2020 16:11:22 -0700 Subject: [PATCH 11/12] fix lint issue --- .../tests/test_middleware.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index 0fa2e2a924..2291a1c80a 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -95,7 +95,9 @@ def test_templated_route_get(self): span.attributes["http.route"], "^route/(?P[0-9]{4})/template/$", ) - self.assertEqual(span.attributes["http.target"], "/route/2020/template/") + self.assertEqual( + span.attributes["http.target"], "/route/2020/template/" + ) self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK") From 73482409e318693dc365552f5fb661126c6cca73 Mon Sep 17 00:00:00 2001 From: stschenk Date: Wed, 14 Oct 2020 08:56:40 -0700 Subject: [PATCH 12/12] remove setting of http.target, it is handled elsewhere --- .../src/opentelemetry/instrumentation/django/middleware.py | 1 - .../tests/test_middleware.py | 3 --- 2 files changed, 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py index c28b690282..e3cb78dbd8 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py @@ -117,7 +117,6 @@ def process_request(self, request): ) for key, value in attributes.items(): span.set_attribute(key, value) - span.set_attribute("http.target", request.path) activation = tracer.use_span(span, end_on_exit=True) activation.__enter__() diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py index 2291a1c80a..6e3196e3ef 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py @@ -95,9 +95,6 @@ def test_templated_route_get(self): span.attributes["http.route"], "^route/(?P[0-9]{4})/template/$", ) - self.assertEqual( - span.attributes["http.target"], "/route/2020/template/" - ) self.assertEqual(span.attributes["http.scheme"], "http") self.assertEqual(span.attributes["http.status_code"], 200) self.assertEqual(span.attributes["http.status_text"], "OK")