Skip to content
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

Add Strawberry GraphQL integration #2393

Merged
merged 44 commits into from
Oct 10, 2023
Merged

Conversation

sentrivana
Copy link
Contributor

@sentrivana sentrivana commented Sep 27, 2023

Capture GraphQL errors and spans when using Strawberry server side.

This is a port of the existing Sentry tracing extension in Strawberry with added error monitoring.

The integration has an option called async_execution which controls whether to hook into Strawberry sync or async. If not provided, we try to guess based on whether an async web framework is installed.

Example event in Sentry: https://sentry-sdks.sentry.io/issues/4507792283/events/c517f630fef54a378affa9602ccf3ff8/

Strawberry is one of three integrations for #2257, split from #2381.

@sentrivana
Copy link
Contributor Author

sentrivana commented Sep 27, 2023

Hey @patrick91, here's a shot at porting the 🍓 Sentry tracing extension to the Sentry SDK and adding error monitoring. Any feedback is welcome!

Some notes:

  • The integration, if enabled, will automatically remove the existing Strawberry Sentry tracing extension, if present, to avoid conflicts.
  • The error monitoring will only work if folks don't override the default process_result. This is not optimal but it was the most straightforward place to hook into.
  • I've made some changes to the tracing extension, mostly renaming things (e.g. span ops) to comply with our and/or OTel conventions. Also changed all span.set_tags to span.set_data since the SDK ideally shouldn't be setting span tags.

@patrick91
Copy link

we spoke on discord about this, I'm very excited to see this landing! From our side we'll add some code to make it easier to hook into the process result code (so users can still override it)

I'll also make sure to have an integration test on our side once this lands, so we don't break the integration in future ☺️

@patrick91
Copy link

Made the PR here: strawberry-graphql/strawberry#3127 @sentrivana let me know if that looks ok to you!

@sentrivana sentrivana marked this pull request as ready for review October 3, 2023 11:01
@sentrivana
Copy link
Contributor Author

Thanks @patrick91 for being awesome and helping us integrate with Strawberry better! This PR now utilizes a new hook released with Strawberry 0.209.5.

Copy link
Member

@szokeasaurusrex szokeasaurusrex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have suggested a few changes. But, generally looks good!

sentry_sdk/consts.py Show resolved Hide resolved
sentry_sdk/integrations/strawberry.py Show resolved Hide resolved
sentry_sdk/integrations/strawberry.py Outdated Show resolved Hide resolved
sentry_sdk/integrations/strawberry.py Outdated Show resolved Hide resolved
Comment on lines 369 to 370
elif event.get("request", {}).get("data"):
del event["request"]["data"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could wrap it in a try/except block like so? Your choice, I think either way is good

Suggested change
elif event.get("request", {}).get("data"):
del event["request"]["data"]
else:
try:
del event["request"]["data"]
except KeyError:
pass

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that PEP8 prefers what you suggested here but from a personal taste point of view I prefer the original because I find it more concise and there is less visual clutter. So if you're fine with it I'd keep the original.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made one slight change to it though: 7831520 So that, if for some reason event["request"] is there and is None, we don't get an AttributeError.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have thought about this a bit more, and I think I would really prefer to switch to the try/except syntax, since I think it is much more readable for people looking at this code for the first time.

With the try/except block, the code reads in plain English as "If we want to send PII, send it, otherwise ensure we don't send any request data by deleting it if it's there." It is immediately obvious what the code does.

With the elif statement, the code reads in plain English as "If we want to send PII, send it, otherwise check whether the event has a request (treat the request as an empty dictionary if it doesn't) and check whether the event's request has any data, and if it does, delete the event request data." In this situation, the code's objective is less obvious; only after thoroughly reading the contents of the elif condition and the statement it contains is it clear that the code here is designed to ensure the request has no data when we send the event.

Although the elif version is more concise, this concision comes at the expense of readability and understandability, so I think the try/except version would be preferable here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, changed: 2ccd1eb
(The TypeError is there for the event["request"] == None case -- it'd throw that instead of the AttributeError I mentioned above.)

sentry_sdk/integrations/strawberry.py Outdated Show resolved Hide resolved
tests/integrations/strawberry/test_strawberry_py3.py Outdated Show resolved Hide resolved
tests/integrations/strawberry/test_strawberry_py3.py Outdated Show resolved Hide resolved
tests/integrations/strawberry/test_strawberry_py3.py Outdated Show resolved Hide resolved
sentrivana and others added 3 commits October 10, 2023 09:43
Co-authored-by: Daniel Szoke <szokeasaurusrex@users.noreply.github.com>
Co-authored-by: Daniel Szoke <szokeasaurusrex@users.noreply.github.com>
Co-authored-by: Daniel Szoke <szokeasaurusrex@users.noreply.github.com>
@sentrivana
Copy link
Contributor Author

Hope I've addressed everything @szokeasaurusrex -- please have another look when you have time.

Copy link
Member

@szokeasaurusrex szokeasaurusrex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added one comment to your tests, also please address my reply to our try/except thread. Then, the code looks good to me! @antonpirker should review before merge to double-check that I didn't miss anything

tests/integrations/strawberry/test_strawberry_py3.py Outdated Show resolved Hide resolved
Copy link
Member

@antonpirker antonpirker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 🚀

@sentrivana sentrivana merged commit b873a31 into master Oct 10, 2023
284 checks passed
@sentrivana sentrivana deleted the ivana/strawberry-integration branch October 10, 2023 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants