Skip to content

Commit b873c38

Browse files
authored
Fix relative path in db query source (#2624)
If we send a relative path, make sure there is no leading path separator
1 parent 6f41823 commit b873c38

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,10 @@ def add_query_source(hub, span):
255255
except Exception:
256256
filepath = None
257257
if filepath is not None:
258-
in_app_path = filepath.replace(project_root, "")
258+
if project_root is not None and filepath.startswith(project_root):
259+
in_app_path = filepath.replace(project_root, "").lstrip(os.sep)
260+
else:
261+
in_app_path = filepath
259262
span.set_data(SPANDATA.CODE_FILEPATH, in_app_path)
260263

261264
try:

tests/integrations/asyncpg/test_asyncpg.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,4 +542,8 @@ async def test_query_source(sentry_init, capture_events):
542542
assert data.get(SPANDATA.CODE_FILEPATH).endswith(
543543
"tests/integrations/asyncpg/test_asyncpg.py"
544544
)
545+
546+
is_relative_path = data.get(SPANDATA.CODE_FILEPATH)[0] != os.sep
547+
assert is_relative_path
548+
545549
assert data.get(SPANDATA.CODE_FUNCTION) == "test_query_source"

tests/integrations/django/test_db_query_data.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import
22

3+
import os
34
import pytest
45

56
from django import VERSION as DJANGO_VERSION
@@ -109,6 +110,10 @@ def test_query_source(sentry_init, client, capture_events):
109110
assert data.get(SPANDATA.CODE_FILEPATH).endswith(
110111
"tests/integrations/django/myapp/views.py"
111112
)
113+
114+
is_relative_path = data.get(SPANDATA.CODE_FILEPATH)[0] != os.sep
115+
assert is_relative_path
116+
112117
assert data.get(SPANDATA.CODE_FUNCTION) == "postgres_select_orm"
113118

114119
break

tests/integrations/sqlalchemy/test_sqlalchemy.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import sys
1+
import os
22
import pytest
3+
import sys
34

45
from sqlalchemy import Column, ForeignKey, Integer, String, create_engine
56
from sqlalchemy.exc import IntegrityError
@@ -327,6 +328,10 @@ class Person(Base):
327328
assert data.get(SPANDATA.CODE_FILEPATH).endswith(
328329
"tests/integrations/sqlalchemy/test_sqlalchemy.py"
329330
)
331+
332+
is_relative_path = data.get(SPANDATA.CODE_FILEPATH)[0] != os.sep
333+
assert is_relative_path
334+
330335
assert data.get(SPANDATA.CODE_FUNCTION) == "test_query_source"
331336
break
332337
else:

0 commit comments

Comments
 (0)