Sha256: bf055b293e5300cddabba4ec00da1b355a92ff165aa8a533dbc63d889ea80889

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require 'active_support/notifications'

module Traxor
  module Rails
    module ActiveRecord
      COUNT_METRIC = 'rails.active_record.statements.count'
      SELECT_METRIC = 'rails.active_record.statements.select.count'
      INSERT_METRIC = 'rails.active_record.statements.insert.count'
      UPDATE_METRIC = 'rails.active_record.statements.update.count'
      DELETE_METRIC = 'rails.active_record.statements.delete.count'
      INSTANTIATION_METRIC = 'rails.active_record.instantiation.count'

      def self.record(event)
        sql = event.payload[:sql].to_s.strip.upcase
        name = event.payload[:name].to_s.strip
        return if name.casecmp('SCHEMA').zero?
        tags = {}
        tags[:active_record_class_name] = name.split.first if name.length.positive?

        Metric.count COUNT_METRIC, 1, tags
        Metric.count SELECT_METRIC, 1, tags if sql.start_with?('SELECT')
        Metric.count INSERT_METRIC, 1, tags if sql.start_with?('INSERT')
        Metric.count UPDATE_METRIC, 1, tags if sql.start_with?('UPDATE')
        Metric.count DELETE_METRIC, 1, tags if sql.start_with?('DELETE')
      end

      def self.record_instantiations(event)
        record_count = event.payload[:record_count].to_i
        tags = { active_record_class_name: event.payload[:class_name] }

        Metric.count INSTANTIATION_METRIC, record_count, tags if record_count.positive?
      end
    end
  end
end

if Traxor.enabled? && Traxor.scopes.include?(:active_record)
  ActiveSupport::Notifications.subscribe 'sql.active_record' do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    Traxor::Rails::ActiveRecord.record(event)
  end

  ActiveSupport::Notifications.subscribe 'instantiation.active_record' do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    Traxor::Rails::ActiveRecord.record_instantiations(event)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
traxor-0.1.20 lib/traxor/rails/active_record.rb
traxor-0.1.19 lib/traxor/rails/active_record.rb