Module: Instana::Instrumentation::Mysql2Adapter

Defined in:
lib/instana/frameworks/instrumentation/mysql2_adapter.rb

Constant Summary collapse

IGNORED_PAYLOADS =
%w(SCHEMA EXPLAIN CACHE).freeze
EXPLAINED_SQLS =
/\A\s*(with|select|update|delete|insert)\b/i

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

This module supports instrumenting ActiveRecord with the mysql2 adapter.



9
10
11
12
13
14
15
16
17
18
# File 'lib/instana/frameworks/instrumentation/mysql2_adapter.rb', line 9

def self.included(klass)
  # ActiveRecord 3.1 and up only (for now possibly)
  if ActiveRecord::VERSION::STRING > '3.0'
    Instana::Util.method_alias(klass, :exec_delete)
    Instana::Util.method_alias(klass, :exec_insert)
    Instana::Util.method_alias(klass, :exec_query)

    @@sanitize_regexp = Regexp.new('(\'[\s\S][^\']*\'|\d*\.\d+|\d+|NULL)', Regexp::IGNORECASE)
  end
end

Instance Method Details

#collect(sql) ⇒ Hash

Collect up this DB connection info for reporting.

Parameters:

  • sql (String)

Returns:

  • (Hash)

    Hash of collected KVs



25
26
27
28
29
30
31
32
33
# File 'lib/instana/frameworks/instrumentation/mysql2_adapter.rb', line 25

def collect(sql)
  payload = { :activerecord => {} }
  payload[:activerecord][:sql] = sql.gsub(@@sanitize_regexp, '?')
  payload[:activerecord][:adapter] = @config[:adapter]
  payload[:activerecord][:host] = @config[:host]
  payload[:activerecord][:db] = @config[:database]
  payload[:activerecord][:username] = @config[:username]
  payload
end

#exec_delete_with_instana(sql, name = nil, binds = []) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/instana/frameworks/instrumentation/mysql2_adapter.rb', line 46

def exec_delete_with_instana(sql, name = nil, binds = [])
  if !::Instana.tracer.tracing? || ignore_payload?(name, sql)
    return exec_delete_without_instana(sql, name, binds)
  end

  kv_payload = collect(sql)
  ::Instana.tracer.trace(:activerecord, kv_payload) do
    exec_delete_without_instana(sql, name, binds)
  end
end

#exec_insert_with_instana(sql, name = 'SQL', binds = [], *args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/instana/frameworks/instrumentation/mysql2_adapter.rb', line 57

def exec_insert_with_instana(sql, name = 'SQL', binds = [], *args)
  if !::Instana.tracer.tracing? || ignore_payload?(name, sql)
    return exec_insert_without_instana(sql, name, binds, *args)
  end

  kv_payload = collect(sql)
  ::Instana.tracer.trace(:activerecord, kv_payload) do
    exec_insert_without_instana(sql, name, binds, *args)
  end
end

#exec_query_with_instana(sql, name = 'SQL', binds = [], *args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/instana/frameworks/instrumentation/mysql2_adapter.rb', line 68

def exec_query_with_instana(sql, name = 'SQL', binds = [], *args)
  if !::Instana.tracer.tracing? || ignore_payload?(name, sql) ||
      ::Instana.tracer.current_span[:n] == :activerecord
    return exec_query_without_instana(sql, name, binds, *args)
  end

  kv_payload = collect(sql)
  ::Instana.tracer.trace(:activerecord, kv_payload) do
    exec_query_without_instana(sql, name, binds, *args)
  end
end

#ignore_payload?(name, sql) ⇒ Boolean

In the spirit of ::ActiveRecord::ExplainSubscriber.ignore_payload? There are only certain calls that we’re interested in tracing. e.g. No use to instrument framework caches.

Parameters:

  • payload (String)

Returns:

  • (Boolean)


42
43
44
# File 'lib/instana/frameworks/instrumentation/mysql2_adapter.rb', line 42

def ignore_payload?(name, sql)
  IGNORED_PAYLOADS.include?(name) || sql !~ EXPLAINED_SQLS
end