Skip to content

Commit

Permalink
Fix relative path in db query source (#2624)
Browse files Browse the repository at this point in the history
If we send a relative path, make sure there is no leading path separator
  • Loading branch information
antonpirker authored Jan 9, 2024
1 parent 6f41823 commit b873c38
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ def add_query_source(hub, span):
except Exception:
filepath = None
if filepath is not None:
in_app_path = filepath.replace(project_root, "")
if project_root is not None and filepath.startswith(project_root):
in_app_path = filepath.replace(project_root, "").lstrip(os.sep)
else:
in_app_path = filepath
span.set_data(SPANDATA.CODE_FILEPATH, in_app_path)

try:
Expand Down
4 changes: 4 additions & 0 deletions tests/integrations/asyncpg/test_asyncpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,8 @@ async def test_query_source(sentry_init, capture_events):
assert data.get(SPANDATA.CODE_FILEPATH).endswith(
"tests/integrations/asyncpg/test_asyncpg.py"
)

is_relative_path = data.get(SPANDATA.CODE_FILEPATH)[0] != os.sep
assert is_relative_path

assert data.get(SPANDATA.CODE_FUNCTION) == "test_query_source"
5 changes: 5 additions & 0 deletions tests/integrations/django/test_db_query_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import

import os
import pytest

from django import VERSION as DJANGO_VERSION
Expand Down Expand Up @@ -109,6 +110,10 @@ def test_query_source(sentry_init, client, capture_events):
assert data.get(SPANDATA.CODE_FILEPATH).endswith(
"tests/integrations/django/myapp/views.py"
)

is_relative_path = data.get(SPANDATA.CODE_FILEPATH)[0] != os.sep
assert is_relative_path

assert data.get(SPANDATA.CODE_FUNCTION) == "postgres_select_orm"

break
Expand Down
7 changes: 6 additions & 1 deletion tests/integrations/sqlalchemy/test_sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import os
import pytest
import sys

from sqlalchemy import Column, ForeignKey, Integer, String, create_engine
from sqlalchemy.exc import IntegrityError
Expand Down Expand Up @@ -327,6 +328,10 @@ class Person(Base):
assert data.get(SPANDATA.CODE_FILEPATH).endswith(
"tests/integrations/sqlalchemy/test_sqlalchemy.py"
)

is_relative_path = data.get(SPANDATA.CODE_FILEPATH)[0] != os.sep
assert is_relative_path

assert data.get(SPANDATA.CODE_FUNCTION) == "test_query_source"
break
else:
Expand Down

0 comments on commit b873c38

Please sign in to comment.