Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions lib/mongo/tracing/open_telemetry/command_tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
25 changes: 20 additions & 5 deletions lib/mongo/tracing/open_telemetry/operation_tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 37 additions & 1 deletion spec/mongo/tracing/open_telemetry/command_tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions spec/mongo/tracing/open_telemetry/operation_tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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') }
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions spec/support/tracing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -53,6 +59,10 @@ class Context
def initialize(span)
@span = span
end

def valid?
true
end
end

class Tracer
Expand Down
Loading