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
76 changes: 35 additions & 41 deletions lib/mongo/tracing/open_telemetry/command_tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def trace_command(message, _operation_context, connection, &block)
private

# Runs the command with the span made current, and finishes the span.
# A span that is not recording is still made current, so that its
# context is propagated, but gets no further attributes and no
# outcome: the SDK would discard them.
#
# @param span [ OpenTelemetry::Trace::Span ] a span with a valid context.
# @param message [ Mongo::Protocol::Message ] the command message.
Expand All @@ -90,13 +93,15 @@ def trace_command(message, _operation_context, connection, &block)
#
# rubocop:disable Lint/RescueException
def trace_with_span(span, message)
::OpenTelemetry::Trace.with_span(span) do |s, c|
recording = span.recording?
apply_deferred_attributes(span, message) if recording
::OpenTelemetry::Trace.with_span(span) do |s, _c|
yield.tap do |result|
process_command_result(result, cursor_id(message), c, s)
process_command_result(result, s) if recording
end
end
rescue Exception => e
handle_command_exception(span, e)
handle_command_exception(span, e) if recording
raise e
ensure
span.finish
Expand Down Expand Up @@ -136,55 +141,59 @@ def create_command_span(message, connection)
# Processes the command result and updates span attributes.
#
# @param result [ Object ] the command result.
# @param cursor_id [ Integer | nil ] the cursor ID.
# @param context [ OpenTelemetry::Context ] the context.
# @param span [ OpenTelemetry::Trace::Span ] the current span.
def process_command_result(result, cursor_id, context, span)
process_cursor_context(result, cursor_id, context, span)
def process_command_result(result, span)
process_cursor_context(result, span)
maybe_trace_error(result, span)
end

# Handles exceptions that occur during command execution.
#
# @param span [ OpenTelemetry::Trace::Span | nil ] the span.
# @param span [ OpenTelemetry::Trace::Span ] the span.
# @param exception [ Exception ] the exception that occurred.
def handle_command_exception(span, exception)
return unless span

if exception.is_a?(Mongo::Error::OperationFailure)
span.set_attribute('db.response.status_code', exception.code.to_s)
end
span.record_exception(exception)
span.status = ::OpenTelemetry::Trace::Status.error("Unhandled exception of type: #{exception.class}")
end

# Builds span attributes for the command.
# Builds the attributes passed at span creation: the cheap set the
# OpenTelemetry spec recommends. They are built for every span,
# recorded or not, and are the only ones the sampler sees. The rest
# are set by apply_deferred_attributes once the span is known to
# record.
#
# @param message [ Mongo::Protocol::Message ] the command message.
# @param connection [ Mongo::Server::Connection ] the connection.
#
# @return [ Hash ] OpenTelemetry span attributes following MongoDB semantic conventions.
def span_attributes(message, connection)
base_attributes(message)
.merge(connection_attributes(connection))
.merge(session_attributes(message))
.compact
{
'db.system.name' => 'mongodb',
'db.namespace' => database(message),
'db.collection.name' => collection_name(message),
'db.command.name' => command_name(message)
}.compact.merge(connection_attributes(connection))
end

# Returns base database and command attributes.
# Sets the remaining attributes on a span that is recording. They are
# not visible to the sampler; only custom samplers keying on them are
# affected.
#
# @param span [ OpenTelemetry::Trace::Span ] the span.
# @param message [ Mongo::Protocol::Message ] the command message.
#
# @return [ Hash ] base span attributes.
def base_attributes(message)
def apply_deferred_attributes(span, message)
{
'db.system.name' => 'mongodb',
'db.namespace' => database(message),
'db.collection.name' => collection_name(message),
'db.command.name' => command_name(message),
'db.query.summary' => query_summary(message),
'db.query.text' => query_text(message)
}
'db.query.text' => query_text(message),
'db.mongodb.cursor_id' => cursor_id(message),
'db.mongodb.lsid' => lsid(message),
'db.mongodb.txn_number' => txn_number(message)
}.each do |key, value|
span.set_attribute(key, value) unless value.nil?
end
end

# Returns connection-related attributes.
Expand All @@ -202,26 +211,11 @@ def connection_attributes(connection)
}
end

# Returns session and transaction attributes.
#
# @param message [ Mongo::Protocol::Message ] the command message.
#
# @return [ Hash ] session span attributes.
def session_attributes(message)
{
'db.mongodb.cursor_id' => cursor_id(message),
'db.mongodb.lsid' => lsid(message),
'db.mongodb.txn_number' => txn_number(message)
}
end

