Module: RailsMetrics::PayloadParser

Defined in:
lib/rails_metrics/payload_parser.rb

Overview

ActiveSupport::Notifications usually comes with extra information as the SQL query, response status and many others. This information is called payload.

By default, RailsMetrics stores the whole payload in the database but it allows you to manipulate it or even ignore some through the add and ignore methods.

For example, “activerecord.sql” has as paylaod a hash with :name (like “Product Load”), the :sql to be performed and the :connection_id. We can remove the connection from the hash by simply providing :except:

RailsMetrics::PayloadParser.add "active_record.sql", :except => :name

Or, we could also:

RailsMetrics::PayloadParser.add "active_record.sql", :slice => [:name, :sql]

Finally, in some cases manipulating the hash is not enough and you might need to customize it further, as in “action_view.render_template”. You can do that by giving a block which will receive the payload as argument:

RailsMetrics::PayloadParser.add "action_view.render_template" do |payload|
  payload = payload.dup
  payload[:template] = payload[:template].gsub("RAILS_ROOT", Rails.root)
  payload
end

ATTENTION: if you need to modify the payload or any of its values, be sure to .dup if first, as in the example above.

If you want to ignore any payload, you can use the ignore method:

RailsMetrics::PayloadParser.ignore "active_record.sql"

Class Method Summary collapse

Class Method Details

.add(*names, &block) ⇒ Object

Add a new parser.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rails_metrics/payload_parser.rb', line 47

def self.add(*names, &block)
  options = names.extract_options!

  names.each do |name|
    parsers[name.to_s] = if block_given?
      block
    elsif options.present?
      options.to_a.flatten
    else
      true
    end
  end
end

.filter(name, payload) ⇒ Object

Filter the given payload based on the name given and configured parsers



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rails_metrics/payload_parser.rb', line 67

def self.filter(name, payload)
  parser = parsers[name]
  case parser
  when Array
    payload.send(*parser)
  when Proc
    parser.call(payload)
  when TrueClass, NilClass
    payload
  when FalseClass
    nil
  end
end

.ignore(*names) ⇒ Object

Delete a previous parser



62
63
64
# File 'lib/rails_metrics/payload_parser.rb', line 62

def self.ignore(*names)
  names.each { |name| parsers[name.to_s] = false }
end

.mapped_pathsObject

Holds the mapped paths used in prunning.



42
43
44
# File 'lib/rails_metrics/payload_parser.rb', line 42

def self.mapped_paths
  @mapped_paths ||= {}
end

.parsersObject

Holds the parsers used by RailsMetrics.



37
38
39
# File 'lib/rails_metrics/payload_parser.rb', line 37

def self.parsers
  @parsers ||= {}
end

.prune_path(raw_path) ⇒ Object

Prune paths based on the mapped paths set.



82
83
84
85
86
87
# File 'lib/rails_metrics/payload_parser.rb', line 82

def self.prune_path(raw_path)
  mapped_paths.each do |path, replacement|
    raw_path = raw_path.gsub(path, replacement)
  end
  raw_path
end