Skip to content

Capture GraphQL client errors #2243

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 43 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
a66873b
Capture GraphQL client errors
sentrivana Jun 30, 2023
d1e2a7c
fix older on_request_chunk_sent
sentrivana Jul 12, 2023
9931ea8
fixes
sentrivana Jul 12, 2023
da4f138
types
sentrivana Jul 12, 2023
25af57f
2.7 fixes
sentrivana Jul 12, 2023
2577ef2
ignore 2.7 imports when checking
sentrivana Jul 12, 2023
eed47b6
change httpx query saving
sentrivana Jul 12, 2023
0ba4217
more robust qs parsing
sentrivana Jul 12, 2023
b0ae249
more aiohttp tests
sentrivana Jul 12, 2023
3a81e1c
older httpx compat
sentrivana Jul 12, 2023
4480fd6
fix regex
sentrivana Jul 12, 2023
2e66b1d
wip
sentrivana Jul 12, 2023
c910a35
fix test
sentrivana Jul 12, 2023
8de1aa7
don't pester me
sentrivana Jul 12, 2023
e18f9b5
fix 2.7 import
sentrivana Jul 13, 2023
2772305
more 2.7 import fixes
sentrivana Jul 13, 2023
5e7c99c
cleanup
sentrivana Jul 13, 2023
52b3a59
More 2.7 compat
sentrivana Jul 13, 2023
2f82654
2.7
sentrivana Jul 13, 2023
3595db4
mypy
sentrivana Jul 13, 2023
7d11f90
note
sentrivana Jul 13, 2023
364dfac
compat fixes
sentrivana Jul 13, 2023
ff61171
Merge branch 'master' into ivana/graphql
sentrivana Jul 13, 2023
2aafc36
add comment
sentrivana Jul 13, 2023
5f01782
fix
sentrivana Jul 13, 2023
6ec0770
add tests for graphql type/opname extraction
sentrivana Jul 14, 2023
511b8e6
var for better readability
sentrivana Jul 14, 2023
cf64228
add integration options
sentrivana Jul 14, 2023
898f689
fix test
sentrivana Jul 14, 2023
60c7e24
format
sentrivana Jul 14, 2023
7ffa465
add pii guards
sentrivana Jul 14, 2023
c18ee00
update graphql regex
sentrivana Jul 14, 2023
3726bc5
add test cases
sentrivana Jul 14, 2023
2ede9de
fix aiohttp and tests
sentrivana Jul 14, 2023
36c78bd
Merge branch 'master' into ivana/graphql
sentrivana Jul 14, 2023
85e155e
fix 2.7
sentrivana Jul 14, 2023
f8c1c64
add integration option tests
sentrivana Jul 17, 2023
4ba7a86
more tests
sentrivana Jul 17, 2023
9de27bb
more tests
sentrivana Jul 17, 2023
6496452
compat
sentrivana Jul 17, 2023
c58393d
compat with old pytest-httpx
sentrivana Jul 17, 2023
04d8e3f
Merge branch 'master' into ivana/graphql
antonpirker Jul 20, 2023
068fca3
Merge branch 'master' into ivana/graphql
antonpirker Jul 20, 2023
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
172 changes: 156 additions & 16 deletions sentry_sdk/integrations/aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import json
import sys
import weakref

try:
from urllib.parse import parse_qsl
except ImportError:
from urlparse import parse_qsl # type: ignore

from sentry_sdk.api import continue_trace
from sentry_sdk._compat import reraise
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.hub import Hub
from sentry_sdk.hub import Hub, _should_send_default_pii
from sentry_sdk.integrations import Integration, DidNotEnable
from sentry_sdk.integrations.logging import ignore_logger
from sentry_sdk.sessions import auto_session_tracking
Expand All @@ -29,14 +35,17 @@
CONTEXTVARS_ERROR_MESSAGE,
SENSITIVE_DATA_SUBSTITUTE,
AnnotatedValue,
SentryGraphQLClientError,
_get_graphql_operation_name,
_get_graphql_operation_type,
)

try:
import asyncio

from aiohttp import __version__ as AIOHTTP_VERSION
from aiohttp import ClientSession, TraceConfig
from aiohttp.web import Application, HTTPException, UrlDispatcher
from aiohttp import ClientSession, ContentTypeError, TraceConfig
from aiohttp.web import Application, HTTPException, UrlDispatcher, Response
except ImportError:
raise DidNotEnable("AIOHTTP not installed")

Expand All @@ -45,7 +54,11 @@
if TYPE_CHECKING:
from aiohttp.web_request import Request
from aiohttp.abc import AbstractMatchInfo
from aiohttp import TraceRequestStartParams, TraceRequestEndParams
from aiohttp import (
TraceRequestStartParams,
TraceRequestEndParams,
TraceRequestChunkSentParams,
)
from types import SimpleNamespace
from typing import Any
from typing import Dict
Expand All @@ -64,15 +77,17 @@
class AioHttpIntegration(Integration):
identifier = "aiohttp"

def __init__(self, transaction_style="handler_name"):
# type: (str) -> None
def __init__(self, transaction_style="handler_name", capture_graphql_errors=True):
# type: (str, bool) -> None
if transaction_style not in TRANSACTION_STYLE_VALUES:
raise ValueError(
"Invalid value for transaction_style: %s (must be in %s)"
% (transaction_style, TRANSACTION_STYLE_VALUES)
)
self.transaction_style = transaction_style

self.capture_graphql_errors = capture_graphql_errors

