Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 6c1f023278
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 79 lines (67 sloc) 2.564 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
# Setup to ignore any query which is not a SELECT, INSERT, UPDATE
# or DELETE and queries made by the own store.
RailsMetrics.ignore :invalid_queries do |name, payload|
  name == "active_record.sql" &&
    payload[:sql] !~ /^(SELECT|INSERT|UPDATE|DELETE)/
end

module RailsMetrics
  module ORM
    # Include in your model to store metrics. For ActiveRecord, you need the
    # following setup:
    #
    # script/generate model Metric script/generate name:string duration:integer
    # request_id:integer parent_id:integer payload:text started_at:datetime created_at:datetime --skip-timestamps
    #
    # You can use any model name you wish. Next, you need to include
    # RailsMetrics::ORM::ActiveRecord:
    #
    # class Metric < ActiveRecord::Base
    # include RailsMetrics::ORM::ActiveRecord
    # end
    #

    ORM.primary_key_finder = :find
    ORM.delete_all = :delete_all

    ORM.metric_model_properties = %w[
name:string
duration:integer
request_id:integer
parent_id:integer
payload:text
started_at:datetime
created_at:datetime
]

    def self.add_metric_model_config(generator, file_name, class_name)
      generator.inject_into_class "app/models/#{file_name}.rb", class_name, <<-CONTENT
include RailsMetrics::ORM::#{Rails::Generators.options[:rails][:orm].to_s.camelize}
CONTENT
    end

    module ActiveRecord
      extend ActiveSupport::Concern
      include RailsMetrics::Store

      included do
        # Create a new connection pool just for the given resource
        establish_connection(Rails.env)

        # Set required validations
        validates_presence_of :name, :started_at, :duration

        # Serialize payload data
        serialize :payload

        # Select scopes
        scope :requests, where(:name => "rack.request")
        scope :by_name, lambda { |name| where(:name => name) }
        scope :by_request_id, lambda { |request_id| where(:request_id => request_id) }

        # Order scopes
        # We need to add the id in the earliest and latest scope since the database
        # does not store miliseconds. The id then comes as second criteria, since
        # the ones started first are first saved in the database.
        scope :earliest, order("started_at ASC, id ASC")
        scope :latest, order("started_at DESC, id DESC")
        scope :slowest, order("duration DESC")
        scope :fastest, order("duration ASC")
      end

    protected

      def save_metric!
        save!
      end
    end
  end
end
Something went wrong with that request. Please try again.