@@ -179,59 +179,50 @@ def on_operation(self):
179
179
},
180
180
)
181
181
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
196
193
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
201
196
202
- yield
197
+ if self ._operation_name is not None :
198
+ graphql_span .set_data ("graphql.operation.name" , self ._operation_name )
203
199
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
+ )
209
204
210
- self .graphql_span .finish ()
205
+ root_span = graphql_span .root_span
206
+ if root_span :
207
+ root_span .op = op
211
208
212
209
def on_validate (self ):
213
210
# type: () -> Generator[None, None, None]
214
- self . validation_span = self . graphql_span . start_child (
211
+ with sentry_sdk . start_span (
215
212
op = OP .GRAPHQL_VALIDATE ,
216
213
name = "validation" ,
217
214
origin = StrawberryIntegration .origin ,
218
- )
219
-
220
- yield
221
-
222
- self .validation_span .finish ()
215
+ ):
216
+ yield
223
217
224
218
def on_parse (self ):
225
219
# type: () -> Generator[None, None, None]
226
- self . parsing_span = self . graphql_span . start_child (
220
+ with sentry_sdk . start_span (
227
221
op = OP .GRAPHQL_PARSE ,
228
222
name = "parsing" ,
229
223
origin = StrawberryIntegration .origin ,
230
- )
231
-
232
- yield
233
-
234
- self .parsing_span .finish ()
224
+ ):
225
+ yield
235
226
236
227
def should_skip_tracing (self , _next , info ):
237
228
# type: (Callable[[Any, GraphQLResolveInfo, Any, Any], Any], GraphQLResolveInfo) -> bool
@@ -253,7 +244,7 @@ async def resolve(self, _next, root, info, *args, **kwargs):
253
244
254
245
field_path = "{}.{}" .format (info .parent_type , info .field_name )
255
246
256
- with self . graphql_span . start_child (
247
+ with sentry_sdk . start_span (
257
248
op = OP .GRAPHQL_RESOLVE ,
258
249
name = "resolving {}" .format (field_path ),
259
250
origin = StrawberryIntegration .origin ,
@@ -274,7 +265,7 @@ def resolve(self, _next, root, info, *args, **kwargs):
274
265
275
266
field_path = "{}.{}" .format (info .parent_type , info .field_name )
276
267
277
- with self . graphql_span . start_child (
268
+ with sentry_sdk . start_span (
278
269
op = OP .GRAPHQL_RESOLVE ,
279
270
name = "resolving {}" .format (field_path ),
280
271
origin = StrawberryIntegration .origin ,
0 commit comments