class Sapience::Extensions::ActiveRecord::Notifications

Public Class Methods

new(opts = {}) click to toggle source

Options:

*:metric_name - the metric name, defaults to “activerecord.query” *:include_schema - record schema queries, off by default *:include_generic - record general (nameless) queries, off by default *:tags - additional tags

# File lib/sapience/extensions/active_record/notifications.rb, line 12
def initialize(opts = {})
  super
  @metric_name     = opts[:metric_name] || "activerecord.sql"
  @include_schema  = opts[:include_schema] == true
  @include_generic = opts[:include_generic] == true
  @include_raw     = opts[:include_raw] == true

  Sapience::Extensions::Notifications.subscribe "sql.active_record" do |event|
    record event
  end
end

Private Instance Methods

record(event) click to toggle source
# File lib/sapience/extensions/active_record/notifications.rb, line 26
def record(event) # rubocop:disable AbcSize, CyclomaticComplexity, PerceivedComplexity
  return unless record?

  payload = event.payload
  name    = payload[:name]
  return if (name.nil? || name == "SQL") && !@include_generic
  return if name == "SCHEMA" && !@include_schema

  name = name.downcase.split(/\W/).join(".") if name
  tags = self.tags.dup
  tags.push "query:#{name}" if name

  metrics.batch do
    metrics.increment metric_name, tags: tags
    metrics.timing "#{metric_name}.time", event.duration, tags: tags
  end
end