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

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

Constant Summary collapse

ACTIVE_RECORD =
"ActiveRecord".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



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

def instrument_additional_methods
  instrument_save_methods
  instrument_relation_methods
end

.instrument_relation_methodsObject



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

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



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

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



124
125
126
127
128
129
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 124

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



171
172
173
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 171

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

.metrics_for(name, sql, adapter_name) ⇒ Object



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

def metrics_for(name, sql, adapter_name)
  product = map_product(adapter_name)
  splits = split_name(name)
  model = model_from_splits(splits) || product
  operation = operation_from_splits(splits, sql)

  TingYun::Agent::Datastore::MetricHelper.metrics_for(product, operation, model, ACTIVE_RECORD)
end

.model_from_splits(splits) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 92

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

.operation_from_splits(splits, sql) ⇒ Object



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

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



84
85
86
87
88
89
90
# File 'lib/ting_yun/instrumentation/support/active_record_helper.rb', line 84

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