Class: HoneycombRails::Subscribers::ActiveRecord

Inherits:
Object
  • Object
show all
Includes:
Sampling
Defined in:
lib/honeycomb-rails/subscribers/active_record.rb

Instance Method Summary collapse

Methods included from Sampling

#sample_event_if_required

Constructor Details

#initialize(honeybuilder) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



12
13
14
# File 'lib/honeycomb-rails/subscribers/active_record.rb', line 12

def initialize(honeybuilder)
  @honeybuilder = honeybuilder
end

Instance Method Details

#call(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/honeycomb-rails/subscribers/active_record.rb', line 22

def call(*args)
  event = ActiveSupport::Notifications::Event.new(*args)
  data = event.payload.slice(:name, :connection_id)
  data[:sql] = event.payload[:sql].strip
  event.payload[:binds].each do |b|
    case b
    when Array
      data["bind_#{ b[0].name }".to_sym] = b[1]
    else
      data["bind_#{ b.name }".to_sym] = b.value
    end
  end
  data[:duration] = event.duration

  # NOTE: Backtraces can be very verbose! Keep an eye on the data that gets sent
  #       into Honeycomb and, if needed, experiment with BacktraceCleaner's
  #       filters and silencers to trim down the noise.
  data[:local_stack] = Rails.backtrace_cleaner.clean(caller)

  honeycomb_event = @honeybuilder.event
  honeycomb_event.add(data)

  sample_event_if_required(honeycomb_event, event)

  honeycomb_event.send
end

#subscribe!Object



16
17
18
19
20
# File 'lib/honeycomb-rails/subscribers/active_record.rb', line 16

def subscribe!
  ::ActiveSupport::Notifications.subscribe(/sql.active_record/) do |*args|
    call(*args)
  end
end