Module: Instana::Instrumentation::MysqlAdapter

Defined in:
lib/instana/frameworks/instrumentation/mysql_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
# File 'lib/instana/frameworks/instrumentation/mysql_adapter.rb', line 9

def self.included(klass)
  if ActiveRecord::VERSION::STRING >= '3.2'
    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



22
23
24
25
26
27
28
29
30
# File 'lib/instana/frameworks/instrumentation/mysql_adapter.rb', line 22

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_query_with_instana(sql, name = 'SQL', binds = [], *args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/instana/frameworks/instrumentation/mysql_adapter.rb', line 43

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)


39
40
41
# File 'lib/instana/frameworks/instrumentation/mysql_adapter.rb', line 39

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