From 65f82fb930c23b95051d16c13ecd369dd0023a32 Mon Sep 17 00:00:00 2001 From: Denys Halenok Date: Thu, 9 Jun 2022 14:11:37 +0200 Subject: [PATCH 1/3] Resolve view name through view_class --- elasticapm/utils/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/elasticapm/utils/__init__.py b/elasticapm/utils/__init__.py index 0f208866e..754c3e192 100644 --- a/elasticapm/utils/__init__.py +++ b/elasticapm/utils/__init__.py @@ -83,7 +83,9 @@ def get_name_from_func(func: FunctionType) -> str: module = func.__module__ - if hasattr(func, "__name__"): + if hasattr(func, "view_class"): + view_name = func.view_class.__name__ + elif hasattr(func, "__name__"): view_name = func.__name__ else: # Fall back if there's no __name__ view_name = func.__class__.__name__ From f0ecce7d636d0e18864d0b99b36698472f770030 Mon Sep 17 00:00:00 2001 From: Denys Halenok Date: Fri, 10 Jun 2022 15:31:23 +0200 Subject: [PATCH 2/3] Add a test for Django class-based view --- tests/contrib/django/django_tests.py | 9 +++++++++ tests/contrib/django/testapp/urls.py | 1 + tests/contrib/django/testapp/views.py | 6 ++++++ 3 files changed, 16 insertions(+) diff --git a/tests/contrib/django/django_tests.py b/tests/contrib/django/django_tests.py index d12b12e78..ef430fbca 100644 --- a/tests/contrib/django/django_tests.py +++ b/tests/contrib/django/django_tests.py @@ -1662,3 +1662,12 @@ def test_default_app_config_present_by_version(): assert default_app_config_is_defined else: assert not default_app_config_is_defined + + +def test_transaction_name_from_class_based_view(client, django_elasticapm_client): + with override_settings( + **middleware_setting(django.VERSION, ["elasticapm.contrib.django.middleware.TracingMiddleware"]) + ): + client.get(reverse("elasticapm-class-based")) + transaction = django_elasticapm_client.events[TRANSACTION][0] + assert transaction["name"] == "GET tests.contrib.django.testapp.views.ClassBasedView" diff --git a/tests/contrib/django/testapp/urls.py b/tests/contrib/django/testapp/urls.py index b038a7fd0..857215280 100644 --- a/tests/contrib/django/testapp/urls.py +++ b/tests/contrib/django/testapp/urls.py @@ -66,6 +66,7 @@ def handler500(request): re_path(r"^trigger-500-log-request$", views.logging_request_exc, name="elasticapm-log-request-exc"), re_path(r"^streaming$", views.streaming_view, name="elasticapm-streaming-view"), re_path(r"^name-override$", views.override_transaction_name_view, name="elasticapm-name-override"), + re_path(r"^class-based$", views.ClassBasedView.as_view(), name="elasticapm-class-based"), ) diff --git a/tests/contrib/django/testapp/views.py b/tests/contrib/django/testapp/views.py index f28da794c..5a11b0961 100644 --- a/tests/contrib/django/testapp/views.py +++ b/tests/contrib/django/testapp/views.py @@ -36,6 +36,7 @@ from django.contrib.auth.models import User from django.http import HttpResponse, StreamingHttpResponse from django.shortcuts import get_object_or_404, render +from django.views import View import elasticapm @@ -141,3 +142,8 @@ def override_transaction_name_view(request): elasticapm.set_transaction_name("foo") elasticapm.set_transaction_result("okydoky") return HttpResponse() + + +class ClassBasedView(View): + def get(self, request): + return HttpResponse() From 37a7dd2f1ca917e5da61970d7e173e01fa842f77 Mon Sep 17 00:00:00 2001 From: Benjamin Wohlwend Date: Mon, 13 Jun 2022 16:37:55 +0200 Subject: [PATCH 3/3] update changelog --- CHANGELOG.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 37e0861b0..3922770c3 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -48,7 +48,7 @@ endif::[] * Fix return for `opentelemetry.Span.is_recording()` {pull}1530[#1530] * Fix error logging for bad SERVICE_NAME config {pull}1546[#1546] * Do not instrument old versions of Tornado > 6.0 due to incompatibility {pull}1566[#1566] - +* Fix transaction names for class based views in Django 4.0+ {pull}1571[#1571] [[release-notes-6.x]] === Python Agent version 6.x