diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py index ef53d5dc38..12a90dd156 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py @@ -76,10 +76,21 @@ def __call__(self, execute: Type[T], sql, params, many, context) -> T: with_db_driver = getattr( conf.settings, "SQLCOMMENTER_WITH_DB_DRIVER", True ) + with_action = getattr( + conf.settings, "SQLCOMMENTER_WITH_ACTION", True + ) db_driver = context["connection"].settings_dict.get("ENGINE", "") resolver_match = self.request.resolver_match + # app_name is the application namespace for the URL pattern that matches the URL. + # See https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.ResolverMatch.app_name + app_name = ( + (resolver_match.app_name or None) + if resolver_match and with_app_name + else None + ) + sql = _add_sql_comment( sql, # Information about the controller. @@ -88,6 +99,7 @@ def __call__(self, execute: Type[T], sql, params, many, context) -> T: if resolver_match and with_controller else None ), + action=self.request.method if with_action else None, # route is the pattern that matched a request with a controller i.e. the regex # See https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.ResolverMatch.route # getattr() because the attribute doesn't exist in Django < 2.2. @@ -98,11 +110,8 @@ def __call__(self, execute: Type[T], sql, params, many, context) -> T: ), # app_name is the application namespace for the URL pattern that matches the URL. # See https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.ResolverMatch.app_name - app_name=( - (resolver_match.app_name or None) - if resolver_match and with_app_name - else None - ), + app_name=app_name, + application=app_name, # Framework centric information. framework=f"django:{_django_version}" if with_framework else None, # Information about the database and driver. diff --git a/instrumentation/opentelemetry-instrumentation-django/tests/test_sqlcommenter.py b/instrumentation/opentelemetry-instrumentation-django/tests/test_sqlcommenter.py index eec02d7a54..53cfb2472d 100644 --- a/instrumentation/opentelemetry-instrumentation-django/tests/test_sqlcommenter.py +++ b/instrumentation/opentelemetry-instrumentation-django/tests/test_sqlcommenter.py @@ -107,28 +107,29 @@ def test_middleware_added_at_position(self, sqlcommenter_middleware): "opentelemetry.instrumentation.django.middleware.sqlcommenter_middleware._get_opentelemetry_values" ) def test_query_wrapper(self, trace_capture): - requests_mock = MagicMock() - requests_mock.resolver_match.view_name = "view" - requests_mock.resolver_match.route = "route" - requests_mock.resolver_match.app_name = "app" + mock_request = MagicMock() + mock_request.method = "GET" + mock_request.resolver_match.view_name = "view" + mock_request.resolver_match.route = "route" + mock_request.resolver_match.app_name = "app" trace_capture.return_value = { "traceparent": "*traceparent='00-000000000000000000000000deadbeef-000000000000beef-00" } - qw_instance = _QueryWrapper(requests_mock) - execute_mock_obj = MagicMock() + qw_instance = _QueryWrapper(request=mock_request) + mock_execute = MagicMock() qw_instance( - execute_mock_obj, - "Select 1;", - MagicMock("test"), - MagicMock("test1"), - MagicMock(), + execute=mock_execute, + sql="Select 1;", + params=MagicMock("test"), + many=MagicMock("test1"), + context=MagicMock(), ) - output_sql = execute_mock_obj.call_args[0][0] + output_sql = mock_execute.call_args[0][0] self.assertEqual( output_sql, - "Select 1 /*app_name='app',controller='view',route='route',traceparent='%%2Atraceparent%%3D%%2700-0000000" - "00000000000000000deadbeef-000000000000beef-00'*/;", + "Select 1 /*action='GET',app_name='app',application='app',controller='view',route='route'," + "traceparent='%%2Atraceparent%%3D%%2700-000000000000000000000000deadbeef-000000000000beef-00'*/;", ) @patch(