From 7b42c286461e0c91a401d09fdde7c1bfe01c4900 Mon Sep 17 00:00:00 2001 From: Patrick Cherry Date: Wed, 7 Feb 2024 10:36:14 +0000 Subject: [PATCH] Return early if `with_child_span` returns nil --- lib/graphql/tracing/sentry_trace.rb | 5 +++-- spec/graphql/tracing/sentry_trace_spec.rb | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/graphql/tracing/sentry_trace.rb b/lib/graphql/tracing/sentry_trace.rb index 35d890af5d..744a40d424 100644 --- a/lib/graphql/tracing/sentry_trace.rb +++ b/lib/graphql/tracing/sentry_trace.rb @@ -64,9 +64,10 @@ def instrument_execution(platform_key, trace_method, data=nil, &block) return yield unless Sentry.initialized? Sentry.with_child_span(op: platform_key, start_timestamp: Sentry.utc_now.to_f) do |span| - result = block.call - span.finish + result = yield + return result unless span + span.finish if trace_method == "execute_multiplex" && data.key?(:multiplex) operation_names = data[:multiplex].queries.map{|q| operation_name(q) } span.set_description(operation_names.join(", ")) diff --git a/spec/graphql/tracing/sentry_trace_spec.rb b/spec/graphql/tracing/sentry_trace_spec.rb index d75bd52a5c..f80ee6a79e 100644 --- a/spec/graphql/tracing/sentry_trace_spec.rb +++ b/spec/graphql/tracing/sentry_trace_spec.rb @@ -40,6 +40,17 @@ def thing; :thing; end end end + describe "When Sentry.with_child_span returns nil" do + it "does not initialize any spans" do + Sentry.stub(:with_child_span, nil) do + SentryTraceTestSchema.execute("{ int thing { str } }") + assert_equal [], Sentry::SPAN_DATA + assert_equal [], Sentry::SPAN_DESCRIPTIONS + assert_equal [], Sentry::SPAN_OPS + end + end + end + it "sets the expected spans" do SentryTraceTestSchema.execute("{ int thing { str } }") expected_span_ops = [