panmind-usage-tracker 0.4.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,4 @@
1
- #!/usr/bin/env ruby
2
-
3
1
  require 'usage_tracker'
4
- require 'eventmachine'
5
2
  require 'json'
6
3
 
7
4
  module UsageTracker
@@ -15,16 +12,6 @@ module UsageTracker
15
12
  end
16
13
  end
17
14
 
18
- # Debug hook
19
- if UsageTracker.env == 'test'
20
- alias :real_receive_data :receive_data
21
- def receive_data(data)
22
- UsageTracker.log.debug "Received #{data.inspect}"
23
- ret = real_receive_data(data)
24
- UsageTracker.log.debug ret ? "Stored #{ret}" : 'Failed to store input data'
25
- end
26
- end
27
-
28
15
  private
29
16
  def parse(data)
30
17
  JSON(data).tap {|h| h.reject! {|k,v| v.nil?}}
@@ -57,8 +44,8 @@ module UsageTracker
57
44
  tries = 0
58
45
 
59
46
  begin
60
- doc['_id'] = make_id
61
- UsageTracker.database.save_doc(doc)
47
+ UsageTracker.log.debug "Received #{doc.inspect}"
48
+ UsageTracker.adapter.save_doc(doc)
62
49
 
63
50
  rescue RestClient::Conflict => e
64
51
  if (tries += 1) < 10
@@ -68,65 +55,10 @@ module UsageTracker
68
55
  UsageTracker.log.error "Losing '#{doc.inspect}' because of too many conflicts"
69
56
  end
70
57
 
71
- rescue Encoding::UndefinedConversionError
58
+ rescue
72
59
  UsageTracker.log.error "Losing '#{doc.inspect}' because #$!" # FIXME handle this error properly
73
60
  end
74
61
  end
75
-
76
- # Timestamp as _id has the advantage that documents
77
- # are sorted automatically by CouchDB.
78
- #
79
- # Eventual duplication (multiple servers) is (possibly)
80
- # avoided by adding a random digit at the end.
81
- #
82
- def make_id
83
- Time.now.to_f.to_s.ljust(16, '0') + rand(10).to_s
84
- end
85
- end
86
-
87
- connect!
88
-
89
- # Setup signal handlers
90
- #
91
- # * INT, TERM: graceful exit
92
- # * USR1 : rotate logs
93
- #
94
- def self.sigexit(sig)
95
- log "Received SIG#{sig}"
96
- EventMachine.stop_event_loop
97
- end
98
-
99
- trap('INT') { sigexit 'INT' }
100
- trap('TERM') { sigexit 'TERM' }
101
- trap('USR1') { log.rotate }
102
-
103
- # Run the Event Loop
104
- #
105
- EventMachine.run do
106
- begin
107
- host, port = UsageTracker.settings.host, UsageTracker.settings.port
108
-
109
- unless (1024..65535).include? port.to_i
110
- raise "Please set a listening port between 1024 and 65535"
111
- end
112
-
113
- EventMachine.open_datagram_socket host, port, Reactor
114
- log "Listening on #{host}:#{port} UDP"
115
- write_pid!
116
-
117
- $stderr.puts "Started, logging to #{log.path}"
118
- [$stdin, $stdout, $stderr].each {|io| io.reopen '/dev/null'}
119
-
120
- rescue Exception => e
121
- message = e.message == 'no datagram socket' ? "Unable to bind #{host}:#{port}" : e
122
- log.fatal message
123
- $stderr.puts message unless $stderr.closed?
124
- EventMachine.stop_event_loop
125
- exit 1
126
- end
127
62
  end
128
63
 
