Skip to content

Add Graphene GraphQL error integration #2389

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 15 commits into from
Oct 2, 2023
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
83 changes: 83 additions & 0 deletions .github/workflows/test-integration-graphene.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: Test graphene

on:
push:
branches:
- master
- release/**

pull_request:

# Cancel in progress workflows on pull_requests.
# https://docs.github.com/en/actions/using-jobs/using-concurrency#example-using-a-fallback-value
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

permissions:
contents: read

env:
BUILD_CACHE_KEY: ${{ github.sha }}
CACHED_BUILD_PATHS: |
${{ github.workspace }}/dist-serverless

jobs:
test:
name: graphene, python ${{ matrix.python-version }}, ${{ matrix.os }}
runs-on: ${{ matrix.os }}
timeout-minutes: 30

strategy:
fail-fast: false
matrix:
python-version: ["3.7","3.8","3.9","3.10","3.11"]
# python3.6 reached EOL and is no longer being supported on
# new versions of hosted runners on Github Actions
# ubuntu-20.04 is the last version that supported python3.6
# see https://github.com/actions/setup-python/issues/544#issuecomment-1332535877
os: [ubuntu-20.04]

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Setup Test Env
run: |
pip install coverage "tox>=3,<4"

- name: Test graphene
uses: nick-fields/retry@v2
with:
timeout_minutes: 15
max_attempts: 2
retry_wait_seconds: 5
shell: bash
command: |
set -x # print commands that are executed
coverage erase

# Run tests
./scripts/runtox.sh "py${{ matrix.python-version }}-graphene" --cov=tests --cov=sentry_sdk --cov-report= --cov-branch &&
coverage combine .coverage* &&
coverage xml -i

- uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage.xml


check_required_tests:
name: All graphene tests passed or skipped
needs: test
# Always run this, even if a dependent job failed
if: always()
runs-on: ubuntu-20.04
steps:
- name: Check for failures
if: contains(needs.test.result, 'failure')
run: |
echo "One of the dependent jobs has failed. You may need to re-run it." && exit 1
113 changes: 113 additions & 0 deletions sentry_sdk/integrations/graphene.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
from sentry_sdk.hub import Hub, _should_send_default_pii
from sentry_sdk.integrations import DidNotEnable, Integration
from sentry_sdk.integrations.modules import _get_installed_modules
from sentry_sdk.utils import (
capture_internal_exceptions,
event_from_exception,
parse_version,
)
from sentry_sdk._types import TYPE_CHECKING


try:
from graphene.types import schema as graphene_schema # type: ignore
except ImportError:
raise DidNotEnable("graphene is not installed")


if TYPE_CHECKING:
from typing import Any, Dict, Union
from graphene.language.source import Source # type: ignore
from graphql.execution import ExecutionResult # type: ignore
from graphql.type import GraphQLSchema # type: ignore


class GrapheneIntegration(Integration):
identifier = "graphene"

@staticmethod
def setup_once():
# type: () -> None
installed_packages = _get_installed_modules()
version = parse_version(installed_packages["graphene"])

if version is None:
raise DidNotEnable("Unparsable graphene version: {}".format(version))

if version < (3, 3):
raise DidNotEnable("graphene 3.3 or newer required.")

_patch_graphql()


def _patch_graphql():
# type: () -> None
old_graphql_sync = graphene_schema.graphql_sync
old_graphql_async = graphene_schema.graphql

def _sentry_patched_graphql_sync(schema, source, *args, **kwargs):
# type: (GraphQLSchema, Union[str, Source], Any, Any) -> ExecutionResult
hub = Hub.current
integration = hub.get_integration(GrapheneIntegration)
if integration is None:
return old_graphql_sync(schema, source, *args, **kwargs)

with hub.configure_scope() as scope:
scope.add_event_processor(_event_processor)

result = old_graphql_sync(schema, source, *args, **kwargs)

with capture_internal_exceptions():
for error in result.errors or []:
event, hint = event_from_exception(
error,
client_options=hub.client.options if hub.client else None,
mechanism={
"type": integration.identifier,
"handled": False,
},
)
hub.capture_event(event, hint=hint)

return result

async def _sentry_patched_graphql_async(schema, source, *args, **kwargs):
# type: (GraphQLSchema, Union[str, Source], Any, Any) -> ExecutionResult
hub = Hub.current
integration = hub.get_integration(GrapheneIntegration)
if integration is None:
return await old_graphql_async(schema, source, *args, **kwargs)

with hub.configure_scope() as scope:
scope.add_event_processor(_event_processor)

result = await old_graphql_async(schema, source, *args, **kwargs)

with capture_internal_exceptions():
for error in result.errors or []:
event, hint = event_from_exception(
error,
client_options=hub.client.options if hub.client else None,
mechanism={
"type": integration.identifier,
"handled": False,
},
)
hub.capture_event(event, hint=hint)

return result

graphene_schema.graphql_sync = _sentry_patched_graphql_sync
graphene_schema.graphql = _sentry_patched_graphql_async


def _event_processor(event, hint):
# type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]
if _should_send_default_pii():
request_info = event.setdefault("request", {})
request_info["api_target"] = "graphql"

elif event.get("request", {}).get("data"):
del event["request"]["data"]

return event
Loading