Skip to content
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
31 changes: 2 additions & 29 deletions sentry_sdk/integrations/gql.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import sentry_sdk
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.utils import (
ensure_integration_enabled,
event_from_exception,
has_data_collection_enabled,
parse_version,
)

Expand Down Expand Up @@ -61,10 +59,7 @@ def _data_from_document(document: "DocumentNode") -> "EventDataType":
data: "EventDataType" = {"query": print_ast(document)}

if operation_ast is not None:
if has_data_collection_enabled(client_options):
if client_options["data_collection"]["graphql"]["variables"]:
data["variables"] = operation_ast.variable_definitions
elif should_send_default_pii():
if client_options["data_collection"]["graphql"]["variables"]:
data["variables"] = operation_ast.variable_definitions

if operation_ast.name is not None:
Expand Down Expand Up @@ -155,29 +150,7 @@ def processor(event: "Event", hint: "dict[str, Any]") -> "Event":
)

client_options = sentry_sdk.get_client().options
if has_data_collection_enabled(client_options):
if client_options["data_collection"]["graphql"]["document"]:
if GraphQLRequest is not None and isinstance(
document_or_request, GraphQLRequest
):
# In v4.0.0, gql moved to using GraphQLRequest instead of
# DocumentNode in execute
# https://github.com/graphql-python/gql/pull/556
document = document_or_request.document
else:
document = document_or_request

request["data"] = _data_from_document(document)
contexts = event.setdefault("contexts", {})
response = contexts.setdefault("response", {})
response.update(
{
"data": {"errors": errors},
"type": response,
}
)

elif should_send_default_pii():
if client_options["data_collection"]["graphql"]["document"]:
if GraphQLRequest is not None and isinstance(
document_or_request, GraphQLRequest
):
Expand Down
51 changes: 1 addition & 50 deletions tests/integrations/gql/test_gql.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _make_erroneous_query(capture_events, execute_query):
(exception,) = event["exception"]["values"]

assert exception["type"] == "TransportQueryError", (
"%s was captured, but we expected a TransportQueryError" % exception(type)
"%s was captured, but we expected a TransportQueryError" % exception["type"]
)

assert "request" in event
Expand Down Expand Up @@ -143,42 +143,10 @@ def test_real_gql_request_no_error(sentry_init, capture_events, execute_query):
)


@pytest.mark.parametrize("execute_query", _execute_query_funcs)
def test_real_gql_request_with_error_no_pii(sentry_init, capture_events, execute_query):
"""
Integration test verifying that the GQLIntegration works as expected with query resulting
in a GraphQL error, and that PII is not sent.
"""
sentry_init(integrations=[GQLIntegration()])

event = _make_erroneous_query(capture_events, execute_query)

assert "data" not in event["request"]
assert "response" not in event["contexts"]


@pytest.mark.parametrize("execute_query", _execute_query_funcs)
def test_real_gql_request_with_error_with_pii(
sentry_init, capture_events, execute_query
):
"""
Integration test verifying that the GQLIntegration works as expected with query resulting
in a GraphQL error, and that PII is not sent.
"""
sentry_init(integrations=[GQLIntegration()], send_default_pii=True)

event = _make_erroneous_query(capture_events, execute_query)

assert "data" in event["request"]
assert "response" in event["contexts"]


@pytest.mark.parametrize("execute_query", _execute_query_funcs)
@pytest.mark.parametrize(
"init_kwargs,expect_data",
[
pytest.param({}, False, id="no_pii_no_data_collection"),
pytest.param({"send_default_pii": True}, True, id="legacy_pii_on"),
pytest.param(
{"data_collection": {}},
True,
Expand All @@ -194,22 +162,6 @@ def test_real_gql_request_with_error_with_pii(
False,
id="data_collection_document_off",
),
pytest.param(
{
"send_default_pii": True,
"data_collection": {"graphql": {"document": False}},
},
False,
id="data_collection_takes_precedence_over_send_default_pii_on",
),
pytest.param(
{
"send_default_pii": False,
"data_collection": {"graphql": {"document": True}},
},
True,
id="data_collection_takes_precedence_over_send_default_pii_off",
),
],
)
def test_real_gql_request_with_error_data_collection(
Expand All @@ -236,7 +188,6 @@ def test_real_gql_request_with_error_data_collection(
@pytest.mark.parametrize(
"init_kwargs,expect_variables",
[
pytest.param({"send_default_pii": True}, True, id="legacy_pii_on"),
pytest.param(
{"data_collection": {}},
True,
Expand Down
Loading