sql_metrics 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -5
- data/Rakefile +4 -0
- data/lib/sql_metrics/config.rb +27 -0
- data/lib/sql_metrics/railtie.rb +10 -0
- data/lib/sql_metrics/sql_metrics.rb +86 -0
- data/lib/sql_metrics/version.rb +1 -1
- data/lib/sql_metrics.rb +4 -92
- data/lib/tasks/sql_metrics.rake +13 -0
- data/sql_metrics.gemspec +4 -4
- metadata +21 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 95e9da225dbae5b2d9816beeb79056815ca21447
|
4
|
+
data.tar.gz: 3ad9ad3f140f648ef17e2179504134ef3a1efb60
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 08ea15e904fb3af8da2acf01383bf496273f6bad4929e86ec8552b1c6c681dbb4b9cd90a5c3fcbf431e5109d2db31784bd65806c7da2f5b243767d60e9801620
|
7
|
+
data.tar.gz: b9d88e8b958a0d701933c1a0e9ee77895bd566fc05e398e31cd3725b095a518ed0f6dec9602556d36b9b9364d6526620414b62c0f4602e8cbd4ec30ae31dece7
|
data/README.md
CHANGED
@@ -36,11 +36,7 @@ Or install it yourself as:
|
|
36
36
|
|
37
37
|
### Setup database and table
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
CREATE TABLE events (created_at timestamp, name varchar(200), properties json);
|
42
|
-
|
43
|
-
Now you need to tell the gem how to connect to your db. So simply create a file called sql_metrics.rb into your config/libs folder with the config:
|
39
|
+
Tell the gem how to connect to your db. So simply create a file called sql_metrics.rb into your config/libs folder with the config:
|
44
40
|
|
45
41
|
SqlMetrics.configure do |config|
|
46
42
|
config.host = '127.0.0.1'
|
@@ -49,6 +45,10 @@ Now you need to tell the gem how to connect to your db. So simply create a file
|
|
49
45
|
config.password = 'my_password'
|
50
46
|
end
|
51
47
|
|
48
|
+
Then to create the required events table just run this rake task:
|
49
|
+
|
50
|
+
rake sql_metrics:create_events_table
|
51
|
+
|
52
52
|
### Track a event
|
53
53
|
|
54
54
|
A simple event can look like this:
|
data/Rakefile
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
module SqlMetrics
|
2
|
+
class Configuration
|
3
|
+
require 'pg'
|
4
|
+
require 'logger'
|
5
|
+
require 'json'
|
6
|
+
require 'geocoder'
|
7
|
+
|
8
|
+
attr_accessor :host, :port, :options, :tty, :db_name, :user, :password, :database_schema, :event_table_name,
|
9
|
+
:bots_regex, :logger
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
self.host = nil
|
13
|
+
self.port = 5432
|
14
|
+
self.options = nil
|
15
|
+
self.tty = nil
|
16
|
+
self.db_name = nil
|
17
|
+
self.user = nil
|
18
|
+
self.password = nil
|
19
|
+
self.database_schema = 'public'
|
20
|
+
self.event_table_name = 'events'
|
21
|
+
|
22
|
+
self.bots_regex = /Googlebot|Pingdom|bing|Yahoo|Amazon|Twitter|Yandex|majestic12/i
|
23
|
+
|
24
|
+
self.logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
module SqlMetrics
|
2
|
+
class << self
|
3
|
+
def track(name, properties = {}, request = nil, options = nil)
|
4
|
+
created_at = Time.now.utc
|
5
|
+
|
6
|
+
Thread.new do
|
7
|
+
track_now(created_at, name, properties, request, options)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def create_events_table
|
12
|
+
conn = pg_connection
|
13
|
+
|
14
|
+
conn.exec("CREATE TABLE #{SqlMetrics.configuration.event_table_name} (created_at timestamp, name varchar(200), properties json);")
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def track_now(created_at, name, properties = {}, request = nil, options = nil)
|
20
|
+
properties = merge_request_and_options_into_properties(properties, request, options)
|
21
|
+
|
22
|
+
unless options and options[:filter_bots] == false
|
23
|
+
return false if properties[:user_agent] and properties[:user_agent].match(SqlMetrics.configuration.bots_regex)
|
24
|
+
end
|
25
|
+
|
26
|
+
send_async_query(created_at, name, properties)
|
27
|
+
|
28
|
+
rescue => e
|
29
|
+
SqlMetrics.configuration.logger.error e
|
30
|
+
SqlMetrics.configuration.logger.error e.backtrace.join("\n")
|
31
|
+
end
|
32
|
+
|
33
|
+
def merge_request_and_options_into_properties(properties, request, options)
|
34
|
+
if request
|
35
|
+
properties[:user_agent] = request.user_agent
|
36
|
+
properties[:session_id] = request.session_options[:id]
|
37
|
+
properties[:remote_ip] = request.remote_ip
|
38
|
+
|
39
|
+
unless options and options[:geo_lookup] == false
|
40
|
+
if properties[:remote_ip] and geo_object = Geocoder.search(properties[:remote_ip]).first
|
41
|
+
properties[:remote_city] = geo_object.city
|
42
|
+
properties[:remote_country] = geo_object.country
|
43
|
+
properties[:remote_country_code] = geo_object.country_code
|
44
|
+
properties[:remote_coordinates] = geo_object.coordinates
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
properties[:referer] = request.referer
|
49
|
+
referer = Addressable::URI.parse(request.referer)
|
50
|
+
properties[:referrer_host] = referer.host if referer
|
51
|
+
|
52
|
+
properties[:requested_url] = request.fullpath
|
53
|
+
fullpath = Addressable::URI.parse(request.fullpath)
|
54
|
+
properties[:requested_url_host] = fullpath.host if fullpath
|
55
|
+
end
|
56
|
+
|
57
|
+
properties
|
58
|
+
end
|
59
|
+
|
60
|
+
def send_async_query(created_at, name, properties)
|
61
|
+
pg_connection.send_query(build_psql_query(created_at, name, properties))
|
62
|
+
end
|
63
|
+
|
64
|
+
def build_psql_query(created_at, name, properties)
|
65
|
+
"INSERT INTO #{SqlMetrics.configuration.event_table_name} (
|
66
|
+
created_at,
|
67
|
+
name,
|
68
|
+
properties
|
69
|
+
) VALUES (
|
70
|
+
'#{created_at}',
|
71
|
+
'#{name}',
|
72
|
+
'#{properties.to_json}'
|
73
|
+
);"
|
74
|
+
end
|
75
|
+
|
76
|
+
def pg_connection
|
77
|
+
PGconn.open(:dbname => SqlMetrics.configuration.db_name,
|
78
|
+
:host => SqlMetrics.configuration.host,
|
79
|
+
:port => SqlMetrics.configuration.port,
|
80
|
+
:options => SqlMetrics.configuration.options,
|
81
|
+
:tty => SqlMetrics.configuration.tty,
|
82
|
+
:user => SqlMetrics.configuration.user,
|
83
|
+
:password => SqlMetrics.configuration.password)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/lib/sql_metrics/version.rb
CHANGED
data/lib/sql_metrics.rb
CHANGED
@@ -1,77 +1,12 @@
|
|
1
1
|
require "sql_metrics/version"
|
2
|
+
require "sql_metrics/config"
|
3
|
+
require "sql_metrics/sql_metrics"
|
2
4
|
|
3
|
-
|
4
|
-
class Configuration
|
5
|
-
require 'pg'
|
6
|
-
require 'logger'
|
7
|
-
require 'json'
|
8
|
-
require 'geocoder'
|
9
|
-
|
10
|
-
attr_accessor :host, :port, :options, :tty, :db_name, :user, :password, :database_schema, :event_table_name,
|
11
|
-
:bots_regex, :logger
|
12
|
-
|
13
|
-
def initialize
|
14
|
-
self.host = nil
|
15
|
-
self.port = 5432
|
16
|
-
self.options = nil
|
17
|
-
self.tty = nil
|
18
|
-
self.db_name = nil
|
19
|
-
self.user = nil
|
20
|
-
self.password = nil
|
21
|
-
self.database_schema = 'public'
|
22
|
-
self.event_table_name = 'events'
|
23
|
-
|
24
|
-
self.bots_regex = /Googlebot|Pingdom|bing|Yahoo|Amazon|Twitter|Yandex|majestic12/i
|
25
|
-
|
26
|
-
self.logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
27
|
-
end
|
28
|
-
end
|
5
|
+
require "sql_metrics/railtie" if defined?(Rails)
|
29
6
|
|
7
|
+
module SqlMetrics
|
30
8
|
class << self
|
31
9
|
attr_accessor :configuration
|
32
|
-
|
33
|
-
def merge_request_and_options_into_properties(properties, request, options)
|
34
|
-
if request
|
35
|
-
properties[:user_agent] = request.user_agent
|
36
|
-
properties[:session_id] = request.session_options[:id]
|
37
|
-
properties[:remote_ip] = request.remote_ip
|
38
|
-
|
39
|
-
unless options and options[:geo_lookup] == false
|
40
|
-
if properties[:remote_ip] and geo_object = Geocoder.search(properties[:remote_ip]).first
|
41
|
-
properties[:remote_city] = geo_object.city
|
42
|
-
properties[:remote_country] = geo_object.country
|
43
|
-
properties[:remote_country_code] = geo_object.country_code
|
44
|
-
properties[:remote_coordinates] = geo_object.coordinates
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
properties[:referrer] = request.referer
|
49
|
-
referer = Addressable::URI.parse(request.referer)
|
50
|
-
properties[:referrer_host] = referer.host if referer
|
51
|
-
|
52
|
-
properties[:requested_url] = request.fullpath
|
53
|
-
fullpath = Addressable::URI.parse(request.fullpath)
|
54
|
-
properties[:requested_url_host] = fullpath.host if fullpath
|
55
|
-
end
|
56
|
-
|
57
|
-
properties
|
58
|
-
end
|
59
|
-
|
60
|
-
def send_async_query(name, properties)
|
61
|
-
pg_connection.send_query(build_psql_query(name, properties))
|
62
|
-
end
|
63
|
-
|
64
|
-
def build_psql_query(name, properties)
|
65
|
-
"INSERT INTO #{SqlMetrics.configuration.event_table_name} (
|
66
|
-
created_at,
|
67
|
-
name,
|
68
|
-
properties
|
69
|
-
) VALUES (
|
70
|
-
'#{Time.now.utc}',
|
71
|
-
'#{name}',
|
72
|
-
'#{properties.to_json}'
|
73
|
-
);"
|
74
|
-
end
|
75
10
|
end
|
76
11
|
|
77
12
|
def self.configuration
|
@@ -81,27 +16,4 @@ module SqlMetrics
|
|
81
16
|
def self.configure
|
82
17
|
yield(configuration) if block_given?
|
83
18
|
end
|
84
|
-
|
85
|
-
def self.track(name, properties = {}, request = nil, options = nil)
|
86
|
-
properties = merge_request_and_options_into_properties(properties, request, options)
|
87
|
-
|
88
|
-
unless options and options[:filter_bots] == false
|
89
|
-
return false if properties[:user_agent] and properties[:user_agent].match(SqlMetrics.configuration.bots_regex)
|
90
|
-
end
|
91
|
-
|
92
|
-
send_async_query(name, properties)
|
93
|
-
rescue => e
|
94
|
-
SqlMetrics.configuration.logger.error e
|
95
|
-
SqlMetrics.configuration.logger.error e.backtrace.join("\n")
|
96
|
-
end
|
97
|
-
|
98
|
-
def self.pg_connection
|
99
|
-
PGconn.open(:dbname => SqlMetrics.configuration.db_name,
|
100
|
-
:host => SqlMetrics.configuration.host,
|
101
|
-
:port => SqlMetrics.configuration.port,
|
102
|
-
:options => SqlMetrics.configuration.options,
|
103
|
-
:tty => SqlMetrics.configuration.tty,
|
104
|
-
:user => SqlMetrics.configuration.user,
|
105
|
-
:password => SqlMetrics.configuration.password)
|
106
|
-
end
|
107
19
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
namespace :sql_metrics do
|
2
|
+
|
3
|
+
desc 'Insert the events table'
|
4
|
+
task :create_events_table => :environment do
|
5
|
+
begin
|
6
|
+
SqlMetrics.create_events_table
|
7
|
+
puts "Succesfully inserted #{SqlMetrics.configuration.event_table_name} table!"
|
8
|
+
rescue => e
|
9
|
+
puts e
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
data/sql_metrics.gemspec
CHANGED
@@ -21,9 +21,9 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.10"
|
23
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
-
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
25
25
|
|
26
|
-
spec.add_runtime_dependency "pg"
|
27
|
-
spec.add_runtime_dependency "logging"
|
28
|
-
spec.add_runtime_dependency "geocoder"
|
26
|
+
spec.add_runtime_dependency "pg", "~> 0.18"
|
27
|
+
spec.add_runtime_dependency "logging", "~> 2.0"
|
28
|
+
spec.add_runtime_dependency "geocoder", "~> 1.2"
|
29
29
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sql_metrics
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthias
|
@@ -42,58 +42,58 @@ dependencies:
|
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '3.3'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '3.3'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: pg
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
61
|
+
version: '0.18'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
68
|
+
version: '0.18'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: logging
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
75
|
+
version: '2.0'
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
82
|
+
version: '2.0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: geocoder
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - "
|
87
|
+
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '1.2'
|
90
90
|
type: :runtime
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - "
|
94
|
+
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
96
|
+
version: '1.2'
|
97
97
|
description: A simple gem to track metric events in your own postgres or Amazon Redshift
|
98
98
|
database.
|
99
99
|
email:
|
@@ -113,7 +113,11 @@ files:
|
|
113
113
|
- bin/console
|
114
114
|
- bin/setup
|
115
115
|
- lib/sql_metrics.rb
|
116
|
+
- lib/sql_metrics/config.rb
|
117
|
+
- lib/sql_metrics/railtie.rb
|
118
|
+
- lib/sql_metrics/sql_metrics.rb
|
116
119
|
- lib/sql_metrics/version.rb
|
120
|
+
- lib/tasks/sql_metrics.rake
|
117
121
|
- sql_metrics.gemspec
|
118
122
|
homepage: https://github.com/KaktusLab/sql_metrics
|
119
123
|
licenses:
|