python-sentry-sdk/0007-fix-tracing-Fix-add_query_source-with-modules-outsid.patch
Roman Inflianskas 304f1d0bd2
Run integration tests with a PostgreSQL server
Enabling integration tests with a PostgreSQL server led to the discovery
of the issue, which is documented at
https://github.com/getsentry/sentry-python/issues/3312.

Short version:
Previously, when packages added in `in_app_include` were installed to a
location outside of the project root directory, span from those packages
were not extended with OTel compatible source code information. Cases
include running Python from virtualenv created outside of the project
root directory or Python packages installed into the system using package
managers. This resulted in an inconsistency: spans from the same project
would be different, depending on the deployment method.

Add a patch to fix this issue as part of the update.

[skip changelog]
2024-08-20 08:59:19 +03:00

97 lines
3.6 KiB
Diff

From 7ff39563c20b83809ae2378479d0324880231b33 Mon Sep 17 00:00:00 2001
From: Roman Inflianskas <rominf@pm.me>
Date: Thu, 18 Jul 2024 16:57:21 +0300
Subject: [PATCH 2/2] fix(tracing): Fix `add_query_source` with modules outside
of project root
Fix: https://github.com/getsentry/sentry-python/issues/3312
Previously, when packages added in `in_app_include` were installed
to a location outside of the project root directory, span from
those packages were not extended with OTel compatible source code
information. Cases include running Python from virtualenv created
outside of the project root directory or Python packages installed into
the system using package managers. This resulted in an inconsistency:
spans from the same project would be different, depending on the
deployment method.
In this change, the logic was slightly changed to avoid these
discrepancies and conform to the requirements, described in the PR with
better setting of in-app in stack frames:
https://github.com/getsentry/sentry-python/pull/1894#issue-1579192436.
Note that the `_module_in_list` function returns `False` if `name` is
`None` or `items` are falsy, hence extra check before function call can
be omitted to simplify code.
---
sentry_sdk/tracing_utils.py | 22 ++++++++--------------
sentry_sdk/utils.py | 6 +++---
2 files changed, 11 insertions(+), 17 deletions(-)
diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py
index 47e14ecd..bfcf8d0a 100644
--- a/sentry_sdk/tracing_utils.py
+++ b/sentry_sdk/tracing_utils.py
@@ -22,6 +22,7 @@ from sentry_sdk.utils import (
is_sentry_url,
_is_external_source,
_module_in_list,
+ _is_in_project_root,
)
from sentry_sdk._types import TYPE_CHECKING
@@ -218,21 +219,14 @@ def add_query_source(span):
is_sentry_sdk_frame = namespace is not None and namespace.startswith(
"sentry_sdk."
)
+ should_be_included = _module_in_list(namespace, in_app_include)
+ should_be_excluded = _is_external_source(abs_path) or _module_in_list(
+ namespace, in_app_exclude
+ )
- should_be_included = not _is_external_source(abs_path)
- if namespace is not None:
- if in_app_exclude and _module_in_list(namespace, in_app_exclude):
- should_be_included = False
- if in_app_include and _module_in_list(namespace, in_app_include):
- # in_app_include takes precedence over in_app_exclude, so doing it
- # at the end
- should_be_included = True
-
- if (
- abs_path is not None
- and abs_path.startswith(project_root)
- and should_be_included
- and not is_sentry_sdk_frame
+ if not is_sentry_sdk_frame and (
+ should_be_included
+ or (_is_in_project_root(abs_path, project_root) and not should_be_excluded)
):
break
diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py
index fcbff45e..22df7aef 100644
--- a/sentry_sdk/utils.py
+++ b/sentry_sdk/utils.py
@@ -1043,7 +1043,7 @@ def event_from_exception(
def _module_in_list(name, items):
- # type: (str, Optional[List[str]]) -> bool
+ # type: (Optional[str], Optional[List[str]]) -> bool
if name is None:
return False
@@ -1070,8 +1070,8 @@ def _is_external_source(abs_path):
def _is_in_project_root(abs_path, project_root):
- # type: (str, Optional[str]) -> bool
- if project_root is None:
+ # type: (Optional[str], Optional[str]) -> bool
+ if abs_path is None or project_root is None:
return False
# check if path is in the project root
--
2.45.2