Class: Salopulse::Instrumentation::ActiveRecordSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/salopulse/instrumentation/active_record_subscriber.rb

Constant Summary collapse

INTERNAL_NAMES =
%w[SCHEMA TRANSACTION].freeze

Class Method Summary collapse

Class Method Details

.internal?(event) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
# File 'lib/salopulse/instrumentation/active_record_subscriber.rb', line 28

def self.internal?(event)
  name = event.payload[:name].to_s
  return true if INTERNAL_NAMES.include?(name)
  return true if event.payload[:cached]
  sql = event.payload[:sql].to_s
  sql.start_with?("SHOW ", "EXPLAIN ", "PRAGMA ")
end

.subscribe(client) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/salopulse/instrumentation/active_record_subscriber.rb', line 6

def self.subscribe(client)
  require "active_support/notifications"
  @subscriber ||= ActiveSupport::Notifications.subscribe("sql.active_record") do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    next if internal?(event)
    next if Salopulse::RequestContext.suppressed?

    client.capture_sql(
      query: event.payload[:sql],
      duration_ms: event.duration,
      rows_returned: event.payload[:row_count]
    )
  end
end

.unsubscribeObject



21
22
23
24
25
26
# File 'lib/salopulse/instrumentation/active_record_subscriber.rb', line 21

def self.unsubscribe
  return unless @subscriber
  require "active_support/notifications"
  ActiveSupport::Notifications.unsubscribe(@subscriber)
  @subscriber = nil
end