Skip to content

Commit 313f044

Browse files
committed
Add sqlcommenter fields
1 parent eb6024c commit 313f044

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,32 @@ def __call__(self, execute: Type[T], sql, params, many, context) -> T:
7878
with_db_driver = getattr(
7979
conf.settings, "SQLCOMMENTER_WITH_DB_DRIVER", True
8080
)
81+
with_action = getattr(
82+
conf.settings, "SQLCOMMENTER_WITH_ACTION", True
83+
)
8184

8285
db_driver = context["connection"].settings_dict.get("ENGINE", "")
8386
resolver_match = self.request.resolver_match
8487

88+
# app_name is the application namespace for the URL pattern that matches the URL.
89+
# See https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.ResolverMatch.app_name
90+
app_name = (resolver_match.app_name or None) if resolver_match and with_app_name else None
91+
8592
sql = _add_sql_comment(
8693
sql,
8794
# Information about the controller.
8895
controller=resolver_match.view_name
8996
if resolver_match and with_controller
9097
else None,
98+
action=self.request.method if with_action else None,
9199
# route is the pattern that matched a request with a controller i.e. the regex
92100
# See https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.ResolverMatch.route
93101
# getattr() because the attribute doesn't exist in Django < 2.2.
94102
route=getattr(resolver_match, "route", None)
95103
if resolver_match and with_route
96104
else None,
97-
# app_name is the application namespace for the URL pattern that matches the URL.
98-
# See https://docs.djangoproject.com/en/stable/ref/urlresolvers/#django.urls.ResolverMatch.app_name
99-
app_name=(resolver_match.app_name or None)
100-
if resolver_match and with_app_name
101-
else None,
105+
app_name=app_name,
106+
application=app_name,
102107
# Framework centric information.
103108
framework=f"django:{_django_version}" if with_framework else None,
104109
# Information about the database and driver.

instrumentation/opentelemetry-instrumentation-django/tests/test_sqlcommenter.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,29 @@ def test_middleware_added(self, sqlcommenter_middleware):
7676
"opentelemetry.instrumentation.django.middleware.sqlcommenter_middleware._get_opentelemetry_values"
7777
)
7878
def test_query_wrapper(self, trace_capture):
79-
requests_mock = MagicMock()
80-
requests_mock.resolver_match.view_name = "view"
81-
requests_mock.resolver_match.route = "route"
82-
requests_mock.resolver_match.app_name = "app"
79+
mock_request = MagicMock()
80+
mock_request.method = "GET"
81+
mock_request.resolver_match.view_name = "view"
82+
mock_request.resolver_match.route = "route"
83+
mock_request.resolver_match.app_name = "app"
8384

8485
trace_capture.return_value = {
8586
"traceparent": "*traceparent='00-000000000000000000000000deadbeef-000000000000beef-00"
8687
}
87-
qw_instance = _QueryWrapper(requests_mock)
88-
execute_mock_obj = MagicMock()
88+
qw_instance = _QueryWrapper(request=mock_request)
89+
mock_execute = MagicMock()
8990
qw_instance(
90-
execute_mock_obj,
91-
"Select 1;",
92-
MagicMock("test"),
93-
MagicMock("test1"),
94-
MagicMock(),
91+
execute=mock_execute,
92+
sql="Select 1;",
93+
params=MagicMock("test"),
94+
many=MagicMock("test1"),
95+
context=MagicMock(),
9596
)
96-
output_sql = execute_mock_obj.call_args[0][0]
97+
output_sql = mock_execute.call_args[0][0]
9798
self.assertEqual(
9899
output_sql,
99-
"Select 1 /*app_name='app',controller='view',route='route',traceparent='%%2Atraceparent%%3D%%2700-0000000"
100-
"00000000000000000deadbeef-000000000000beef-00'*/;",
100+
"Select 1 /*action='GET',app_name='app',application='app',controller='view',route='route',"
101+
"traceparent='%%2Atraceparent%%3D%%2700-000000000000000000000000deadbeef-000000000000beef-00'*/;",
101102
)
102103

103104
@patch(

0 commit comments

Comments
 (0)