Class: Mongo::Tracing::OpenTelemetry::Tracer Private
- Inherits:
-
Object
- Object
- Mongo::Tracing::OpenTelemetry::Tracer
- Defined in:
- lib/mongo/tracing/open_telemetry/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.
OpenTelemetry tracer for MongoDB operations and commands.
Instance Attribute Summary collapse
-
#otel_tracer ⇒ OpenTelemetry::Trace::Tracer
readonly
private
The OpenTelemetry tracer implementation used to create spans for MongoDB operations and commands.
Instance Method Summary collapse
-
#cursor_context_map ⇒ Hash
private
Returns the cursor context map for tracking cursor-related OpenTelemetry contexts.
-
#cursor_map_key(session, cursor_id) ⇒ String | nil
private
Generates a unique key for cursor tracking in the context map.
-
#enabled? ⇒ Boolean
private
Whether OpenTelemetry is enabled or not.
-
#finish_transaction_span(session) ⇒ Object
private
Finish a transaction span and deactivate its context.
-
#initialize(enabled: nil, query_text_max_length: nil, otel_tracer: nil) ⇒ Tracer
constructor
private
Initializes a new OpenTelemetry tracer.
-
#parent_context_for(operation_context, cursor_id) ⇒ OpenTelemetry::Context | nil
private
Determines the parent OpenTelemetry context for an operation.
-
#start_transaction_span(session) ⇒ Object
private
Start a transaction span and activate its context.
-
#trace_command(message, operation_context, connection) { ... } ⇒ Object
private
Trace a MongoDB command.
-
#trace_operation(operation, operation_context, op_name: nil) { ... } ⇒ Object
private
Trace a MongoDB operation.
-
#transaction_context_map ⇒ Hash
private
Returns the transaction context map for tracking active transaction contexts.
-
#transaction_map_key(session) ⇒ String | nil
private
Generates a unique key for transaction tracking.
-
#transaction_span_map ⇒ Hash
private
Returns the transaction span map for tracking active transaction spans.
-
#transaction_token_map ⇒ Hash
private
Returns the transaction token map for tracking context attachment tokens.
Constructor Details
#initialize(enabled: nil, query_text_max_length: nil, otel_tracer: nil) ⇒ Tracer
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 OpenTelemetry tracer.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 42 def initialize(enabled: nil, query_text_max_length: nil, otel_tracer: nil) @enabled = if enabled.nil? %w[true 1 yes].include?(ENV['OTEL_RUBY_INSTRUMENTATION_MONGODB_ENABLED']&.downcase) else enabled end check_opentelemetry_loaded @query_text_max_length = if query_text_max_length.nil? ENV['OTEL_RUBY_INSTRUMENTATION_MONGODB_QUERY_TEXT_MAX_LENGTH'].to_i else query_text_max_length end @otel_tracer = otel_tracer || initialize_tracer @operation_tracer = OperationTracer.new(@otel_tracer, self) @command_tracer = CommandTracer.new(@otel_tracer, self, query_text_max_length: @query_text_max_length) end |
Instance Attribute Details
#otel_tracer ⇒ OpenTelemetry::Trace::Tracer (readonly)
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.
Returns the OpenTelemetry tracer implementation used to create spans for MongoDB operations and commands.
27 28 29 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 27 def otel_tracer @otel_tracer end |
Instance Method Details
#cursor_context_map ⇒ Hash
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.
Returns the cursor context map for tracking cursor-related OpenTelemetry contexts.
145 146 147 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 145 def cursor_context_map @cursor_context_map ||= {} end |
#cursor_map_key(session, cursor_id) ⇒ String | nil
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.
Generates a unique key for cursor tracking in the context map.
155 156 157 158 159 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 155 def cursor_map_key(session, cursor_id) return if cursor_id.nil? || session.nil? "#{session.session_id['id'].to_uuid}-#{cursor_id}" end |
#enabled? ⇒ Boolean
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.
Whether OpenTelemetry is enabled or not.
62 63 64 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 62 def enabled? @enabled end |
#finish_transaction_span(session) ⇒ 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.
Finish a transaction span and deactivate its context.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 123 def finish_transaction_span(session) return unless enabled? key = transaction_map_key(session) return unless key span = transaction_span_map.delete(key) token = transaction_token_map.delete(key) transaction_context_map.delete(key) return unless span && token begin span.finish ensure ::OpenTelemetry::Context.detach(token) end end |
#parent_context_for(operation_context, cursor_id) ⇒ OpenTelemetry::Context | nil
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.
Determines the parent OpenTelemetry context for an operation.
Returns the transaction context if the operation is part of a transaction, otherwise returns nil. Cursor-based context nesting is not currently implemented.
170 171 172 173 174 175 176 177 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 170 def parent_context_for(operation_context, cursor_id) if (key = transaction_map_key(operation_context.session)) transaction_context_map[key] elsif (_key = cursor_map_key(operation_context.session, cursor_id)) # We return nil here unless we decide how to nest cursor operations. nil end end |
#start_transaction_span(session) ⇒ 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.
Start a transaction span and activate its context.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 95 def start_transaction_span(session) return unless enabled? key = transaction_map_key(session) return unless key # Create the transaction span with minimal attributes span = @otel_tracer.start_span( 'transaction', attributes: { 'db.system' => 'mongodb' }, kind: :client ) # Create a context containing this span context = ::OpenTelemetry::Trace.context_with_span(span) # Activate the context and store the token for later detachment token = ::OpenTelemetry::Context.attach(context) # Store span, token, and context for later retrieval transaction_span_map[key] = span transaction_token_map[key] = token transaction_context_map[key] = context 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.
86 87 88 89 90 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 86 def trace_command(, operation_context, connection, &block) return yield unless enabled? @command_tracer.trace_command(, operation_context, connection, &block) end |
#trace_operation(operation, operation_context, op_name: nil) { ... } ⇒ 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 operation.
73 74 75 76 77 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 73 def trace_operation(operation, operation_context, op_name: nil, &block) return yield unless enabled? @operation_tracer.trace_operation(operation, operation_context, op_name: op_name, &block) end |
#transaction_context_map ⇒ Hash
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.
Returns the transaction context map for tracking active transaction contexts.
182 183 184 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 182 def transaction_context_map @transaction_context_map ||= {} end |
#transaction_map_key(session) ⇒ String | nil
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.
Generates a unique key for transaction tracking.
Returns nil for implicit sessions or sessions not in a transaction.
207 208 209 210 211 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 207 def transaction_map_key(session) return if session.nil? || session.implicit? || !session.in_transaction? "#{session.session_id['id'].to_uuid}-#{session.txn_num}" end |
#transaction_span_map ⇒ Hash
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.
Returns the transaction span map for tracking active transaction spans.
189 190 191 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 189 def transaction_span_map @transaction_span_map ||= {} end |
#transaction_token_map ⇒ Hash
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.
Returns the transaction token map for tracking context attachment tokens.
196 197 198 |
# File 'lib/mongo/tracing/open_telemetry/tracer.rb', line 196 def transaction_token_map @transaction_token_map ||= {} end |