129
- # Goodbye!
130
- #
131
- log 'Exiting'
132
64
  end
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # This is the runner script where EventMachine gets
4
+ # actually invocated.
5
+ # Behaviour is defined in the UsageTracker::Reactor module
6
+
7
+ $:<< File.expand_path(File.dirname(__FILE__) + '/..')
8
+
9
+ require 'usage_tracker'
10
+ require 'usage_tracker/reactor'
11
+ require 'eventmachine'
12
+
13
+ module UsageTracker
14
+
15
+ connect!
16
+
17
+ # Setup signal handlers
18
+ #
19
+ # * INT, TERM: graceful exit
20
+ # * USR1 : rotate logs
21
+ #
22
+ def self.sigexit(sig)
23
+ log "Received SIG#{sig}"
24
+ EventMachine.stop_event_loop
25
+ end
26
+
27
+ trap('INT') { sigexit 'INT' }
28
+ trap('TERM') { sigexit 'TERM' }
29
+ trap('USR1') { log.rotate }
30
+
31
+ # Run the Event Loop
32
+ #
33
+ EventMachine.run do
34
+ begin
35
+
36
+ run!
37
+
38
+ $stderr.puts "Started, logging to #{log.path}"
39
+ [$stdin, $stdout, $stderr].each {|io| io.reopen '/dev/null'}
40
+
41
+ rescue Exception => e
42
+ message = e.message == 'no datagram socket' ? "Unable to bind #{UsageTracker.settings.host}:#{UsageTracker.settings.port}" : e
43
+ log.fatal message
44
+ $stderr.puts message unless $stderr.closed?
45
+ EventMachine.stop_event_loop
46
+ exit 1
47
+ end
48
+ end
49
+
50
+ # Goodbye!
51
+ #
52
+ log 'Exiting'
53
+ end
54
+
@@ -5,56 +5,94 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{panmind-usage-tracker}
8
- s.version = "0.4.0"
8
+ s.version = "1.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Marcello Barnaba", "Christian Wo\u0308rner"]
12
- s.date = %q{2010-12-03}
11
+ s.authors = ["Marcello Barnaba", "Christian Wörner", "Fabrizio Regini"]
12
+ s.date = %q{2011-05-25}
13
13
  s.default_executable = %q{usage_tracker}
14
- s.description = %q{This software implements a Rails 3 Middleware and an EventMachine reactor to store into CouchDB the results of HTTP request processing}
15
- s.email = %q{vjt@openssl.it}
14
+ s.description = %q{This software implements a Rails 3 Middleware and an EventMachine reactor to store into a database the results of HTTP request processing}
15
+ s.email = %q{info@panmind.org}
16
16
  s.executables = ["usage_tracker"]
17
17
  s.extra_rdoc_files = [
18
18
  "README.md"
19
19
  ]
20
20
  s.files = [
21
+ "Gemfile",
21
22
  "README.md",
22
23
  "Rakefile",
23
24
  "VERSION",
24
25
  "bin/usage_tracker",
25
26
  "config/usage_tracker.yml.sample",
26
27
  "config/usage_tracker_upstart.conf",
27
- "config/views.yml",
28
28
  "lib/usage_tracker.rb",
29
+ "lib/usage_tracker/adapter.rb",
30
+ "lib/usage_tracker/adapters/couchdb.rb",
31
+ "lib/usage_tracker/adapters/mongodb.rb",
29
32
  "lib/usage_tracker/context.rb",
30
33
  "lib/usage_tracker/log.rb",
31
34
  "lib/usage_tracker/middleware.rb",
32
35
  "lib/usage_tracker/railtie.rb",
33
36
  "lib/usage_tracker/reactor.rb",
34
- "middleware_test.rb"
37
+ "lib/usage_tracker/runner.rb",
38
+ "panmind-usage-tracker.gemspec",
39
+ "spec/spec_helper.rb",
40
+ "spec/usage_tracker_spec.rb"
35
41
  ]
