Class: Binocs::SqlLogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/binocs/log_subscriber.rb

Overview

Captures ActiveRecord SQL queries during a request

Constant Summary collapse

IGNORED_QUERIES =

Only capture queries that are meaningful for debugging

%w[SCHEMA EXPLAIN].freeze

Instance Method Summary collapse

Instance Method Details

#sql(event) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/binocs/log_subscriber.rb', line 96

def sql(event)
  return unless Thread.current[:binocs_logs]

  payload = event.payload
  return if IGNORED_QUERIES.include?(payload[:name])
  return if payload[:name] == "CACHE"

  entry = {
    type: "sql",
    name: payload[:name] || "SQL",
    sql: truncate_sql(payload[:sql]),
    duration: event.duration.round(2),
    timestamp: Time.current.iso8601
  }

  # Flag queries that are likely part of an error
  if payload[:exception]
    entry[:error] = true
    entry[:exception_class] = payload[:exception].first
    entry[:exception_message] = payload[:exception].second
  end

  Thread.current[:binocs_logs] << entry
end