diff --git a/lib/datadog/tracing/contrib/rails/runner.rb b/lib/datadog/tracing/contrib/rails/runner.rb index 79920d5b707..c6461c63a08 100644 --- a/lib/datadog/tracing/contrib/rails/runner.rb +++ b/lib/datadog/tracing/contrib/rails/runner.rb @@ -58,7 +58,7 @@ def runner(code_or_file = nil, *_command_argv) # Capture the executed source code when provided from STDIN. def eval(*args) span = Datadog::Tracing.active_span - if span.name == Ext::SPAN_RUNNER_STDIN + if span&.name == Ext::SPAN_RUNNER_STDIN source = args[0] span.set_tag( Ext::TAG_RUNNER_SOURCE, diff --git a/spec/datadog/tracing/contrib/rails/runner_spec.rb b/spec/datadog/tracing/contrib/rails/runner_spec.rb index 388951ac72f..79e05d73215 100644 --- a/spec/datadog/tracing/contrib/rails/runner_spec.rb +++ b/spec/datadog/tracing/contrib/rails/runner_spec.rb @@ -136,6 +136,14 @@ expect(span.get_tag('operation')).to eq('runner.inline') end + context "when a current span isn't detected" do + it "doesn't error when a span can't be identified to set the source tag on" do + allow(Datadog::Tracing).to receive(:active_span).and_return(nil) + + expect { run }.to output('OK').to_stdout + end + end + include_context 'with a custom service name' include_context 'with source code too long'