Skip to content

Commit 6d2c89a

Browse files
chore(service.naming): schematize django db calls [backport 1.16] (#6358)
Backport 2dbe9cf from #6335 to 1.16. Django has a separate mechanism for determining service name when using an underlying database. This change adds the schematized service.naming to those Django DB calls. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) Co-authored-by: Teague Bick <[email protected]>
1 parent 3c79a02 commit 6d2c89a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

ddtrace/contrib/django/patch.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def cursor(django, pin, func, instance, args, kwargs):
106106
else:
107107
database_prefix = config.django.database_service_name_prefix
108108
service = "{}{}{}".format(database_prefix, alias, "db")
109+
service = schematize_service_name(service)
109110

110111
vendor = getattr(conn, "vendor", "db")
111112
prefix = sqlx.normalize_vendor(vendor)

tests/contrib/django/test_django.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,60 @@ def test(client, test_spans):
15301530
assert status == 0, (out, err)
15311531

15321532

1533+
@pytest.mark.parametrize("global_service_name", [None, "mysvc"])
1534+
@pytest.mark.parametrize("schema_version", [None, "v0", "v1"])
1535+
def test_schematized_default_db_service_name(
1536+
ddtrace_run_python_code_in_subprocess, schema_version, global_service_name
1537+
):
1538+
expected_service_name = {
1539+
None: "defaultdb",
1540+
"v0": "defaultdb",
1541+
"v1": global_service_name or _DEFAULT_SPAN_SERVICE_NAMES["v1"],
1542+
}[schema_version]
1543+
code = """
1544+
import pytest
1545+
import sys
1546+
1547+
import django
1548+
1549+
from tests.contrib.django.conftest import *
1550+
from tests.utils import override_config
1551+
1552+
@pytest.mark.django_db
1553+
def test_connection(client, test_spans):
1554+
from django.contrib.auth.models import User
1555+
1556+
users = User.objects.count()
1557+
assert users == 0
1558+
1559+
test_spans.assert_span_count(1)
1560+
spans = test_spans.get_spans()
1561+
1562+
span = spans[0]
1563+
assert span.name == "sqlite.query"
1564+
assert span.service == "{}"
1565+
assert span.span_type == "sql"
1566+
assert span.get_tag("django.db.vendor") == "sqlite"
1567+
assert span.get_tag("django.db.alias") == "default"
1568+
1569+
if __name__ == "__main__":
1570+
sys.exit(pytest.main(["-x", __file__]))
1571+
""".format(
1572+
expected_service_name
1573+
)
1574+
1575+
env = os.environ.copy()
1576+
if schema_version is not None:
1577+
env["DD_TRACE_SPAN_ATTRIBUTE_SCHEMA"] = schema_version
1578+
if global_service_name is not None:
1579+
env["DD_SERVICE"] = global_service_name
1580+
out, err, status, _ = ddtrace_run_python_code_in_subprocess(
1581+
code,
1582+
env=env,
1583+
)
1584+
assert status == 0, (out, err)
1585+
1586+
15331587
@pytest.mark.parametrize("schema_version", [None, "v0", "v1"])
15341588
def test_schematized_operation_name(ddtrace_run_python_code_in_subprocess, schema_version):
15351589
expected_operation_name = {None: "django.request", "v0": "django.request", "v1": "http.server.request"}[

0 commit comments

Comments
 (0)