sql_metrics 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.idea/.name +1 -0
- data/.idea/.rakeTasks +7 -0
- data/.idea/misc.xml +14 -0
- data/.idea/modules.xml +8 -0
- data/.idea/sql_metrics.iml +164 -0
- data/.idea/vcs.xml +6 -0
- data/.idea/workspace.xml +750 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +149 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/sql_metrics/version.rb +3 -0
- data/lib/sql_metrics.rb +97 -0
- data/sql_metrics-0.1.0.gem +0 -0
- data/sql_metrics.gemspec +29 -0
- metadata +150 -0
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Matthias
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
# SqlMetrics
|
2
|
+
|
3
|
+
A simple gem to track metric events in your own postgres or Amazon Redshift database.
|
4
|
+
|
5
|
+
## Why?
|
6
|
+
I got sick of being limited by tracking services dashboards...and also of paying these services crazy monthy fee's.
|
7
|
+
|
8
|
+
Yes I know about Google Analytics...but I am also tired of GA missing 20-30% of my data.
|
9
|
+
|
10
|
+
So I went ahead a rewrote a library I had written to store events in mixpanel to instead put save them into a postgres database (which means it will also work with Amazon Redshift!).
|
11
|
+
|
12
|
+
I have it running just fine on heroku's postgres offering with a site thats being hit with ~ 250k users per month.
|
13
|
+
|
14
|
+
## Features
|
15
|
+
|
16
|
+
* Asynchronously stores events into Postgres or Amazon Redshift based db
|
17
|
+
* Filters commonly known bots by default
|
18
|
+
* Uses geoip gem to extract city/country from client ip's
|
19
|
+
|
20
|
+
## Todo
|
21
|
+
|
22
|
+
* Write some unit tests
|
23
|
+
* Batch inserting events to db to improve performance under very high load
|
24
|
+
* Track Users (just because thats a common thing to do besides tracking raw events)
|
25
|
+
* Offer SQL based dashboard that allows to run custom queries and also render charts
|
26
|
+
|
27
|
+
## Installation
|
28
|
+
|
29
|
+
Add this line to your application's Gemfile:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
gem 'sql_metrics'
|
33
|
+
```
|
34
|
+
|
35
|
+
And then execute:
|
36
|
+
|
37
|
+
$ bundle
|
38
|
+
|
39
|
+
Or install it yourself as:
|
40
|
+
|
41
|
+
$ gem install sql_metrics
|
42
|
+
|
43
|
+
## Usage
|
44
|
+
|
45
|
+
### Setup database and table
|
46
|
+
|
47
|
+
You need to create the following table in your postgres or redshift database:
|
48
|
+
|
49
|
+
CREATE TABLE events (created_at timestamp, name varchar(200), properties json);
|
50
|
+
|
51
|
+
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:
|
52
|
+
|
53
|
+
SqlMetrics.configure do |config|
|
54
|
+
config.host = '127.0.0.1'
|
55
|
+
config.db_name = 'my_metrics_db'
|
56
|
+
config.user = 'my_postgres_user'
|
57
|
+
config.password = 'my_password'
|
58
|
+
end
|
59
|
+
|
60
|
+
### Track a event
|
61
|
+
|
62
|
+
A simple event can look like this:
|
63
|
+
|
64
|
+
SqlMetrics.track(
|
65
|
+
'event_name',
|
66
|
+
{
|
67
|
+
:a_property => 'hello world',
|
68
|
+
:another_property => 'hello user'
|
69
|
+
}
|
70
|
+
)
|
71
|
+
|
72
|
+
You can also pass a rails request object from a controller:
|
73
|
+
|
74
|
+
SqlMetrics.track(
|
75
|
+
'event_name',
|
76
|
+
{
|
77
|
+
:a_property => 'hello world',
|
78
|
+
:another_property => 'hello user'
|
79
|
+
},
|
80
|
+
request
|
81
|
+
)
|
82
|
+
|
83
|
+
The gem automaticall filters bots for you using the user_agent property from the rails request object...you can disable this if you want:
|
84
|
+
|
85
|
+
SqlMetrics.track(
|
86
|
+
'event_name',
|
87
|
+
{
|
88
|
+
:a_property => 'hello world',
|
89
|
+
:another_property => 'hello user'
|
90
|
+
},
|
91
|
+
request,
|
92
|
+
{
|
93
|
+
:filter_bots => false
|
94
|
+
}
|
95
|
+
)
|
96
|
+
|
97
|
+
This will automatically fetch properties like the user agent, client ip, requested url, etc
|
98
|
+
|
99
|
+
## Additional Config parameters
|
100
|
+
|
101
|
+
### Change DB Table name to use
|
102
|
+
|
103
|
+
SqlMetrics.configure do |config|
|
104
|
+
config.host = '127.0.0.1'
|
105
|
+
config.db_name = 'my_metrics_db'
|
106
|
+
config.user = 'my_postgres_user'
|
107
|
+
config.password = 'my_password'
|
108
|
+
|
109
|
+
config.event_table_name = 'my_custom_events_table'
|
110
|
+
end
|
111
|
+
|
112
|
+
### Change DB Schema to use
|
113
|
+
|
114
|
+
SqlMetrics.configure do |config|
|
115
|
+
config.host = '127.0.0.1'
|
116
|
+
config.db_name = 'my_metrics_db'
|
117
|
+
config.user = 'my_postgres_user'
|
118
|
+
config.password = 'my_password'
|
119
|
+
|
120
|
+
config.database_schema = 'my_custom_schema'
|
121
|
+
end
|
122
|
+
|
123
|
+
### Change Bot regex filter
|
124
|
+
|
125
|
+
SqlMetrics.configure do |config|
|
126
|
+
config.host = '127.0.0.1'
|
127
|
+
config.db_name = 'my_metrics_db'
|
128
|
+
config.user = 'my_postgres_user'
|
129
|
+
config.password = 'my_password'
|
130
|
+
|
131
|
+
config.bots_regex = /Googlebot|Pingdom|bing|Yahoo|Amazon|Twitter|Yandex|majestic12/i
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
## Development
|
136
|
+
|
137
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
138
|
+
|
139
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
140
|
+
|
141
|
+
## Contributing
|
142
|
+
|
143
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/KaktusLab/sql_metrics. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
144
|
+
|
145
|
+
|
146
|
+
## License
|
147
|
+
|
148
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
149
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sql_metrics"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/sql_metrics.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require "sql_metrics/version"
|
2
|
+
|
3
|
+
module SqlMetrics
|
4
|
+
class Configuration
|
5
|
+
require 'pg'
|
6
|
+
require 'logger'
|
7
|
+
require 'json'
|
8
|
+
|
9
|
+
attr_accessor :host, :port, :options, :tty, :db_name, :user, :password, :database_schema, :event_table_name,
|
10
|
+
:bots_regex, :logger
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
self.host = nil
|
14
|
+
self.port = 5432
|
15
|
+
self.options = nil
|
16
|
+
self.tty = nil
|
17
|
+
self.db_name = nil
|
18
|
+
self.user = nil
|
19
|
+
self.password = nil
|
20
|
+
self.database_schema = 'public'
|
21
|
+
self.event_table_name = 'events'
|
22
|
+
|
23
|
+
self.bots_regex = /Googlebot|Pingdom|bing|Yahoo|Amazon|Twitter|Yandex|majestic12/i
|
24
|
+
|
25
|
+
self.logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class << self
|
30
|
+
attr_accessor :configuration
|
31
|
+
|
32
|
+
def merge_request_into_properties(properties, request)
|
33
|
+
if request
|
34
|
+
properties[:user_agent] = request.user_agent
|
35
|
+
properties[:session_id] = request.session_options[:id]
|
36
|
+
properties[:remote_ip] = request.remote_ip
|
37
|
+
|
38
|
+
properties[:referrer] = request.referer
|
39
|
+
referer = Addressable::URI.parse(request.referer)
|
40
|
+
properties[:referrer_host] = referer.host if referer
|
41
|
+
|
42
|
+
properties[:requested_url] = request.fullpath
|
43
|
+
fullpath = Addressable::URI.parse(request.fullpath)
|
44
|
+
properties[:requested_url_host] = fullpath.host if fullpath
|
45
|
+
end
|
46
|
+
|
47
|
+
properties
|
48
|
+
end
|
49
|
+
|
50
|
+
def send_async_query(name, properties)
|
51
|
+
pg_connection.send_query(build_psql_query(name, properties))
|
52
|
+
end
|
53
|
+
|
54
|
+
def build_psql_query(name, properties)
|
55
|
+
"INSERT INTO #{SqlMetrics.configuration.event_table_name} (
|
56
|
+
created_at,
|
57
|
+
name,
|
58
|
+
properties
|
59
|
+
) VALUES (
|
60
|
+
'#{Time.now.utc}',
|
61
|
+
'#{name}',
|
62
|
+
'#{properties.to_json}'
|
63
|
+
);"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.configuration
|
68
|
+
@configuration ||= Configuration.new
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.configure
|
72
|
+
yield(configuration) if block_given?
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.track(name, properties = {}, request = nil, options = nil)
|
76
|
+
properties = merge_request_into_properties(properties, request)
|
77
|
+
|
78
|
+
unless options and options[:filter_bots] == false
|
79
|
+
return false if properties[:user_agent] and properties[:user_agent].match(SqlMetrics.configuration.bots_regex)
|
80
|
+
end
|
81
|
+
|
82
|
+
send_async_query(name, properties)
|
83
|
+
rescue => e
|
84
|
+
SqlMetrics.configuration.logger.error e
|
85
|
+
SqlMetrics.configuration.logger.error e.backtrace.join("\n")
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.pg_connection
|
89
|
+
PGconn.open(:dbname => SqlMetrics.configuration.db_name,
|
90
|
+
:host => SqlMetrics.configuration.host,
|
91
|
+
:port => SqlMetrics.configuration.port,
|
92
|
+
:options => SqlMetrics.configuration.options,
|
93
|
+
:tty => SqlMetrics.configuration.tty,
|
94
|
+
:user => SqlMetrics.configuration.user,
|
95
|
+
:password => SqlMetrics.configuration.password)
|
96
|
+
end
|
97
|
+
end
|
Binary file
|
data/sql_metrics.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sql_metrics/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sql_metrics"
|
8
|
+
spec.version = SqlMetrics::VERSION
|
9
|
+
spec.authors = ["Matthias"]
|
10
|
+
spec.email = ["matthias.chills@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Track events in your own postgres database.}
|
13
|
+
spec.description = %q{A simple gem to track metric events in your own postgres or Amazon Redshift database.}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
|
26
|
+
spec.add_runtime_dependency "pg"
|
27
|
+
spec.add_runtime_dependency "logging"
|
28
|
+
spec.add_runtime_dependency "geoip"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sql_metrics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthias
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pg
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: logging
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: geoip
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: A simple gem to track metric events in your own postgres or Amazon Redshift
|
98
|
+
database.
|
99
|
+
email:
|
100
|
+
- matthias.chills@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".idea/.name"
|
107
|
+
- ".idea/.rakeTasks"
|
108
|
+
- ".idea/misc.xml"
|
109
|
+
- ".idea/modules.xml"
|
110
|
+
- ".idea/sql_metrics.iml"
|
111
|
+
- ".idea/vcs.xml"
|
112
|
+
- ".idea/workspace.xml"
|
113
|
+
- ".rspec"
|
114
|
+
- ".travis.yml"
|
115
|
+
- CODE_OF_CONDUCT.md
|
116
|
+
- Gemfile
|
117
|
+
- LICENSE.txt
|
118
|
+
- README.md
|
119
|
+
- Rakefile
|
120
|
+
- bin/console
|
121
|
+
- bin/setup
|
122
|
+
- lib/sql_metrics.rb
|
123
|
+
- lib/sql_metrics/version.rb
|
124
|
+
- sql_metrics-0.1.0.gem
|
125
|
+
- sql_metrics.gemspec
|
126
|
+
homepage: ''
|
127
|
+
licenses:
|
128
|
+
- MIT
|
129
|
+
metadata: {}
|
130
|
+
post_install_message:
|
131
|
+
rdoc_options: []
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
requirements: []
|
145
|
+
rubyforge_project:
|
146
|
+
rubygems_version: 2.4.8
|
147
|
+
signing_key:
|
148
|
+
specification_version: 4
|
149
|
+
summary: Track events in your own postgres database.
|
150
|
+
test_files: []
|