Module: RailsOtelContext::ActiveRecordContext::RelationScopeCapture

Defined in:
lib/rails_otel_context/activerecord_context.rb

Overview

Captures scope name from Relation at SQL materialization time.

exec_queries covers record-loading (.to_a, .load). Aggregate and existence queries take separate paths that never call exec_queries – count/sum/etc. go through #calculate, and #pluck / #exists? are their own methods – so each needs the same scope-key capture to tag its sql.active_record span.

Instance Method Summary collapse

Instance Method Details

#calculate(*args, **kwargs) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/rails_otel_context/activerecord_context.rb', line 191

def calculate(*args, **kwargs, &)
  scope_name = instance_variable_get(:@_otel_scope_name)
  Thread.current[SCOPE_THREAD_KEY] = scope_name if scope_name
  super
ensure
  Thread.current[SCOPE_THREAD_KEY] = nil
end

#exec_queriesObject



183
184
185
186
187
188
189
# File 'lib/rails_otel_context/activerecord_context.rb', line 183

def exec_queries(&)
  scope_name = instance_variable_get(:@_otel_scope_name)
  Thread.current[SCOPE_THREAD_KEY] = scope_name if scope_name
  super
ensure
  Thread.current[SCOPE_THREAD_KEY] = nil
end

#exists?(*args) ⇒ Boolean

Returns:

  • (Boolean)


207
208
209
210
211
212
213
# File 'lib/rails_otel_context/activerecord_context.rb', line 207

def exists?(*args)
  scope_name = instance_variable_get(:@_otel_scope_name)
  Thread.current[SCOPE_THREAD_KEY] = scope_name if scope_name
  super
ensure
  Thread.current[SCOPE_THREAD_KEY] = nil
end

#pluck(*args) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/rails_otel_context/activerecord_context.rb', line 199

def pluck(*args, &)
  scope_name = instance_variable_get(:@_otel_scope_name)
  Thread.current[SCOPE_THREAD_KEY] = scope_name if scope_name
  super
ensure
  Thread.current[SCOPE_THREAD_KEY] = nil
end