Sha256: cd62f68a18e70004ec23c1c6f21490b7ff15dfa452b359deffcd2b213710ac32

Contents?: true

Size: 1.32 KB

Versions: 8

Compression:

Stored size: 1.32 KB

Contents

module Datadog::Notifications::Plugins
  class ActiveRecord < Base
    attr_reader :metric_name

    # 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(metric_name: 'activerecord.sql', **opts)
      super

      @metric_name     = metric_name
      @include_schema  = opts[:include_schema] == true
      @include_generic = opts[:include_generic] == true
      @include_raw     = opts[:include_raw] == true

      Datadog::Notifications.subscribe 'sql.active_record' do |reporter, event|
        record reporter, event
      end
    end

    private

    def record(reporter, event)
      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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
datadog-notifications-0.7.2 lib/datadog/notifications/plugins/active_record.rb
datadog-notifications-0.7.1 lib/datadog/notifications/plugins/active_record.rb
datadog-notifications-0.7.0 lib/datadog/notifications/plugins/active_record.rb
datadog-notifications-0.6.7 lib/datadog/notifications/plugins/active_record.rb
datadog-notifications-0.6.6 lib/datadog/notifications/plugins/active_record.rb
datadog-notifications-0.6.5 lib/datadog/notifications/plugins/active_record.rb
datadog-notifications-0.6.4 lib/datadog/notifications/plugins/active_record.rb
datadog-notifications-0.6.3 lib/datadog/notifications/plugins/active_record.rb