@@ -179,59 +179,50 @@ def on_operation(self):
179179 },
180180 )
181181
182- span = sentry_sdk .get_current_span ()
183- if span :
184- self .graphql_span = span .start_child (
185- op = op ,
186- name = description ,
187- origin = StrawberryIntegration .origin ,
188- )
189- else :
190- self .graphql_span = sentry_sdk .start_span (
191- op = op ,
192- name = description ,
193- origin = StrawberryIntegration .origin ,
194- only_if_parent = True ,
195- )
182+ with sentry_sdk .start_span (
183+ op = op ,
184+ name = description ,
185+ origin = StrawberryIntegration .origin ,
186+ only_if_parent = True ,
187+ ) as graphql_span :
188+ graphql_span .set_data ("graphql.operation.type" , operation_type )
189+ graphql_span .set_data ("graphql.document" , self .execution_context .query )
190+ graphql_span .set_data ("graphql.resource_name" , self ._resource_name )
191+
192+ yield
196193
197- self .graphql_span .set_data ("graphql.operation.type" , operation_type )
198- self .graphql_span .set_data ("graphql.operation.name" , self ._operation_name )
199- self .graphql_span .set_data ("graphql.document" , self .execution_context .query )
200- self .graphql_span .set_data ("graphql.resource_name" , self ._resource_name )
194+ # we might have a more accurate operation_name after the parsing
195+ self ._operation_name = self .execution_context .operation_name
201196
202- yield
197+ if self ._operation_name is not None :
198+ graphql_span .set_data ("graphql.operation.name" , self ._operation_name )
203199
204- transaction = self .graphql_span .containing_transaction
205- if transaction and self .execution_context .operation_name :
206- transaction .name = self .execution_context .operation_name
207- transaction .source = TRANSACTION_SOURCE_COMPONENT
208- transaction .op = op
200+ sentry_sdk .get_current_scope ().set_transaction_name (
201+ self ._operation_name ,
202+ source = TRANSACTION_SOURCE_COMPONENT ,
203+ )
209204
210- self .graphql_span .finish ()
205+ root_span = graphql_span .root_span
206+ if root_span :
207+ root_span .op = op
211208
212209 def on_validate (self ):
213210 # type: () -> Generator[None, None, None]
214- self . validation_span = self . graphql_span . start_child (
211+ with sentry_sdk . start_span (
215212 op = OP .GRAPHQL_VALIDATE ,
216213 name = "validation" ,
217214 origin = StrawberryIntegration .origin ,
218- )
219-
220- yield
221-
222- self .validation_span .finish ()
215+ ):
216+ yield
223217
224218 def on_parse (self ):
225219 # type: () -> Generator[None, None, None]
226- self . parsing_span = self . graphql_span . start_child (
220+ with sentry_sdk . start_span (
227221 op = OP .GRAPHQL_PARSE ,
228222 name = "parsing" ,
229223 origin = StrawberryIntegration .origin ,
230- )
231-
232- yield
233-
234- self .parsing_span .finish ()
224+ ):
225+ yield
235226
236227 def should_skip_tracing (self , _next , info ):
237228 # type: (Callable[[Any, GraphQLResolveInfo, Any, Any], Any], GraphQLResolveInfo) -> bool
@@ -253,7 +244,7 @@ async def resolve(self, _next, root, info, *args, **kwargs):
253244
254245 field_path = "{}.{}" .format (info .parent_type , info .field_name )
255246
256- with self . graphql_span . start_child (
247+ with sentry_sdk . start_span (
257248 op = OP .GRAPHQL_RESOLVE ,
258249 name = "resolving {}" .format (field_path ),
259250 origin = StrawberryIntegration .origin ,
@@ -274,7 +265,7 @@ def resolve(self, _next, root, info, *args, **kwargs):
274265
275266 field_path = "{}.{}" .format (info .parent_type , info .field_name )
276267
277- with self . graphql_span . start_child (
268+ with sentry_sdk . start_span (
278269 op = OP .GRAPHQL_RESOLVE ,
279270 name = "resolving {}" .format (field_path ),
280271 origin = StrawberryIntegration .origin ,
0 commit comments