module SqlMetrics

Constants

VERSION

Attributes

configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/sql_metrics.rb, line 16
def self.configure
  yield(configuration) if block_given?
end
create_events_table() click to toggle source
# File lib/sql_metrics/sql_metrics.rb, line 15
def create_events_table
  conn = pg_connection

  conn.exec("CREATE TABLE #{SqlMetrics.configuration.event_table_name} (created_at timestamp, name varchar(200), properties json);")
end
track(name, properties = {}, request = nil, options = nil) click to toggle source
# File lib/sql_metrics/sql_metrics.rb, line 3
def track(name, properties = {}, request = nil, options = nil)
  created_at = Time.now.utc

  Thread.new do
    track_now(created_at, name, properties, request, options)
  end
end
track_user(user) click to toggle source
# File lib/sql_metrics/sql_metrics.rb, line 11
def track_user(user)

end

Private Class Methods

build_psql_query(created_at, name, properties) click to toggle source
# File lib/sql_metrics/sql_metrics.rb, line 80
def build_psql_query(created_at, name, properties)
  "INSERT INTO #{SqlMetrics.configuration.event_table_name} (
    created_at,
    name,
    properties
  ) VALUES (
    '#{created_at}',
    '#{name}',
    '#{properties.to_json}'
  );"
end
merge_request_and_options_into_properties(properties, request, options) click to toggle source
# File lib/sql_metrics/sql_metrics.rb, line 37
def merge_request_and_options_into_properties(properties, request, options)
  if request
    properties[:user_agent] = request.user_agent

    if properties[:user_agent] and properties[:user_agent].match(SqlMetrics.configuration.bots_regex)
      properties[:is_bot] = true
    else
      properties[:is_bot] = false
    end

    if warden_user_key = request.session["warden.user.user.key"]
      properties[:current_user_id] = warden_user_key.first.first
    end

    properties[:session_id] = request.session_options[:id]
    properties[:query_parameters] = request.query_parameters
    properties[:remote_ip] = request.remote_ip

    unless options and options[:geo_lookup] == false
      if properties[:remote_ip] and geo_object = Geocoder.search(properties[:remote_ip]).first
        properties[:remote_city] = geo_object.city
        properties[:remote_country] = geo_object.country
        properties[:remote_country_code] = geo_object.country_code
        properties[:remote_coordinates] = geo_object.coordinates
      end
    end

    properties[:referer] = request.referer
    referer = Addressable::URI.parse(request.referer)
    properties[:referrer_host] = referer.host if referer

    properties[:requested_url] = request.fullpath
    fullpath = Addressable::URI.parse(request.fullpath)
    properties[:requested_url_host] = fullpath.host if fullpath
  end

  properties
end
pg_connection() click to toggle source
# File lib/sql_metrics/sql_metrics.rb, line 92
def pg_connection
  PGconn.open(:dbname => SqlMetrics.configuration.db_name,
              :host => SqlMetrics.configuration.host,
              :port => SqlMetrics.configuration.port,
              :options => SqlMetrics.configuration.options,
              :tty => SqlMetrics.configuration.tty,
              :user => SqlMetrics.configuration.user,
              :password => SqlMetrics.configuration.password)
end
send_async_query(created_at, name, properties) click to toggle source
# File lib/sql_metrics/sql_metrics.rb, line 76
def send_async_query(created_at, name, properties)
  pg_connection.send_query(build_psql_query(created_at, name, properties))
end
track_now(created_at, name, properties = {}, request = nil, options = nil) click to toggle source
# File lib/sql_metrics/sql_metrics.rb, line 23
def track_now(created_at, name, properties = {}, request = nil, options = nil)
  properties = merge_request_and_options_into_properties(properties, request, options)

  unless options and options[:filter_bots] == false
    return false if properties[:is_bot]
  end

  send_async_query(created_at, name, properties)

rescue => e
  SqlMetrics.configuration.logger.error e
  SqlMetrics.configuration.logger.error e.backtrace.join("\n")
end