class StackifyRubyAPM::Spies::PostgresqlAdapterSpy

@api private

Constants

TYPE

Public Instance Methods

check_prepared_stmt(statement, payload) click to toggle source
# File lib/stackify_apm/spies/sinatra_activerecord/postgresql_adapter.rb, line 89
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
exec_delete(sql, name = nil, binds = []) click to toggle source
# File lib/stackify_apm/spies/sinatra_activerecord/postgresql_adapter.rb, line 41
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
exec_query(sql, name = 'SQL', binds = [], prepare: false) click to toggle source

rubocop:disable Lint/UnusedMethodArgument

# File lib/stackify_apm/spies/sinatra_activerecord/postgresql_adapter.rb, line 61
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
exec_update(sql, name = nil, binds = []) click to toggle source
# File lib/stackify_apm/spies/sinatra_activerecord/postgresql_adapter.rb, line 22
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
get_host() click to toggle source
# File lib/stackify_apm/spies/sinatra_activerecord/postgresql_adapter.rb, line 95
def get_host
  connection = self.raw_connection
  "#{connection.host}:#{connection.port}"
rescue StandardError => error
  nil
end
install() click to toggle source
# 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
query_variables(payload) click to toggle source

rubocop:enable Lint/UnusedMethodArgument

# File lib/stackify_apm/spies/sinatra_activerecord/postgresql_adapter.rb, line 81
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