Class: StackifyRubyAPM::Spies::SequelSpy Private

Inherits:
Object
  • Object
show all
Defined in:
lib/stackify_apm/spies/sequel.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

API:

  • private

Constant Summary collapse

DEFAULT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

API:

  • private

'SQL'.freeze
TYPE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

API:

  • private

'db.sequel.sql'.freeze
TABLE_REGEX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

API:

  • private

%{["'`]?([A-Za-z0-9]+)}.freeze
REGEXES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

API:

  • private

{
  /^BEGIN/i => 'BEGIN',
  /^COMMIT/i => 'COMMIT',
  /^SELECT .* FROM #{TABLE_REGEX}/i => 'SELECT FROM ',
  /^INSERT INTO #{TABLE_REGEX}/i => 'INSERT INTO ',
  /^UPDATE #{TABLE_REGEX}/i => 'UPDATE ',
  /^DELETE FROM #{TABLE_REGEX}/i => 'DELETE FROM '
}.freeze
FORMAT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

API:

  • private

'%s%s'.freeze

Instance Method Summary collapse

Instance Method Details

#installObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/stackify_apm/spies/sequel.rb', line 26

def install
  require 'sequel/database/logging'

  ::Sequel::Database.class_eval do
    alias_method 'log_connection_yield_without_apm', 'log_connection_yield'

    def log_connection_yield(sql, *args, **kwargs, &block)
      return log_connection_yield_without_apm(sql, *args, **kwargs, &block) unless StackifyRubyAPM.current_transaction

      name = summarize sql
      db_adapter = ''
      db_adapter = args[0].class.to_s.gsub('::Database', '').downcase if args[0]
      payload = {sql: sql, db_adapter: db_adapter, binds: defined?(args[1]) ? args[1]: []}
      statement = query_variables(payload)
      check_prepared_stmt(statement, payload)
      ctx = Span::Context.new(statement)

      StackifyRubyAPM.span(name, TYPE, context: ctx, &block)
    end

    # rubocop:disable Lint/UselessAssignment
    def summarize(sql)
      current_sql ||=
        REGEXES.find do |regex, sig|
          if (match = sql.match(regex))
            break format(FORMAT, sig, match[1] && match[1].gsub(/["']/, ''))
          end
        end || DEFAULT
    end

    def query_variables(payload)
      props = get_common_db_properties
      props[:PROVIDER] = get_profiler(payload[:db_adapter])
      props[:SQL] = payload[:sql]
      props
    end

    def check_prepared_stmt(statement, payload)
      if StackifyRubyAPM.agent.config.prefix_enabled
        case get_profiler(payload[:db_adapter])
        when 'generic', 'mysql', 'sqlite', 'oracle', 'db2'
          # We check the placeholder of sequel mysql by ? and sometimes sequel sqlite is using : or ?
          if check_prepared_stmt_by_placeholder(payload[:sql].include?('?'), statement, payload)
          else check_prepared_stmt_by_placeholder(payload[:sql].include?(':'), statement, payload)
          end
        when 'postgresql'
          check_prepared_stmt_by_placeholder(!!payload[:sql].match(/\$\d/), statement, payload)
        end
      end
    end
  end
  # rubocop:enable Lint/UselessAssignment
end