@staticmethod
def setup_once():
# type: () -> None
Expand Down Expand Up @@ -111,7 +126,7 @@ async def sentry_app_handle(self, request, *args, **kwargs):
# create a task to wrap each request.
with hub.configure_scope() as scope:
scope.clear_breadcrumbs()
scope.add_event_processor(_make_request_processor(weak_request))
scope.add_event_processor(_make_server_processor(weak_request))

transaction = continue_trace(
request.headers,
Expand Down Expand Up @@ -139,6 +154,7 @@ async def sentry_app_handle(self, request, *args, **kwargs):
reraise(*_capture_exception(hub))

transaction.set_http_status(response.status)

return response

Application._handle = sentry_app_handle
Expand Down Expand Up @@ -198,7 +214,8 @@ def create_trace_config():
async def on_request_start(session, trace_config_ctx, params):
# type: (ClientSession, SimpleNamespace, TraceRequestStartParams) -> None
hub = Hub.current
if hub.get_integration(AioHttpIntegration) is None:
integration = hub.get_integration(AioHttpIntegration)
if integration is None:
return

method = params.method.upper()
Expand Down Expand Up @@ -233,28 +250,95 @@ async def on_request_start(session, trace_config_ctx, params):
params.headers[key] = value

trace_config_ctx.span = span
trace_config_ctx.is_graphql_request = params.url.path == "/graphql"

if integration.capture_graphql_errors and trace_config_ctx.is_graphql_request:
trace_config_ctx.request_headers = params.headers

async def on_request_chunk_sent(session, trace_config_ctx, params):
# type: (ClientSession, SimpleNamespace, TraceRequestChunkSentParams) -> None
integration = Hub.current.get_integration(AioHttpIntegration)
if integration is None:
return

if integration.capture_graphql_errors and trace_config_ctx.is_graphql_request:
trace_config_ctx.request_body = None
with capture_internal_exceptions():
try:
trace_config_ctx.request_body = json.loads(params.chunk)
except json.JSONDecodeError:
return

async def on_request_end(session, trace_config_ctx, params):
# type: (ClientSession, SimpleNamespace, TraceRequestEndParams) -> None
if trace_config_ctx.span is None:
hub = Hub.current
integration = hub.get_integration(AioHttpIntegration)
if integration is None:
return

span = trace_config_ctx.span
span.set_http_status(int(params.response.status))
span.set_data("reason", params.response.reason)
span.finish()
response = params.response

if trace_config_ctx.span is not None:
span = trace_config_ctx.span
span.set_http_status(int(response.status))
span.set_data("reason", response.reason)

if (
integration.capture_graphql_errors
and trace_config_ctx.is_graphql_request
and response.method in ("GET", "POST")
and response.status == 200
):
with hub.configure_scope() as scope:
with capture_internal_exceptions():
try:
response_content = await response.json()
except ContentTypeError:
pass
else:
scope.add_event_processor(
_make_client_processor(
trace_config_ctx=trace_config_ctx,
response=response,
response_content=response_content,
)
)

if (
response_content
and isinstance(response_content, dict)
and response_content.get("errors")
):
try:
raise SentryGraphQLClientError
except SentryGraphQLClientError as ex:
event, hint = event_from_exception(
ex,
client_options=hub.client.options
if hub.client
else None,
mechanism={
"type": AioHttpIntegration.identifier,
"handled": False,
},
)
hub.capture_event(event, hint=hint)

if trace_config_ctx.span is not None:
span.finish()

trace_config = TraceConfig()

trace_config.on_request_start.append(on_request_start)
trace_config.on_request_chunk_sent.append(on_request_chunk_sent)
trace_config.on_request_end.append(on_request_end)

return trace_config


def _make_request_processor(weak_request):
def _make_server_processor(weak_request):
# type: (Callable[[], Request]) -> EventProcessor
def aiohttp_processor(
def aiohttp_server_processor(
event, # type: Dict[str, Any]
hint, # type: Dict[str, Tuple[type, BaseException, Any]]
):
Expand Down Expand Up @@ -286,7 +370,63 @@ def aiohttp_processor(

return event

return aiohttp_processor
return aiohttp_server_processor


def _make_client_processor(trace_config_ctx, response, response_content):
# type: (SimpleNamespace, Response, Optional[Dict[str, Any]]) -> EventProcessor
def aiohttp_client_processor(
event, # type: Dict[str, Any]
hint, # type: Dict[str, Tuple[type, BaseException, Any]]
):
# type: (...) -> Dict[str, Any]
with capture_internal_exceptions():
request_info = event.setdefault("request", {})

parsed_url = parse_url(str(response.url), sanitize=False)
request_info["url"] = parsed_url.url
request_info["method"] = response.method

if getattr(trace_config_ctx, "request_headers", None):
request_info["headers"] = _filter_headers(
dict(trace_config_ctx.request_headers)
)

if _should_send_default_pii():
if getattr(trace_config_ctx, "request_body", None):
request_info["data"] = trace_config_ctx.request_body

request_info["query_string"] = parsed_url.query

if response.url.path == "/graphql":
request_info["api_target"] = "graphql"

query = request_info.get("data")
if response.method == "GET":
query = dict(parse_qsl(parsed_url.query))

if query:
operation_name = _get_graphql_operation_name(query)
operation_type = _get_graphql_operation_type(query)
event["fingerprint"] = [
operation_name,
operation_type,
response.status,
]
event["exception"]["values"][0][
"value"
] = "GraphQL request failed, name: {}, type: {}".format(
operation_name, operation_type
)

if _should_send_default_pii() and response_content:
contexts = event.setdefault("contexts", {})
response_context = contexts.setdefault("response", {})
response_context["data"] = response_content

return event

return aiohttp_client_processor


def _capture_exception(hub):
Expand Down
Loading