-
Notifications
You must be signed in to change notification settings - Fork 507
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
fix(query-source): Fix query source relative filepath #2717
Conversation
Heyy @gggritso 👋🏻 The change and the reasoning make sense to me. As for testing this: we usually try to make sure each change is covered by a test. In this case there are already testcases for query source in the three different integrations where we support it, so ideally this should just be a matter of taking an existing testcase, copying it and modifying it slightly to trigger behavior similar to what you're seeing in Sentry. I think these specific test cases could be repurposed easily:
You'll probably need to add some custom As for Python 2 support, at this stage we still support it 100%, so ideally after this change you'd get the same filepath regardless of Python version. Verifying this locally is a bit tricky because you need some |
Use `filename_for_module` is possible. This will create the same source paths as our stack trace handling.
These are mostly benign and show up on recent macOS versions. Turning them off so tests can succeed.
27640ef
to
d0125d6
Compare
@sentrivana thanks for the hints! I added a unit test for each of the three integrations you mentioned. It was a little complicated, so I updated the PR description to explain everything that's going on (it wasn't a As for Python 2, I'm not sure I can get it to work perfectly. More in the PR, but the behaviour there is different enough that it doesn't really work. You tell me, but since this is an improvement to a feature (Python 2 still has a good graceful fallback!) and I'm not breaking anything existing, it might be worth just merging. P.S. Do you know how long Sentry will keep supporting Python 2? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, left two nits on the asyncpg tests but they apply to all three test suites.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I'd just call the directory helpers
since it's already in an asyncpg
dir/namespace.
It'd also be cool to have a comment either here or where the function is used explaining why this exists and why it needs to be in a separate directory.
Regarding 2.7 support -- we're actually working on a new major for the SDK where we drop 2.7 support. So coming soon! The target date is end of February. |
@sentrivana I added some comments to explain why the As for the dir/namespace, I'm a little stuck there. I'd like to rename I thought about it for a while, and tried to re-do the tests so they only edit |
@gggritso I see -- I think in that case it's ok to leave the |
tl;dr
When generating the
filename
attribute for stack trace frames, the SDK uses thefilename_for_module
function. When generating thecode.filepath
attribute for query spans, the SDK does not use that function. Because of this inconsistency, code mappings that work with stack frames sometimes don't work with queries that come from the same files.This PR makes sure that query sources use
filename_for_module
, so the paths are consistent.filename_for_module
The
filename_for_module
function, roughly, does this:"sentry.api.serializers.base"
) and a full file path (e.g.,"/opt/software/sentry/src/sentry/api/serializers/base.py"
)"sentry"
)"sentry"
module insys.modules
"/opt/software/sentry/src/sentry/__init__.py"
), and strips the last two segments (in this case, `"/opt/software/sentry/src/")"sentry/api/serializers/base.py"
)This has a similar effect to stripping the
project_root
but even better.Testing
filename_for_module
filename_for_module
only starts to differ from regular behaviour if a module is manually added tosys.path
. For example, the Sentry codebase likes to import Sentry modules likefrom sentry.x import y
, rather than relative paths. This is possible becausesentry
is added to the module path. In these cases, Python will report the frame's module (frame.f_globals.get("__name__")
) assentry.x
since that's how it was imported.In order to test this behaviour, I added a small helper module to each relevant integration, and added it to
sys.path
manually, so it can be referenced without using relative paths.N.B. Python 2 has different behaviour! Even for modules that are added to
sys.path
and imported by name, it still reports the frame's module by it's full path, sofilename_for_module
can't remove the extra prefixed path segments. IMO this is fine since it degrades gracefully. The module stripping behaviour is more of an enhancement, since it's possible to work around it with code mappings.OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
Without this line, a lot of the forked tests don't run on macOS at all. Apparently this is a safe thing to ignore most of the time, but you tell me!
Product Context
We're seeing an issue in Sentry where query source file paths aren't matched to GitHub URLs because the path prefixes are different from the prefixes generated for stack traces for the same files.
e.g., in
getsentry/sentry
good path is likesentry/api/serializers/base.py
, but query sources for that file have the file pathsentry/src/sentry/api/serializers/base.py
: we're adding an extrasentry/src
prefix to the path. The existing GitHub file path mappings don't work, and there's no "Open in GitHub" link as a result.It is possible to set up more code mappings to resolve this, but I think the inconsistency is worth fixing.