Skip to content

Fix relative path in db query source #2624

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

Merged
merged 5 commits into from
Jan 9, 2024
Merged
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
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