Module: Instana::Instrumentation::PostgreSQLAdapter

Defined in:
lib/instana/frameworks/instrumentation/postgresql_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 postgresql adapter. Only versions >= 3.1 are supported.



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

def self.included(klass)
  if (::ActiveRecord::VERSION::MAJOR == 3 && ::ActiveRecord::VERSION::MINOR > 0) ||
       ::ActiveRecord::VERSION::MAJOR >= 4

    # ActiveRecord 3.1 and up
    Instana::Util.method_alias(klass, :exec_query)
    Instana::Util.method_alias(klass, :exec_delete)

    @@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



27
28
29
30
31
32
33
34
35
# File 'lib/instana/frameworks/instrumentation/postgresql_adapter.rb', line 27

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



59
60
61
62
63
64
65
66
67
68
# File 'lib/instana/frameworks/instrumentation/postgresql_adapter.rb', line 59

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



48
49
50
51
52
53
54
55
56
57
# File 'lib/instana/frameworks/instrumentation/postgresql_adapter.rb', line 48

def exec_query_with_instana(sql, name = 'SQL', binds = [], *args)
  if !::Instana.tracer.tracing? || ignore_payload?(name, sql)
    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)


44
45
46
# File 'lib/instana/frameworks/instrumentation/postgresql_adapter.rb', line 44

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