class Sqreen::Ecosystem::Databases::Postgres

Public Instance Methods

setup() click to toggle source
# File lib/sqreen/ecosystem/databases/postgres.rb, line 18
def setup
  advice = wrap_for_interest(DatabaseConnectionData, &method(:after_advice))
  instrument 'PG::Connection#initialize', after: advice
end

Private Instance Methods

after_advice(call, _ball) click to toggle source

instance is of type PG::Connection > c = PG::Connection.new(host: '172.17.0.2', password: 'mysecretpassword', user: 'postgres')

=> #<PG::Connection:0x000055b44d077d10>

> %i{host port user db}.map { |m| c.send m }

=> ["172.17.0.2", 5432, "postgres", "postgres"]
# File lib/sqreen/ecosystem/databases/postgres.rb, line 30
def after_advice(call, _ball)
  conn = call.instance

  # build & submit signal
  signal = DatabaseConnectionData.new(transport: :postgres)

  host = conn.host
  if host
    if host.include?('/')
      signal.unix_socket = host
      signal.host = 'localhost'
      signal.ip = '::1'
    else
      signal.host = host
    end
  end

  signal.port = conn.port
  signal.username = conn.user
  signal.db = conn.db

  signal
end