Module: NewRelic::Agent::Instrumentation::ActiveRecordHelper

Defined in:
lib/new_relic/agent/instrumentation/active_record_helper.rb

Class Method Summary collapse

Class Method Details

.metric_for_name(name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 10

def metric_for_name(name)
  return unless name
  parts = name.split(' ')
  if parts.size == 2
    model = parts.first
    operation = parts.last.downcase
    case operation
    when 'load', 'count', 'exists'
      op_name = 'find'
    when 'indexes', 'columns'
      op_name = nil # fall back to DirectSQL
    when 'destroy', 'find', 'save', 'create'
      op_name = operation
    when 'update'
      op_name = 'save'
    else
      if model == 'Join'
        op_name = operation
      end
    end
    "ActiveRecord/#{model}/#{op_name}" if op_name
  end
end

.metric_for_sql(sql) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 34

def metric_for_sql(sql)
  metric = NewRelic::Agent::Instrumentation::MetricFrame.database_metric_name
  if metric.nil?
    if sql =~ /^(select|update|insert|delete|show)/i
      # Could not determine the model/operation so let's find a better
      # metric.  If it doesn't match the regex, it's probably a show
      # command or some DDL which we'll ignore.
      metric = "Database/SQL/#{$1.downcase}"
    else
      metric = "Database/SQL/other"
    end
  end
  metric
end

.remote_service_metric(adapter, host) ⇒ Object

Given a database adapter name and a database server host this returns a metric name in the form: “RemoteService/sql/adapter/host” Host defaults to “localhost”.



64
65
66
67
68
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 64

def remote_service_metric(adapter, host)
  host ||= 'localhost'
  type = adapter.sub(/\d*/, '')
  "RemoteService/sql/#{type}/#{host}"
end

.rollup_metrics_for(metric) ⇒ Object

Given a metric name such as “ActiveRecord/model/action” this returns an array of rollup metrics:

“ActiveRecord/all”, “ActiveRecord/action”

If the metric name is in the form of “ActiveRecord/action” this returns merely: [ “ActiveRecord/all” ]



54
55
56
57
58
# File 'lib/new_relic/agent/instrumentation/active_record_helper.rb', line 54

def rollup_metrics_for(metric)
  metrics = ["ActiveRecord/all"]
  metrics << "ActiveRecord/#{$1}" if metric =~ /ActiveRecord\/[\w|\:]+\/(\w+)/
  metrics
end