From 705912364d8e33a170a350358dcfa58e08d7a421 Mon Sep 17 00:00:00 2001 From: Dmitry Rybakov <160598371+comandeo-mongo@users.noreply.github.com> Date: Thu, 29 Oct 2026 14:04:32 +0100 Subject: [PATCH] RUBY-3952 Skip span work for spans with invalid contexts With no OpenTelemetry SDK installed and no valid parent, the API returns spans whose context is invalid. They cannot be made current, recorded or propagated, but the tracers still entered with_span, built the result attributes and finished them for every operation and command. Return before any of that, as the OpenTelemetry spec's performance guidelines recommend. --- .../tracing/open_telemetry/command_tracer.rb | 26 ++++++++++--- .../open_telemetry/operation_tracer.rb | 25 +++++++++--- .../open_telemetry/command_tracer_spec.rb | 38 ++++++++++++++++++- .../open_telemetry/operation_tracer_spec.rb | 34 +++++++++++++++++ spec/support/tracing.rb | 10 +++++ 5 files changed, 122 insertions(+), 11 deletions(-) diff --git a/lib/mongo/tracing/open_telemetry/command_tracer.rb b/lib/mongo/tracing/open_telemetry/command_tracer.rb index 27585839de..4a1fa5e30a 100644 --- a/lib/mongo/tracing/open_telemetry/command_tracer.rb +++ b/lib/mongo/tracing/open_telemetry/command_tracer.rb @@ -64,14 +64,32 @@ def start_span(message, operation_context, connection); end # @yield the block representing the command to be traced. # # @return [ Object ] the result of the command. - # rubocop:disable Lint/RescueException - def trace_command(message, _operation_context, connection) + def trace_command(message, _operation_context, connection, &block) return yield if skip_tracing?(message) # Commands should always be nested under their operation span, not directly under # the transaction span. Don't pass with_parent to use automatic parent resolution # from the currently active span (the operation span). span = create_command_span(message, connection) + # With no SDK and no valid parent, the API returns a span with an + # invalid context. It cannot be made current, recorded or + # propagated, so it gets no work at all, not even finishing it. + return yield unless span.context.valid? + + trace_with_span(span, message, &block) + end + + private + + # Runs the command with the span made current, and finishes the span. + # + # @param span [ OpenTelemetry::Trace::Span ] a span with a valid context. + # @param message [ Mongo::Protocol::Message ] the command message. + # + # @return [ Object ] the result of the command. + # + # rubocop:disable Lint/RescueException + def trace_with_span(span, message) ::OpenTelemetry::Trace.with_span(span) do |s, c| yield.tap do |result| process_command_result(result, cursor_id(message), c, s) @@ -81,12 +99,10 @@ def trace_command(message, _operation_context, connection) handle_command_exception(span, e) raise e ensure - span&.finish + span.finish end # rubocop:enable Lint/RescueException - private - # Determines whether the command must not be traced. Sensitive auth # commands carry credentials in their payloads (SCRAM proofs, cleartext # passwords, etc.) and the OpenTelemetry spec requires drivers to skip diff --git a/lib/mongo/tracing/open_telemetry/operation_tracer.rb b/lib/mongo/tracing/open_telemetry/operation_tracer.rb index 2800fcd1e6..be5fb8c9d6 100644 --- a/lib/mongo/tracing/open_telemetry/operation_tracer.rb +++ b/lib/mongo/tracing/open_telemetry/operation_tracer.rb @@ -53,21 +53,36 @@ def initialize(otel_tracer, parent_tracer) # @yield the block representing the operation to be traced. # # @return [ Object ] the result of the operation. - # - # rubocop:disable Lint/RescueException def trace_operation(operation, operation_context, op_name: nil, &block) span = create_operation_span(operation, operation_context, op_name) + # With no SDK and no valid parent, the API returns a span with an + # invalid context. It cannot be made current, recorded or + # propagated, so it gets no work at all, not even finishing it. + return yield unless span.context.valid? + + trace_with_span(span, operation, &block) + end + + private + + # Runs the operation with the span made current, and finishes the span. + # + # @param span [ OpenTelemetry::Trace::Span ] a span with a valid context. + # @param operation [ Mongo::Operation ] the operation. + # + # @return [ Object ] the result of the operation. + # + # rubocop:disable Lint/RescueException + def trace_with_span(span, operation, &block) execute_with_span(span, operation, &block) rescue Exception => e handle_span_exception(span, e) raise e ensure - span&.finish + span.finish end # rubocop:enable Lint/RescueException - private - # Creates an OpenTelemetry span for the operation. # # @param operation [ Mongo::Operation ] the operation. diff --git a/spec/mongo/tracing/open_telemetry/command_tracer_spec.rb b/spec/mongo/tracing/open_telemetry/command_tracer_spec.rb index 28ac8b36fb..9b96d24895 100644 --- a/spec/mongo/tracing/open_telemetry/command_tracer_spec.rb +++ b/spec/mongo/tracing/open_telemetry/command_tracer_spec.rb @@ -65,7 +65,10 @@ end describe '#trace_command' do - let(:span) { instance_double(OpenTelemetry::Trace::Span, finish: nil, set_attribute: nil) } + let(:span_context) { instance_double(OpenTelemetry::Trace::SpanContext, valid?: true) } + let(:span) do + instance_double(OpenTelemetry::Trace::Span, finish: nil, set_attribute: nil, context: span_context) + end let(:context) { instance_double(Mongo::Operation::Context) } let(:result) { instance_double(Mongo::Operation::Result, cursor_id: 0, successful?: true) } @@ -101,6 +104,39 @@ command_tracer.trace_command(message, operation_context, connection) { result } end + # With no SDK installed and no valid parent, the API returns a span whose + # context is invalid. Nothing can be done with it, so it gets no work. + context 'with an invalid span' do + let(:invalid_span) { OpenTelemetry::Trace::Span::INVALID } + + before do + allow(otel_tracer).to receive(:start_span).and_return(invalid_span) + end + + it 'does not make the span current' do + expect(OpenTelemetry::Trace).not_to receive(:with_span) + command_tracer.trace_command(message, operation_context, connection) { result } + end + + it 'returns the block result' do + return_value = command_tracer.trace_command(message, operation_context, connection) { :done } + expect(return_value).to eq(:done) + end + + it 'does not finish the span' do + expect(invalid_span).not_to receive(:finish) + command_tracer.trace_command(message, operation_context, connection) { result } + end + + it 'does not record a raised exception' do + error = StandardError.new('generic error') + expect(invalid_span).not_to receive(:record_exception) + expect do + command_tracer.trace_command(message, operation_context, connection) { raise error } + end.to raise_error(error) + end + end + context 'when result has cursor_id' do let(:result) do instance_double(Mongo::Operation::Result, cursor_id: 789, successful?: true) diff --git a/spec/mongo/tracing/open_telemetry/operation_tracer_spec.rb b/spec/mongo/tracing/open_telemetry/operation_tracer_spec.rb index 0f7f6f2b00..35e7b0e53a 100644 --- a/spec/mongo/tracing/open_telemetry/operation_tracer_spec.rb +++ b/spec/mongo/tracing/open_telemetry/operation_tracer_spec.rb @@ -41,6 +41,7 @@ allow(s).to receive(:set_attribute) allow(s).to receive(:record_exception) allow(s).to receive(:status=) + allow(s).to receive(:context).and_return(double('SpanContext', valid?: true)) end end let(:context) { double('OpenTelemetry::Context') } @@ -91,6 +92,39 @@ operation_tracer.trace_operation(operation, operation_context) { result } end + # With no SDK installed and no valid parent, the API returns a span whose + # context is invalid. Nothing can be done with it, so it gets no work. + context 'with an invalid span' do + let(:invalid_span) { OpenTelemetry::Trace::Span::INVALID } + + before do + allow(otel_tracer).to receive(:start_span).and_return(invalid_span) + end + + it 'does not make the span current' do + expect(OpenTelemetry::Trace).not_to receive(:with_span) + operation_tracer.trace_operation(operation, operation_context) { result } + end + + it 'returns the block result' do + return_value = operation_tracer.trace_operation(operation, operation_context) { :done } + expect(return_value).to eq(:done) + end + + it 'does not finish the span' do + expect(invalid_span).not_to receive(:finish) + operation_tracer.trace_operation(operation, operation_context) { result } + end + + it 'does not record a raised exception' do + error = StandardError.new('test error') + expect(invalid_span).not_to receive(:record_exception) + expect do + operation_tracer.trace_operation(operation, operation_context) { raise error } + end.to raise_error(error) + end + end + context 'with custom operation name' do it 'uses the provided op_name' do expect(otel_tracer).to receive(:start_span).with( diff --git a/spec/support/tracing.rb b/spec/support/tracing.rb index 6eb96801da..c2476cbb07 100644 --- a/spec/support/tracing.rb +++ b/spec/support/tracing.rb @@ -22,6 +22,12 @@ def set_attribute(key, value) @attributes[key] = value end + # Mirrors OpenTelemetry::Trace::Span#context. Spans from this tracer + # always have a valid context, so the driver traces them fully. + def context + @context ||= Context.new(self) + end + # rubocop:disable Lint/UnusedMethodArgument def record_exception(exception, attributes: nil) set_attribute('exception.type', exception.class.to_s) @@ -53,6 +59,10 @@ class Context def initialize(span) @span = span end + + def valid? + true + end end class Tracer