Module: DatabaseHelper

Defined in:
lib/stackify_apm/helper/database_helper.rb

Overview

get_profiler - It will return the database driver of the Object.

Instance Method Summary collapse

Instance Method Details

#check_prepared_stmt_by_placeholder(placeholder, statement, payload) ⇒ Object

Check the prepared statement by placeholder if its valid placeholder - contains the prepared statement pattern.

Placeholder for mysql, sqlite, jdbc oracle, db2 is ?, example "SELECT * FROM posts WHERE author = ? and id = ?"
Placeholder for postgres is $1, $2,...$n, example "SELECT * FROM posts WHERE author = $1 and id = $2"

statement - contains the db properties such as SQL, PROVIDER, etc.

So if there's payload value we append PREFIX_SQL_PARAMETERS to the existing object properties.

payload - contains the payload which is the payload value/data of the placeholder.

Example payload: {:sql=>"SELECT * FROM posts WHERE author = ? and id = ?", :db_adapter=>"mysql2::client", :binds=>["J.K. Rowling", 1]}


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stackify_apm/helper/database_helper.rb', line 37

def check_prepared_stmt_by_placeholder(placeholder, statement, payload)
  sqlParam = []
  unless payload[:binds].nil?
    payload[:binds].each_with_index do |record, idx|
      if record && defined?(record.value)
        StackifyRubyAPM::Util.pushToAryIndex(sqlParam, idx, record.value[0..999])
      elsif ((record && record.instance_of?(Array)) && defined?(record[1]))
        StackifyRubyAPM::Util.pushToAryIndex(sqlParam, idx, record[1][0..999])
      else
        StackifyRubyAPM::Util.pushToAryIndex(sqlParam, idx, record[0..999])
      end
    end
    statement[:PREFIX_SQL_PARAMETERS] = sqlParam[0..99].to_json if sqlParam.count > 0
    statement[:PREFIX_SQL_PARAMETER_COUNT] = sqlParam.length.to_s if sqlParam.count > 0
    true
  else
    false
  end
end

#get_common_db_propertiesObject

Common DB span properties shared to sequel, activerecord, etc.



58
59
60
61
62
63
64
65
66
# File 'lib/stackify_apm/helper/database_helper.rb', line 58

def get_common_db_properties
  {
    CATEGORY: 'Database',
    SUBCATEGORY: 'Execute',
    COMPONENT_CATEGORY: 'DB Query',
    COMPONENT_DETAIL: 'Execute SQL Query',
    PROVIDER: 'generic'
  }
end

#get_profiler(driver) ⇒ Object

return back valid PROVIDER based on driver name passed in rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/stackify_apm/helper/database_helper.rb', line 9

def get_profiler(driver)
  if driver.to_s.empty?
    'generic'
  elsif driver.include? 'mysql'
    'mysql'
  elsif driver.include? 'postgres'
    'postgresql'
  elsif driver.include? 'oci8'
    'oracle'
  elsif driver.include? 'db2'
    'db2'
  elsif driver.include? 'sqlite'
    'sqlite'
  else
    'generic'
  end
end