36
42
  s.homepage = %q{http://github.com/Panmind/usage_tracker}
37
43
  s.require_paths = ["lib"]
38
- s.rubygems_version = %q{1.3.7}
39
- s.summary = %q{Write your application request logs in CouchDB}
44
+ s.rubygems_version = %q{1.6.2}
45
+ s.summary = %q{Write your application request logs on CouchDB or MongoDB}
40
46
 
41
47
  if s.respond_to? :specification_version then
42
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
48
  s.specification_version = 3
44
49
 
45
50
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_runtime_dependency(%q<bson_ext>, [">= 0"])
52
+ s.add_runtime_dependency(%q<mongo>, [">= 0"])
53
+ s.add_runtime_dependency(%q<couchrest>, [">= 0"])
54
+ s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.3"])
55
+ s.add_runtime_dependency(%q<json>, [">= 0"])
56
+ s.add_runtime_dependency(%q<eventmachine>, [">= 0"])
57
+ s.add_runtime_dependency(%q<freegenie-em-spec>, ["= 0.2.3"])
58
+ s.add_runtime_dependency(%q<rspec>, [">= 0"])
46
59
  s.add_runtime_dependency(%q<rails>, ["~> 3.0"])
47
60
  s.add_runtime_dependency(%q<eventmachine>, [">= 0"])
48
61
  s.add_runtime_dependency(%q<couchrest>, [">= 0"])
62
+ s.add_runtime_dependency(%q<mongo>, [">= 0"])
63
+ s.add_runtime_dependency(%q<bson>, [">= 0"])
64
+ s.add_runtime_dependency(%q<bson_ext>, [">= 0"])
49
65
  else
66
+ s.add_dependency(%q<bson_ext>, [">= 0"])
67
+ s.add_dependency(%q<mongo>, [">= 0"])
68
+ s.add_dependency(%q<couchrest>, [">= 0"])
69
+ s.add_dependency(%q<activesupport>, ["~> 3.0.3"])
70
+ s.add_dependency(%q<json>, [">= 0"])
71
+ s.add_dependency(%q<eventmachine>, [">= 0"])
72
+ s.add_dependency(%q<freegenie-em-spec>, ["= 0.2.3"])
73
+ s.add_dependency(%q<rspec>, [">= 0"])
50
74
  s.add_dependency(%q<rails>, ["~> 3.0"])
51
75
  s.add_dependency(%q<eventmachine>, [">= 0"])
52
76
  s.add_dependency(%q<couchrest>, [">= 0"])
77
+ s.add_dependency(%q<mongo>, [">= 0"])
78
+ s.add_dependency(%q<bson>, [">= 0"])
79
+ s.add_dependency(%q<bson_ext>, [">= 0"])
53
80
  end
54
81
  else
82
+ s.add_dependency(%q<bson_ext>, [">= 0"])
83
+ s.add_dependency(%q<mongo>, [">= 0"])
84
+ s.add_dependency(%q<couchrest>, [">= 0"])
85
+ s.add_dependency(%q<activesupport>, ["~> 3.0.3"])
86
+ s.add_dependency(%q<json>, [">= 0"])
87
+ s.add_dependency(%q<eventmachine>, [">= 0"])
88
+ s.add_dependency(%q<freegenie-em-spec>, ["= 0.2.3"])
89
+ s.add_dependency(%q<rspec>, [">= 0"])
55
90
  s.add_dependency(%q<rails>, ["~> 3.0"])
56
91
  s.add_dependency(%q<eventmachine>, [">= 0"])
57
92
  s.add_dependency(%q<couchrest>, [">= 0"])
93
+ s.add_dependency(%q<mongo>, [">= 0"])
94
+ s.add_dependency(%q<bson>, [">= 0"])
95
+ s.add_dependency(%q<bson_ext>, [">= 0"])
58
96
  end
59
97
  end
60
98
 
@@ -0,0 +1,6 @@
1
+
2
+ $:<< File.dirname(__FILE__) + '/../lib/'
3
+
4
+
5
+
6
+
@@ -0,0 +1,39 @@
1
+ require File.join( File.dirname( File.expand_path(__FILE__) ) , 'spec_helper' )
2
+
3
+ require 'usage_tracker'
4
+ require 'em-spec/rspec'
5
+ require 'usage_tracker/reactor'
6
+
7
+ describe UsageTracker::Reactor do
8
+ include EM::Spec
9
+
10
+ # Not a great test, ensures the run! method does not crash, at least
11
+ it "should run the reactor" do
12
+ EM.run do
13
+ UsageTracker.connect!
14
+ UsageTracker.run!
15
+ done
16
+ end
17
+ end
18
+
19
+ it "should accept valid keys" do
20
+ EM.run do
21
+ UsageTracker.connect!
22
+ UsageTracker.run!
23
+ UDPSocket.open do |sock|
24
+ sock.connect(UsageTracker.settings.host, UsageTracker.settings.port.to_i)
25
+ sock.write_nonblock({:env => 'env!', :duration => '10.2', :status => '200'}.to_json)
26
+ end
27
+ done
28
+ end
29
+ end
30
+
31
+
32
+ end
33
+
34
+
35
+
36
+
37
+
38
+
39
+
metadata CHANGED
@@ -1,65 +1,176 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: panmind-usage-tracker
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 4
8
- - 0
9
- version: 0.4.0
4
+ prerelease:
5
+ version: 1.0.1
10
6
  platform: ruby
11
7
  authors:
12
8
  - Marcello Barnaba
13
9
  - "Christian Wo\xCC\x88rner"
10
+ - Fabrizio Regini
14
11
  autorequire:
15
12
  bindir: bin
16
13
  cert_chain: []
17
14
 
18
- date: 2010-12-03 00:00:00 +01:00
15
+ date: 2011-05-25 00:00:00 +02:00
19
16
  default_executable: usage_tracker
20
17
  dependencies:
21
18
  - !ruby/object:Gem::Dependency
22
- name: rails
23
- prerelease: false
19
+ name: bson_ext
24
20
  requirement: &id001 !ruby/object:Gem::Requirement
25
21
  none: false
26
22
  requirements:
27
- - - ~>
23
+ - - ">="
28
24
  - !ruby/object:Gem::Version
29
- segments:
30
- - 3
31
- - 0
32
- version: "3.0"
25
+ version: "0"
33
26
  type: :runtime
27
+ prerelease: false
34
28
  version_requirements: *id001
35
29
  - !ruby/object:Gem::Dependency
36
- name: eventmachine
37
- prerelease: false
30
+ name: mongo
38
31
  requirement: &id002 !ruby/object:Gem::Requirement
39
32
  none: false
40
33
  requirements:
41
34
  - - ">="
42
35
  - !ruby/object:Gem::Version
43
- segments:
44
- - 0
45
36
  version: "0"
46
37
  type: :runtime
38
+ prerelease: false
47
39
  version_requirements: *id002
48
40
  - !ruby/object:Gem::Dependency
49
41
  name: couchrest
50
- prerelease: false
51
42
  requirement: &id003 !ruby/object:Gem::Requirement
52
43
  none: false
53
44
  requirements:
54
45
  - - ">="
55
46
  - !ruby/object:Gem::Version
56
- segments:
57
- - 0
58
47
  version: "0"
59
48
  type: :runtime
49
+ prerelease: false
60
50
  version_requirements: *id003
61
- description: This software implements a Rails 3 Middleware and an EventMachine reactor to store into CouchDB the results of HTTP request processing
62
- email: vjt@openssl.it
51
+ - !ruby/object:Gem::Dependency
52
+ name: activesupport
53
+ requirement: &id004 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ version: 3.0.3
59
+ type: :runtime
60
+ prerelease: false
61
+ version_requirements: *id004
62
+ - !ruby/object:Gem::Dependency
63
+ name: json
64
+ requirement: &id005 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: *id005
73
+ - !ruby/object:Gem::Dependency
74
+ name: eventmachine
75
+ requirement: &id006 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ type: :runtime
82
+ prerelease: false
83
+ version_requirements: *id006
84
+ - !ruby/object:Gem::Dependency
85
+ name: freegenie-em-spec
86
+ requirement: &id007 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - "="
90
+ - !ruby/object:Gem::Version
91
+ version: 0.2.3
92
+ type: :runtime
93
+ prerelease: false
94
+ version_requirements: *id007
95
+ - !ruby/object:Gem::Dependency
96
+ name: rspec
97
+ requirement: &id008 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: "0"
103
+ type: :runtime
104
+ prerelease: false
105
+ version_requirements: *id008
106
+ - !ruby/object:Gem::Dependency
107
+ name: rails
108
+ requirement: &id009 !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ~>
112
+ - !ruby/object:Gem::Version
113
+ version: "3.0"
114
+ type: :runtime
115
+ prerelease: false
116
+ version_requirements: *id009
117
+ - !ruby/object:Gem::Dependency
118
+ name: eventmachine
119
+ requirement: &id010 !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: "0"
125
+ type: :runtime
126
+ prerelease: false
127
+ version_requirements: *id010
128
+ - !ruby/object:Gem::Dependency
129
+ name: couchrest
130
+ requirement: &id011 !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: "0"
136
+ type: :runtime
137
+ prerelease: false
138
+ version_requirements: *id011
139
+ - !ruby/object:Gem::Dependency
140
+ name: mongo
141
+ requirement: &id012 !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: "0"
147
+ type: :runtime
148
+ prerelease: false
149
+ version_requirements: *id012
150
+ - !ruby/object:Gem::Dependency
151
+ name: bson
152
+ requirement: &id013 !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: "0"
158
+ type: :runtime
159
+ prerelease: false
160
+ version_requirements: *id013
161
+ - !ruby/object:Gem::Dependency
162
+ name: bson_ext
163
+ requirement: &id014 !ruby/object:Gem::Requirement
164
+ none: false
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: "0"
169
+ type: :runtime
170
+ prerelease: false
171
+ version_requirements: *id014
172
+ description: This software implements a Rails 3 Middleware and an EventMachine reactor to store into a database the results of HTTP request processing
173
+ email: info@panmind.org
63
174
  executables:
64
175
  - usage_tracker
65
176
  extensions: []
@@ -67,21 +178,26 @@ extensions: []
67
178
  extra_rdoc_files:
68
179
  - README.md
69
180
  files:
181
+ - Gemfile
70
182
  - README.md
71
183
  - Rakefile
72
184
  - VERSION
73
185
  - bin/usage_tracker
74
186
  - config/usage_tracker.yml.sample
75
187
  - config/usage_tracker_upstart.conf
76
- - config/views.yml
77
188
  - lib/usage_tracker.rb
189
+ - lib/usage_tracker/adapter.rb
190
+ - lib/usage_tracker/adapters/couchdb.rb
191
+ - lib/usage_tracker/adapters/mongodb.rb
78
192
  - lib/usage_tracker/context.rb
79
193
  - lib/usage_tracker/log.rb
80
194
  - lib/usage_tracker/middleware.rb
81
195
  - lib/usage_tracker/railtie.rb
82
196
  - lib/usage_tracker/reactor.rb
83
- - middleware_test.rb
197
+ - lib/usage_tracker/runner.rb
84
198
  - panmind-usage-tracker.gemspec
199
+ - spec/spec_helper.rb
200
+ - spec/usage_tracker_spec.rb
85
201
  has_rdoc: true
86
202
  homepage: http://github.com/Panmind/usage_tracker
87
203
  licenses: []
@@ -96,23 +212,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
212
  requirements:
97
213
  - - ">="
98
214
  - !ruby/object:Gem::Version
99
- segments:
100
- - 0
101
215
  version: "0"
102
216
  required_rubygems_version: !ruby/object:Gem::Requirement
103
217
  none: false
104
218
  requirements:
105
219
  - - ">="
106
220
  - !ruby/object:Gem::Version
107
- segments:
108
- - 0
109
221
  version: "0"
110
222
  requirements: []
111
223
 
112
224
  rubyforge_project:
113
- rubygems_version: 1.3.7
225
+ rubygems_version: 1.6.2
114
226
  signing_key:
115
227
  specification_version: 3
116
- summary: Write your application request logs in CouchDB
228
+ summary: Write your application request logs on CouchDB or MongoDB
117
229
  test_files: []
118
230