Class: Railscope::Subscribers::QuerySubscriber

Inherits:
BaseSubscriber show all
Defined in:
lib/railscope/subscribers/query_subscriber.rb

Constant Summary collapse

EVENT_NAME =
"sql.active_record"
IGNORED_NAMES =
%w[SCHEMA TRANSACTION].freeze
IGNORED_SQL_PATTERNS =
[
  /\A\s*SELECT.*sqlite_master/i,
  /\A\s*SELECT.*pg_catalog/i,
  /\A\s*SELECT.*information_schema/i,
  /\A\s*SHOW/i,
  /\A\s*SET/i
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.subscribe ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/railscope/subscribers/query_subscriber.rb', line 17

def self.subscribe
  return if @subscribed

  @subscribed = true

  ActiveSupport::Notifications.subscribe(EVENT_NAME) do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    new.record(event)
  end
end

Instance Method Details

#record(event) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/railscope/subscribers/query_subscriber.rb', line 28

def record(event)
  return unless Railscope.enabled?
  return unless Railscope.ready?
  return if ignore_query?(event)

  # Debug: log the batch_id being used
  if context[:job_class].present?
    Rails.logger.debug("[Railscope] Query in job context - batch_id: #{context.batch_id}, job: #{context[:job_class]}")
  end

  create_entry!(
    entry_type: "query",
    payload: build_payload(event),
    tags: build_tags(event),
    family_hash: build_family_hash(event),
    should_display_on_index: true
  )
rescue StandardError => e
  Rails.logger.error("[Railscope] Failed to record query: #{e.message}")
end