Sha256: 58253295a1463f909b7bf74052a3b20b3484edd7edad4df18196f786f5501487

Contents?: true

Size: 1.59 KB

Versions: 16

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true
module Sapience
  module Extensions
    module ActiveRecord
      class Notifications < ::Sapience::Extensions::Notifications
        # Options:
        #
        # *<tt>:metric_name</tt>      - the metric name, defaults to "activerecord.query"
        # *<tt>:include_schema</tt>   - record schema queries, off by default
        # *<tt>:include_generic</tt>  - record general (nameless) queries, off by default
        # *<tt>:tags</tt>             - additional tags
        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

        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
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
sapience-3.0 lib/sapience/extensions/active_record/notifications.rb
sapience-2.15 lib/sapience/extensions/active_record/notifications.rb
sapience-2.14 lib/sapience/extensions/active_record/notifications.rb
sapience-2.13 lib/sapience/extensions/active_record/notifications.rb
sapience-2.12 lib/sapience/extensions/active_record/notifications.rb
sapience-2.11 lib/sapience/extensions/active_record/notifications.rb
sapience-2.10 lib/sapience/extensions/active_record/notifications.rb
sapience-2.9.1 lib/sapience/extensions/active_record/notifications.rb
sapience-2.8 lib/sapience/extensions/active_record/notifications.rb
sapience-2.7.0 lib/sapience/extensions/active_record/notifications.rb
sapience-2.6.1 lib/sapience/extensions/active_record/notifications.rb
sapience-2.6.0 lib/sapience/extensions/active_record/notifications.rb
sapience-2.5.4 lib/sapience/extensions/active_record/notifications.rb
sapience-2.5.3 lib/sapience/extensions/active_record/notifications.rb
sapience-2.5.2 lib/sapience/extensions/active_record/notifications.rb
sapience-2.5.0 lib/sapience/extensions/active_record/notifications.rb