Class: StackifyRubyAPM::Spies::PostgresqlAdapterSpy Private

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

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.

Constant Summary collapse

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.

'db.sinatra_active_record.sql'.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.



16
17
18
19
20
21
22
23
24
25
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/stackify_apm/spies/sinatra_activerecord/postgresql_adapter.rb', line 16

def install
  ActiveRecord::ConnectionAdapters::PostgreSQL::DatabaseStatements.class_eval do
    alias_method 'exec_query_without_apm', 'exec_query'
    alias_method 'exec_delete_without_apm', 'exec_delete'
    alias_method 'exec_update_without_apm', 'exec_update'

    def exec_update(sql, name = nil, binds = [])
      result = nil

      unless StackifyRubyAPM.current_transaction
        exec_update_without_apm(sql, name, binds)
      end

      payload = {sql: sql, binds: binds}
      statement = query_variables(payload)
      check_prepared_stmt(statement, payload)

      ctx = Span::Context.new(statement)
      result = exec_update_without_apm(sql, name, binds)

      StackifyRubyAPM.span name, TYPE, context: ctx do
        return result
      end
    end

    def exec_delete(sql, name = nil, binds = [])
      result = nil

      unless StackifyRubyAPM.current_transaction
        exec_delete_without_apm(sql, name, binds)
      end

      payload = {sql: sql, binds: binds}
      statement = query_variables(payload)
      check_prepared_stmt(statement, payload)

      ctx = Span::Context.new(statement)
      result = exec_delete_without_apm(sql, name, binds)

      StackifyRubyAPM.span name, TYPE, context: ctx do
        return result
      end
    end

    # rubocop:disable Lint/UnusedMethodArgument
    def exec_query(sql, name = 'SQL', binds = [], prepare: false)
      result = nil

      unless StackifyRubyAPM.current_transaction
        exec_query_without_apm(sql, name, binds)
      end

      payload = {sql: sql, binds: binds}
      statement = query_variables(payload)
      check_prepared_stmt(statement, payload)

      ctx = Span::Context.new(statement)
      result = exec_query_without_apm(sql, name, binds)

      StackifyRubyAPM.span name, TYPE, context: ctx do
        return result
      end
    end
    # rubocop:enable Lint/UnusedMethodArgument

    def query_variables(payload)
      props = get_common_db_properties
      props[:PROVIDER] = 'postgresql'
      props[:SQL] = payload[:sql]
      props[:URL] = get_host unless !get_host
      props
    end

    def check_prepared_stmt(statement, payload)
      if StackifyRubyAPM.agent.config.prefix_enabled
        check_prepared_stmt_by_placeholder(!!payload[:sql].match(/\$\d/), statement, payload)
      end
    end

    def get_host
      connection = self.raw_connection
      "#{connection.host}:#{connection.port}"
    rescue StandardError => error
      nil
    end
  end
end