Module: SqlMetrics
- Defined in:
- lib/sql_metrics.rb,
lib/sql_metrics/version.rb
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- VERSION =
"0.1.3"
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
Class Method Summary collapse
- .build_psql_query(name, properties) ⇒ Object
- .configure {|configuration| ... } ⇒ Object
- .merge_request_into_properties(properties, request) ⇒ Object
- .pg_connection ⇒ Object
- .send_async_query(name, properties) ⇒ Object
- .track(name, properties = {}, request = nil, options = nil) ⇒ Object
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
30 31 32 |
# File 'lib/sql_metrics.rb', line 30 def configuration @configuration end |
Class Method Details
.build_psql_query(name, properties) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sql_metrics.rb', line 61 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
78 79 80 |
# File 'lib/sql_metrics.rb', line 78 def self.configure yield(configuration) if block_given? end |
.merge_request_into_properties(properties, request) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sql_metrics.rb', line 32 def merge_request_into_properties(properties, request) if request properties[:user_agent] = request.user_agent properties[:session_id] = request.[:id] properties[:remote_ip] = request.remote_ip 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 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_connection ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/sql_metrics.rb', line 95 def self.pg_connection PGconn.open(:dbname => SqlMetrics.configuration.db_name, :host => SqlMetrics.configuration.host, :port => SqlMetrics.configuration.port, :options => SqlMetrics.configuration., :tty => SqlMetrics.configuration.tty, :user => SqlMetrics.configuration.user, :password => SqlMetrics.configuration.password) end |
.send_async_query(name, properties) ⇒ Object
57 58 59 |
# File 'lib/sql_metrics.rb', line 57 def send_async_query(name, properties) pg_connection.send_query(build_psql_query(name, properties)) end |
.track(name, properties = {}, request = nil, options = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/sql_metrics.rb', line 82 def self.track(name, properties = {}, request = nil, = nil) properties = merge_request_into_properties(properties, request) unless and [: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 |