# Processes cursor context from the command result.
#
# @param result [ Object ] the command result.
# @param _cursor_id [ Integer | nil ] the cursor ID (unused).
# @param _context [ OpenTelemetry::Context ] the context (unused).
# @param span [ OpenTelemetry::Trace::Span ] the current span.
def process_cursor_context(result, _cursor_id, _context, span)
def process_cursor_context(result, span)
cursor_id = normalize_cursor_id(result.cursor_id)
return unless cursor_id.positive?

Expand Down
32 changes: 21 additions & 11 deletions lib/mongo/tracing/open_telemetry/operation_tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def trace_operation(operation, operation_context, op_name: nil, &block)
private

# Runs the operation with the span made current, and finishes the span.
# A span that is not recording is still made current, so that the
# command spans created within it nest under it, but gets no further
# attributes and no outcome: the SDK would discard them.
#
# @param span [ OpenTelemetry::Trace::Span ] a span with a valid context.
# @param operation [ Mongo::Operation ] the operation.
Expand All @@ -74,9 +77,13 @@ def trace_operation(operation, operation_context, op_name: nil, &block)
#
# rubocop:disable Lint/RescueException
def trace_with_span(span, operation, &block)
execute_with_span(span, operation, &block)
recording = span.recording?
if recording && !operation.cursor_id.nil?
span.set_attribute('db.mongodb.cursor_id', operation.cursor_id)
end
execute_with_span(span, operation, recording, &block)
rescue Exception => e
handle_span_exception(span, e)
handle_span_exception(span, e) if recording
raise e
ensure
span.finish
Expand Down Expand Up @@ -104,14 +111,15 @@ def create_operation_span(operation, operation_context, op_name)
#
# @param span [ OpenTelemetry::Trace::Span ] the span.
# @param operation [ Mongo::Operation ] the operation.
# @param recording [ true | false ] whether the span is recording.
#
# @yield the block to execute.
#
# @return [ Object ] the result of the block.
def execute_with_span(span, operation)
def execute_with_span(span, operation, recording)
::OpenTelemetry::Trace.with_span(span) do |s, c|
yield.tap do |result|
process_cursor_context(result, operation.cursor_id, c, s)
process_cursor_context(result, operation.cursor_id, c, s, recording)
end
end
end
Expand All @@ -121,8 +129,6 @@ def execute_with_span(span, operation)
# @param span [ OpenTelemetry::Trace::Span ] the span.
# @param exception [ Exception ] the exception.
def handle_span_exception(span, exception)
return unless span

span.record_exception(exception)
span.status = ::OpenTelemetry::Trace::Status.error(
"Unhandled exception of type: #{exception.class}"
Expand All @@ -139,7 +145,11 @@ def operation_name(operation, op_name = nil)
op_name || operation.class.name.split('::').last.downcase
end

# Builds span attributes for the operation.
# Builds the attributes passed at span creation. The cursor id is set
# by trace_with_span once the span is known to record.
# db.operation.summary is outside the spec's recommended creation set,
# but it is the span name, which is built anyway, and an attribute
# passed at creation is cheaper than one set later.
#
# @param operation [ Mongo::Operation ] the operation.
# @param op_name [ String | nil ] optional operation name.
Expand All @@ -151,8 +161,7 @@ def span_attributes(operation, op_name)
'db.namespace' => operation.db_name.to_s,
'db.collection.name' => collection_name(operation),
'db.operation.name' => operation_name(operation, op_name),
'db.operation.summary' => operation_span_name(operation, op_name),
'db.mongodb.cursor_id' => operation.cursor_id,
'db.operation.summary' => operation_span_name(operation, op_name)
}.compact
end

Expand All @@ -165,7 +174,8 @@ def span_attributes(operation, op_name)
# @param cursor_id [ Integer | nil ] the cursor ID before the operation.
# @param context [ OpenTelemetry::Context ] the OpenTelemetry context.
# @param span [ OpenTelemetry::Trace::Span ] the current span.
def process_cursor_context(result, cursor_id, context, span)
# @param recording [ true | false ] whether the span is recording.
def process_cursor_context(result, cursor_id, context, span, recording)
return unless result.is_a?(Cursor)

if result.id.zero?
Expand All @@ -174,7 +184,7 @@ def process_cursor_context(result, cursor_id, context, span)
elsif result.id && cursor_id.nil?
# New cursor created, store its context.
cursor_context_map[result.id] = context
span.set_attribute('db.mongodb.cursor_id', result.id)
span.set_attribute('db.mongodb.cursor_id', result.id) if recording
end
end

Expand Down
Loading
Loading