Module: OneApm::Agent::Instrumentation::ActiveRecordHelper

Included in:
Sequel::OneApmInstrumentation
Defined in:
lib/one_apm/agent/database/active_record_helper.rb

Constant Summary collapse

DATABASE =
"Database".freeze
STATEMENT =
"statement".freeze
ALL =
"Database/all".freeze
ALLWEB =
"Database/allWeb".freeze
ALLOTHER =
"Database/allOther".freeze

Class Method Summary collapse

Class Method Details

.all(product) ⇒ Object

Database/statement/SqlType:10.128.6.33:3306/all



41
42
43
44
45
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 41

def all(product)
  met_info = [ DATABASE, STATEMENT]
  met_info << database_type(product)
  met_info.compact.join("/")
end

.database_info(config = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 102

def database_info config = {}
  info = []
  db_type, default_port = type_and_default_port_for_db config.fetch(:adapter, "SQL")
  info << db_type
  info << config.fetch(:host, '127.0.0.1')
  info << config.fetch(:port, default_port)
  #database need join with '/'
  "#{info.compact.uniq.join(":")}/#{config.fetch(:database, 'unknown')}"
end

.database_type(product) ⇒ Object



116
117
118
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 116

def database_type(product)
  product.split(':')[0]
end

.metric_for(product, operation, model_name = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 47

def metric_for(product, operation, model_name = nil)
  metrics = [
    operation_metric_for(product, operation, model_name),
    operation_all_metric_for(product, operation),
    product_rollup(product),
    rollup_metrics_for,
    all(product)
  ]
  metrics
end

.model_for_name(name) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 58

def model_for_name(name)
  return 'SQL' unless name && name.respond_to?(:split)
  parts = name.split(' ')
   if parts.size == 2
      parts.first
   else
     'SQL'
   end
end

.operation_all_metric_for(product, operation) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 23

def operation_all_metric_for(product, operation)
  met_info = [ DATABASE, STATEMENT]
  #without data_base_name
  met_info << without_database_name(product)
  met_info << operation
  met_info.compact.join("/")
end

.operation_metric_for(product, operation, model_name) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 15

def operation_metric_for(product, operation, model_name)
  met_info = [ DATABASE, STATEMENT]
  met_info << product
  met_info << model_name
  met_info << operation
  met_info.compact.join("/")
end

.operator_for_name(name) ⇒ Object



68
69
70
71
72
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 68

def operator_for_name(name)
  return unless name && name.include?(' ')
  parts = name.split(' ')
  return rename_for(parts.last.downcase) if parts.size == 2
end

.operator_for_sql(sql) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 92

def operator_for_sql(sql)
  txn = OneApm::Transaction.tl_current
  metric = txn && txn.database_metric_name
  operation = ""
  if metric.nil?
    operation = OneApm::Agent::Database.parse_operation_from_query(sql)
  end
  operation || "other"
end

.product_rollup(product) ⇒ Object



31
32
33
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 31

def product_rollup(product)
  "Database/#{STATEMENT}/#{without_database_name(product)}/all"
end

.rename_for(operation) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 74

def rename_for operation
  op_name = case operation.to_s
    when 'find', 'load', 'count', 'exists', 'all', 'get', 'select'
      'select'
    when 'destroy', 'delete'
      'delete'
    when 'create', 'insert'
      'insert'
    when 'update', 'save'
      'update'
    when 'other'
      'other'
    else
      nil
    end
  op_name
end

.rollup_metrics_forObject



35
36
37
38
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 35

def rollup_metrics_for
  return ALLWEB if OneApm::Transaction.recording_web_transaction?
  return ALLOTHER
end

.type_and_default_port_for_db(adapter) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 121

def type_and_default_port_for_db adapter
  type, port = case adapter
  when /mysql/      then [:MySQL, 3306]
  when /postgresql/ then [:PostgreSQL, 5432]
  when /oracle/     then [:Oracle, 1521]
  when /sqlserver/  then [:SQLSever, 1433]
  else [:Other, -1]
  end
end

.without_database_name(product) ⇒ Object



112
113
114
# File 'lib/one_apm/agent/database/active_record_helper.rb', line 112

def without_database_name(product)
  product.split('/')[0..-2].join('/')
end