Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sqlcommenter fields #2039

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading