Module: SqlMetrics

Defined in:
lib/sql_metrics.rb,
lib/sql_metrics/version.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
"0.1.6"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



31
32
33
# File 'lib/sql_metrics.rb', line 31

def configuration
  @configuration
end

Class Method Details

.build_psql_query(name, properties) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sql_metrics.rb', line 64

def build_psql_query(name, properties)
  "INSERT INTO #{SqlMetrics.configuration.event_table_name} (
    created_at,
    name,
    properties
  ) VALUES (
    '#{Time.now.utc}',
    '#{name}',
    '#{properties.to_json}'
  );"
end

.configure {|configuration| ... } ⇒ Object

Yields:



81
82
83
# File 'lib/sql_metrics.rb', line 81

def self.configure
  yield(configuration) if block_given?
end

.merge_request_and_options_into_properties(properties, request, options) ⇒ Object



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
# File 'lib/sql_metrics.rb', line 33

def merge_request_and_options_into_properties(properties, request, options)
  if request
    properties[:user_agent] = request.user_agent
    properties[:session_id] = request.session_options[:id]
    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[:referrer] = 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_connectionObject



98
99
100
101
102
103
104
105
106
# File 'lib/sql_metrics.rb', line 98

def self.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(name, properties) ⇒ Object



60
61
62
# File 'lib/sql_metrics.rb', line 60

def send_async_query(name, properties)
  pg_connection.send_query(build_psql_query(name, properties))
end

.track(name, properties = {}, request = nil, options = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/sql_metrics.rb', line 85

def self.track(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[:user_agent] and properties[:user_agent].match(SqlMetrics.configuration.bots_regex)
  end

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