Module: TingYun::Instrumentation::Support::ActiveRecordHelper

Defined in:
lib/ting_yun/instrumentation/support/active_record_helper.rb

Constant Summary collapse

ACTIVE_RECORD =
"ActiveRecord".freeze
DATA_MAPPER =
"DataMapper".freeze
SPACE =
' '.freeze
EMPTY =
[].freeze
OPERATION_NAMES =

These are used primarily to optimize and avoid allocation on well known operations coming in. Anything not matching the list is fine, it just needs to get downcased directly for use.

{
    'Find' => 'SELECT',
    'Load' => 'SELECT',
    'Count' => 'SELECT',
    'Exists' => 'SELECT',
    'Create' => 'INSERT',
    'Columns' => 'SELECT',
    'Indexes' => 'SELECT',
    'Destroy' => 'DELETE',
    'Update' => 'UPDATE',
    'Save' => 'INSERT'
}.freeze

Class Method Summary collapse

Class Method Details

.instrument_additional_methodsObject

Used by both the AR 3.x and 4.x instrumentation



18
19
20
21
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 18

def instrument_additional_methods
  instrument_save_methods
  instrument_relation_methods
end

.instrument_relation_methodsObject



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
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 43

def instrument_relation_methods

  ::ActiveRecord::Relation.class_eval do
    alias_method :update_all_without_tingyun, :update_all

    def update_all(*args, &blk)
      ::TingYun::Agent.with_database_metric_name(self.name, nil, ACTIVE_RECORD) do
        update_all_without_tingyun(*args, &blk)
      end
    end

    alias_method :delete_all_without_tingyun, :delete_all

    def delete_all(*args, &blk)
      ::TingYun::Agent.with_database_metric_name(self.name, nil, ACTIVE_RECORD) do
        delete_all_without_tingyun(*args, &blk)
      end
    end

    alias_method :destroy_all_without_tingyun, :destroy_all

    def destroy_all(*args, &blk)
      ::TingYun::Agent.with_database_metric_name(self.name, nil, ACTIVE_RECORD) do
        destroy_all_without_tingyun(*args, &blk)
      end
    end
  end
end

.instrument_save_methodsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 23

def instrument_save_methods
  ::ActiveRecord::Base.class_eval do
    alias_method :save_without_tingyun, :save

    def save(*args, &blk)
      ::TingYun::Agent.with_database_metric_name(self.class.name, nil, ACTIVE_RECORD) do
        save_without_tingyun(*args, &blk)
      end
    end

    alias_method :save_without_tingyun!, :save!

    def save!(*args, &blk)
      ::TingYun::Agent.with_database_metric_name(self.class.name, nil, ACTIVE_RECORD) do
        save_without_tingyun!(*args, &blk)
      end
    end
  end
end

.map_operation(raw_operation) ⇒ Object



139
140
141
142
143
144
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 139

def map_operation(raw_operation)
  direct_op = OPERATION_NAMES[raw_operation]
  return direct_op if direct_op

  # raw_operation.upcase
end

.map_product(adapter_name) ⇒ Object



188
189
190
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 188

def map_product(adapter_name)
  PRODUCT_NAMES.fetch(adapter_name, DEFAULT_PRODUCT_NAME)
end

.metrics_for(name, sql, config) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 72

def metrics_for(name, sql, config)
  config ||={}
  product = map_product(config[:adapter])
  splits = split_name(name)
  model = model_from_splits(splits) || product
  operation = operation_from_splits(splits, sql)

  TingYun::Agent::Datastore::MetricHelper.metrics_for(product, operation, config[:host], config[:port], config[:database], model, ACTIVE_RECORD)
end

.metrics_for_data_mapper(name, sql, config, model = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 82

def metrics_for_data_mapper(name, sql, config, model=nil)
  if config
    product = map_product(config.query['adapter'])
    operation = name || TingYun::Instrumentation::Support::Database.parse_operation_from_query(sql)
    model ||= product
    db = config.query['database'] || config.path.split('/').last
    host = config.host
    port = config.port
    host = nil if config.host && config.host.empty?
    port = nil if config.host && config.host.empty?
    TingYun::Agent::Datastore::MetricHelper.metrics_for(product, operation, host, port, db, model, DATA_MAPPER)
  end
end

.model_from_splits(splits) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 107

def model_from_splits(splits)
  if splits.length == 2
    splits.first
  else
    nil
  end
end

.operation_from_splits(splits, sql) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 115

def operation_from_splits(splits, sql)
  if splits.length == 2
    map_operation(splits[1])
  else
    TingYun::Instrumentation::Support::Database.parse_operation_from_query(sql)
  end
end

.split_name(name) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 99

def split_name(name)
  if name && name.respond_to?(:split)
    name.split(SPACE)
  else
    EMPTY
  end
end