Skip to content

Commit d7f7934

Browse files
committed
feat: Support span first in Graphene
1 parent f963475 commit d7f7934

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

sentry_sdk/integrations/graphene.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from sentry_sdk.consts import OP
55
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
66
from sentry_sdk.scope import should_send_default_pii
7+
from sentry_sdk.traces import StreamedSpan
8+
from sentry_sdk.tracing_utils import has_span_streaming_enabled
79
from sentry_sdk.utils import (
810
capture_internal_exceptions,
911
ensure_integration_enabled,
@@ -120,6 +122,8 @@ def _event_processor(event: "Event", hint: "Dict[str, Any]") -> "Event":
120122
def graphql_span(
121123
schema: "GraphQLSchema", source: "Union[str, Source]", kwargs: "Dict[str, Any]"
122124
) -> "Generator[None, None, None]":
125+
client = sentry_sdk.get_client()
126+
123127
operation_name = kwargs.get("operation_name")
124128

125129
operation_type = "query"
@@ -141,15 +145,30 @@ def graphql_span(
141145
},
142146
)
143147

144-
_graphql_span = sentry_sdk.start_span(op=op, name=operation_name)
148+
if has_span_streaming_enabled(client.options):
149+
attributes = {
150+
"graphql.document": source,
151+
"graphql.operation.name": operation_name,
152+
"graphql.operation.type": operation_type,
153+
"sentry.op": op,
154+
}
155+
_graphql_span = sentry_sdk.traces.start_span(
156+
name=operation_name, attributes=attributes
157+
)
158+
159+
else:
160+
_graphql_span = sentry_sdk.start_span(op=op, name=operation_name)
145161

146-
_graphql_span.set_data("graphql.document", source)
147-
_graphql_span.set_data("graphql.operation.name", operation_name)
148-
_graphql_span.set_data("graphql.operation.type", operation_type)
162+
_graphql_span.set_data("graphql.document", source)
163+
_graphql_span.set_data("graphql.operation.name", operation_name)
164+
_graphql_span.set_data("graphql.operation.type", operation_type)
149165

150-
_graphql_span.__enter__()
166+
_graphql_span.__enter__()
151167

152168
try:
153169
yield
154170
finally:
155-
_graphql_span.__exit__(None, None, None)
171+
if isinstance(_graphql_span, StreamedSpan):
172+
_graphql_span.end()
173+
else:
174+
_graphql_span.__exit__(None, None, None)

0 commit comments

Comments
 (0)