Class: Mongo::Tracing::OpenTelemetry::CommandTracer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/tracing/open_telemetry/command_tracer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

CommandTracer is responsible for tracing MongoDB server commands using OpenTelemetry.

Instance Method Summary collapse

Constructor Details

#initialize(otel_tracer, parent_tracer, query_text_max_length: 0) ⇒ CommandTracer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a new CommandTracer.

Parameters:

  • otel_tracer (OpenTelemetry::Trace::Tracer)

    the OpenTelemetry tracer.

  • parent_tracer (Mongo::Tracing::OpenTelemetry::Tracer)

    the parent tracer for accessing shared context maps.

  • query_text_max_length (Integer) (defaults to: 0)

    maximum length for captured query text. Defaults to 0 (no query text capture).



31
32
33
34
35
# File 'lib/mongo/tracing/open_telemetry/command_tracer.rb', line 31

def initialize(otel_tracer, parent_tracer, query_text_max_length: 0)
  @otel_tracer = otel_tracer
  @parent_tracer = parent_tracer
  @query_text_max_length = query_text_max_length
end

Instance Method Details

#start_span(message, operation_context, connection) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Starts a span for a MongoDB command.

Parameters:



42
# File 'lib/mongo/tracing/open_telemetry/command_tracer.rb', line 42

def start_span(message, operation_context, connection); end

#trace_command(message, _operation_context, connection) { ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Trace a MongoDB command.

Creates an OpenTelemetry span for the command, capturing attributes such as command name, database name, collection name, server address, connection IDs, and optionally query text. The span is automatically nested under the current operation span and is finished when the command completes or fails.

rubocop:disable Lint/RescueException

Parameters:

Yields:

  • the block representing the command to be traced.

Returns:

  • (Object)

    the result of the command.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mongo/tracing/open_telemetry/command_tracer.rb', line 59

def trace_command(message, _operation_context, connection)
  # 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)
  ::OpenTelemetry::Trace.with_span(span) do |s, c|
    yield.tap do |result|
      process_command_result(result, cursor_id(message), c, s)
    end
  end
rescue Exception => e
  handle_command_exception(span, e)
  raise e
ensure
  span&.finish
end