mongo_request_logger 0.3.1 → 0.4.0
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/CHANGELOG.md +7 -0
- data/Gemfile +2 -3
- data/README.md +21 -12
- data/config.ru +4 -2
- data/gemfiles/sinatra1.3.gemfile +2 -3
- data/gemfiles/sinatra1.4.gemfile +2 -3
- data/lib/mongo_request_logger.rb +15 -30
- data/lib/mongo_request_logger/adapters/base.rb +6 -2
- data/lib/mongo_request_logger/adapters/moped.rb +5 -44
- data/lib/mongo_request_logger/railtie.rb +19 -1
- data/lib/mongo_request_logger/version.rb +1 -1
- data/spec/moped_logger_spec.rb +3 -115
- metadata +2 -8
- data/lib/mongo_request_logger/adapters/mongo.rb +0 -90
- data/lib/mongo_request_logger/config.rb +0 -165
- data/spec/config_spec.rb +0 -130
- data/spec/mongo_logger_spec.rb +0 -33
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77639396cad58e5a076a1bfb8faf2814140c79a3
|
4
|
+
data.tar.gz: 04ef4b8af39a09990dd78a6d88648cae5f881902
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 27db8433c868c7cf729afefb4418ca3dd90905c735e608cff97946c3f23098b7cd5ff158dcd77ce2dfa93a1c9a8d47f7f342d9ac60ec2ebca2a18fbe9ec58805
|
7
|
+
data.tar.gz: c60ac9125d39a8c98a43974f9ede24093904c90c3ec9a4c049114154d96c2d11ac3d3ced27f10af0f900648b41bb1c92394054196c2b10b6f521d34f8f435658
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -6,23 +6,28 @@ Log requests in a structured format to MongoDB.
|
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
+
gem 'mongoid', '~> 3.0'
|
9
10
|
gem 'mongo_request_logger'
|
10
11
|
|
11
12
|
## Usage with Rails
|
12
13
|
|
13
|
-
|
14
|
+
Add a `logger` session to your Mongoid config, `config/mongoid.yml`. An additional option
|
15
|
+
`capsize` must be provided.
|
14
16
|
|
15
|
-
|
16
|
-
host: localhost
|
17
|
-
database: your_app_logs_production_dev
|
18
|
-
capsize: 100 # megabytes
|
17
|
+
Example:
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
19
|
+
development:
|
20
|
+
sessions:
|
21
|
+
loggger:
|
22
|
+
database: your_log_db
|
23
|
+
hosts:
|
24
|
+
- host1:27017
|
25
|
+
- host2:27017
|
26
|
+
- host3:27017
|
27
|
+
options:
|
28
|
+
safe: false
|
29
|
+
consistency: eventual
|
30
|
+
capsize: 1000 # MB
|
26
31
|
|
27
32
|
Routes for log viewer
|
28
33
|
|
@@ -39,7 +44,11 @@ In config.ru:
|
|
39
44
|
require './yourapp'
|
40
45
|
|
41
46
|
# Configure database details
|
42
|
-
|
47
|
+
session = Moped::Session.new(%w(127.0.0.1:27017))
|
48
|
+
session.use 'your_log_db'
|
49
|
+
session.options[:capsize] = 10 # MB
|
50
|
+
|
51
|
+
MongoRequestLogger.configure(session: session)
|
43
52
|
|
44
53
|
# Enable the middleware to log the requests
|
45
54
|
use MongoRequestLogger::Rack
|
data/config.ru
CHANGED
@@ -2,9 +2,11 @@
|
|
2
2
|
require 'moped'
|
3
3
|
require 'mongo_request_logger/viewer'
|
4
4
|
|
5
|
-
|
5
|
+
session = Moped::Session.new(%w(127.0.0.1:27017))
|
6
|
+
session.use ENV['DB'] || 'request_logger_test'
|
7
|
+
session.options[:capsize] = 10 # MB
|
6
8
|
|
7
|
-
MongoRequestLogger::Viewer.adapter = MongoRequestLogger::Adapters::Moped.new(
|
9
|
+
MongoRequestLogger::Viewer.adapter = MongoRequestLogger::Adapters::Moped.new(session)
|
8
10
|
|
9
11
|
MongoRequestLogger::Viewer.run!
|
10
12
|
|
data/gemfiles/sinatra1.3.gemfile
CHANGED
data/gemfiles/sinatra1.4.gemfile
CHANGED
data/lib/mongo_request_logger.rb
CHANGED
@@ -8,55 +8,40 @@ if defined?(Rails)
|
|
8
8
|
require 'mongo_request_logger/railtie'
|
9
9
|
end
|
10
10
|
|
11
|
+
require 'mongoid'
|
12
|
+
require 'moped'
|
11
13
|
require 'mongo_request_logger/adapters/moped'
|
12
|
-
require 'mongo_request_logger/adapters/mongo'
|
13
14
|
|
14
15
|
|
15
16
|
module MongoRequestLogger
|
16
|
-
|
17
|
-
|
17
|
+
|
18
|
+
def self.session
|
19
|
+
@session
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.configure(options={})
|
18
23
|
require 'mongo_request_logger/rack'
|
19
24
|
require 'mongo_request_logger/viewer'
|
20
25
|
|
21
|
-
begin
|
22
|
-
# We prefer moped ...
|
23
|
-
require 'moped'
|
24
|
-
rescue LoadError
|
25
|
-
# ... but fallback to mongo
|
26
|
-
require 'mongo'
|
27
|
-
end
|
28
|
-
|
29
26
|
environment = ENV['RACK_ENV'] || ENV['RAILS_ENV']
|
27
|
+
session = options[:session] || Mongoid::Sessions.with_name(:logger)
|
28
|
+
if session
|
29
|
+
log_config = session.options
|
30
30
|
|
31
|
-
|
32
|
-
global_config.load_file(config_path)
|
33
|
-
log_config = global_config.namespaced(environment)
|
34
|
-
fallback_log = options[:fallback_log] || log_config['fallback_log'] || "log/#{environment}.log"
|
31
|
+
fallback_log = options[:fallback_log] || log_config['fallback_log'] || "log/#{environment}.log"
|
35
32
|
|
33
|
+
adapter = MongoRequestLogger::Adapters::Moped.new(session)
|
36
34
|
|
37
|
-
|
38
|
-
if defined? Moped
|
39
|
-
adapter = MongoRequestLogger::Adapters::Moped.new(log_config)
|
40
|
-
else
|
41
|
-
adapter = MongoRequestLogger::Adapters::Mongo.new(log_config)
|
42
|
-
end
|
35
|
+
@session = session
|
43
36
|
|
44
37
|
MongoRequestLogger::Viewer.adapter = adapter
|
45
38
|
|
46
|
-
if defined?(PhusionPassenger)
|
47
|
-
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
48
|
-
if forked
|
49
|
-
adapter.reconnect
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
39
|
logger = MongoRequestLogger::Logger.new adapter, fallback_log
|
55
40
|
else
|
41
|
+
fallback_log = options[:fallback_log] || "log/#{environment}.log"
|
56
42
|
logger = ::Logger.new(fallback_log)
|
57
43
|
end
|
58
44
|
|
59
|
-
|
60
45
|
MongoRequestLogger::Rack.logger = logger
|
61
46
|
MongoRequestLogger::Rack.ignore_prefixes << '/assets'
|
62
47
|
MongoRequestLogger::Rack.ignore_prefixes << '/favicon.ico'
|
@@ -7,7 +7,12 @@ module MongoRequestLogger
|
|
7
7
|
attr_reader :configuration, :connection, :connection_type, :collection, :authenticated
|
8
8
|
|
9
9
|
def collection_name
|
10
|
-
|
10
|
+
options[:collection] || 'server_log'
|
11
|
+
end
|
12
|
+
|
13
|
+
# Capsize in bytes
|
14
|
+
def capsize
|
15
|
+
options[:capsize] * 1024 * 1024
|
11
16
|
end
|
12
17
|
|
13
18
|
def authenticated?
|
@@ -37,7 +42,6 @@ module MongoRequestLogger
|
|
37
42
|
:count => stats["count"].to_i,
|
38
43
|
:size => stats["size"].to_f,
|
39
44
|
:storageSize => stats["storageSize"].to_f,
|
40
|
-
:db_name => @configuration["database"],
|
41
45
|
:collection => collection_name
|
42
46
|
}
|
43
47
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
# Adapted from https://github.com/le0pard/mongodb_logger/
|
2
2
|
|
3
3
|
require 'mongo_request_logger/adapters/base'
|
4
|
-
require 'active_support/core_ext/hash/indifferent_access'
|
5
|
-
require 'mongo_request_logger/config'
|
6
4
|
|
7
5
|
module MongoRequestLogger
|
8
6
|
module Adapters
|
@@ -12,13 +10,8 @@ module MongoRequestLogger
|
|
12
10
|
end
|
13
11
|
|
14
12
|
|
15
|
-
def initialize(
|
16
|
-
|
17
|
-
@configuration = options
|
18
|
-
@connection = ::Moped::Session.new(*options.moped_arguments)
|
19
|
-
|
20
|
-
login
|
21
|
-
|
13
|
+
def initialize(session)
|
14
|
+
@connection = session
|
22
15
|
MongoRequestLogger::Adapters::Moped.connection = @connection
|
23
16
|
|
24
17
|
@connection_type = @connection.class
|
@@ -26,23 +19,13 @@ module MongoRequestLogger
|
|
26
19
|
check_for_collection
|
27
20
|
end
|
28
21
|
|
29
|
-
def
|
30
|
-
|
31
|
-
@authenticated = @connection.login(*@configuration.credentials)
|
32
|
-
end
|
33
|
-
|
34
|
-
# Allow MongoDB configured both with and without authentication
|
35
|
-
begin
|
36
|
-
@connection.command(ping: 1)
|
37
|
-
rescue ::Moped::Errors::AuthenticationFailure => e
|
38
|
-
@connection.logout
|
39
|
-
@connection.command(ping: 1)
|
40
|
-
end
|
22
|
+
def options
|
23
|
+
@connection.options
|
41
24
|
end
|
42
25
|
|
43
26
|
def create_collection
|
44
27
|
@connection.cluster.with_primary do |node|
|
45
|
-
node.command(@
|
28
|
+
node.command(@connection.send(:current_database).name, {create: collection_name, capped: true, size: capsize})
|
46
29
|
end
|
47
30
|
end
|
48
31
|
|
@@ -74,28 +57,6 @@ module MongoRequestLogger
|
|
74
57
|
def clear!
|
75
58
|
@collection.drop
|
76
59
|
end
|
77
|
-
|
78
|
-
|
79
60
|
end
|
80
61
|
end
|
81
62
|
end
|
82
|
-
|
83
|
-
# Adapted from Mongoid 3.0.16 lib/mongoid/railtie.rb
|
84
|
-
|
85
|
-
# Unicorn clears the START_CTX when a worker is forked, so if we have
|
86
|
-
# data in START_CTX then we know we're being preloaded. Unicorn does
|
87
|
-
# not provide application-level hooks for executing code after the
|
88
|
-
# process has forked, so we reconnect lazily.
|
89
|
-
if defined?(Unicorn) && !Unicorn::HttpServer::START_CTX.empty?
|
90
|
-
MongoRequestLogger::Adapters::Moped.connection.disconnect if MongoRequestLogger::Adapters::Moped.connection
|
91
|
-
end
|
92
|
-
|
93
|
-
# Passenger provides the :starting_worker_process event for executing
|
94
|
-
# code after it has forked, so we use that and reconnect immediately.
|
95
|
-
if defined?(PhusionPassenger)
|
96
|
-
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
97
|
-
if MongoRequestLogger::Adapters::Moped.connection && forked
|
98
|
-
MongoRequestLogger::Adapters::Moped.connection.disconnect
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
@@ -7,7 +7,7 @@ if (defined? Rails) && Rails.version =~ /^3\./
|
|
7
7
|
class Railtie < ::Rails::Railtie
|
8
8
|
class << self
|
9
9
|
def setup(app)
|
10
|
-
logger = MongoRequestLogger.configure(
|
10
|
+
logger = MongoRequestLogger.configure(fallback_log: Rails.root.join("log/#{Rails.env}.log"))
|
11
11
|
|
12
12
|
app.config.logger = logger
|
13
13
|
|
@@ -24,6 +24,24 @@ if (defined? Rails) && Rails.version =~ /^3\./
|
|
24
24
|
|
25
25
|
app.config.middleware.insert_after ActionDispatch::DebugExceptions, MongoRequestLogger::Rack
|
26
26
|
|
27
|
+
app.config.after_initialize do
|
28
|
+
# Unicorn clears the START_CTX when a worker is forked, so if we have
|
29
|
+
# data in START_CTX then we know we're being preloaded. Unicorn does
|
30
|
+
# not provide application-level hooks for executing code after the
|
31
|
+
# process has forked, so we reconnect lazily.
|
32
|
+
if defined?(Unicorn) && !Unicorn::HttpServer::START_CTX.empty?
|
33
|
+
::MongoRequestLogger.session.disconnect
|
34
|
+
end
|
35
|
+
|
36
|
+
# Passenger provides the :starting_worker_process event for executing
|
37
|
+
# code after it has forked, so we use that and reconnect immediately.
|
38
|
+
if ::Mongoid::Config.running_with_passenger?
|
39
|
+
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
40
|
+
::MongoRequestLogger.session.disconnect if forked
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
27
45
|
end
|
28
46
|
end
|
29
47
|
|
data/spec/moped_logger_spec.rb
CHANGED
@@ -16,16 +16,11 @@ describe 'MongoRequestLogger::Logger with moped' do
|
|
16
16
|
session.logout
|
17
17
|
session.command(ping: 1)
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
session['server_log'].drop rescue nil
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
database: 'request_logger_test',
|
25
|
-
collection: 'server_log',
|
26
|
-
capsize: 10,
|
27
|
-
}
|
28
|
-
@adapter = MongoRequestLogger::Adapters::Moped.new(config)
|
22
|
+
session.options[:capsize] = 10
|
23
|
+
@adapter = MongoRequestLogger::Adapters::Moped.new(session)
|
29
24
|
|
30
25
|
@file = 'test-log.txt'
|
31
26
|
FileUtils.remove @file, :force => true
|
@@ -37,111 +32,4 @@ describe 'MongoRequestLogger::Logger with moped' do
|
|
37
32
|
|
38
33
|
include_examples "log"
|
39
34
|
|
40
|
-
context "when configured with a simple config hash" do
|
41
|
-
it "should configure with a single host" do
|
42
|
-
config = {
|
43
|
-
host: 'localhost',
|
44
|
-
port: 27017,
|
45
|
-
database: 'request_logger_test',
|
46
|
-
collection: 'server_log',
|
47
|
-
capsize: 10,
|
48
|
-
}
|
49
|
-
|
50
|
-
adapter = MongoRequestLogger::Adapters::Moped.new(config)
|
51
|
-
connection = adapter.connection
|
52
|
-
connection.options[:database].should == 'request_logger_test'
|
53
|
-
connection.consistency.should == :eventual # The default
|
54
|
-
connection.cluster.nodes.first.resolved_address.should == '127.0.0.1:27017'
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should configure with a host array" do
|
58
|
-
config = {
|
59
|
-
hosts: [
|
60
|
-
'localhost:27017',
|
61
|
-
'localhost:27017',
|
62
|
-
],
|
63
|
-
database: 'request_logger_test',
|
64
|
-
collection: 'server_log',
|
65
|
-
capsize: 10,
|
66
|
-
}
|
67
|
-
|
68
|
-
adapter = MongoRequestLogger::Adapters::Moped.new(config)
|
69
|
-
connection = adapter.connection
|
70
|
-
connection.options[:database].should == 'request_logger_test'
|
71
|
-
connection.cluster.nodes[0].resolved_address.should == '127.0.0.1:27017'
|
72
|
-
connection.cluster.nodes[1].resolved_address.should == '127.0.0.1:27017'
|
73
|
-
end
|
74
|
-
|
75
|
-
it "should pass through options" do
|
76
|
-
# This should work similarly for ssl: true. Unfortunately, we don't have a
|
77
|
-
# ssl host to test against.
|
78
|
-
config = {
|
79
|
-
host: 'localhost',
|
80
|
-
port: 27017,
|
81
|
-
consistency: 'strong',
|
82
|
-
timeout: 30,
|
83
|
-
database: 'request_logger_test',
|
84
|
-
collection: 'server_log',
|
85
|
-
capsize: 10,
|
86
|
-
}
|
87
|
-
|
88
|
-
adapter = MongoRequestLogger::Adapters::Moped.new(config)
|
89
|
-
connection = adapter.connection
|
90
|
-
connection.options[:database].should == 'request_logger_test'
|
91
|
-
connection.options[:timeout].should == 30
|
92
|
-
connection.options[:consistency].should == :strong
|
93
|
-
connection.consistency.should == :strong
|
94
|
-
|
95
|
-
connection.cluster.nodes[0].resolved_address.should == '127.0.0.1:27017'
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
context "when configured with an URI" do
|
100
|
-
it "should configure with a single host" do
|
101
|
-
config = {
|
102
|
-
uri: 'mongodb://localhost:27017/request_logger_test',
|
103
|
-
collection: 'server_log',
|
104
|
-
capsize: 10,
|
105
|
-
}
|
106
|
-
|
107
|
-
adapter = MongoRequestLogger::Adapters::Moped.new(config)
|
108
|
-
connection = adapter.connection
|
109
|
-
connection.options[:database].should == 'request_logger_test'
|
110
|
-
connection.consistency.should == :eventual # The default
|
111
|
-
connection.cluster.nodes.first.resolved_address.should == '127.0.0.1:27017'
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should configure with multiple hosts" do
|
115
|
-
# We only have a single real host to connect to, so we add it multiple times
|
116
|
-
config = {
|
117
|
-
uri: 'mongodb://localhost:27017,localhost:27017/request_logger_test',
|
118
|
-
collection: 'server_log',
|
119
|
-
capsize: 10,
|
120
|
-
}
|
121
|
-
|
122
|
-
adapter = MongoRequestLogger::Adapters::Moped.new(config)
|
123
|
-
connection = adapter.connection
|
124
|
-
connection.options[:database].should == 'request_logger_test'
|
125
|
-
connection.cluster.nodes[0].resolved_address.should == '127.0.0.1:27017'
|
126
|
-
connection.cluster.nodes[1].resolved_address.should == '127.0.0.1:27017'
|
127
|
-
end
|
128
|
-
|
129
|
-
it "should configure with URI options" do
|
130
|
-
# This should work similarly for ssl=true. Unfortunately, we don't have a
|
131
|
-
# ssl host to test against.
|
132
|
-
config = {
|
133
|
-
uri: 'mongodb://localhost:27017/request_logger_test?consistency=strong',
|
134
|
-
collection: 'server_log',
|
135
|
-
capsize: 10,
|
136
|
-
}
|
137
|
-
|
138
|
-
adapter = MongoRequestLogger::Adapters::Moped.new(config)
|
139
|
-
connection = adapter.connection
|
140
|
-
connection.options[:database].should == 'request_logger_test'
|
141
|
-
connection.options[:consistency].should == :strong
|
142
|
-
connection.consistency.should == :strong
|
143
|
-
|
144
|
-
connection.cluster.nodes[0].resolved_address.should == '127.0.0.1:27017'
|
145
|
-
end
|
146
|
-
end
|
147
35
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_request_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Embark Mobile
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|
@@ -375,10 +375,8 @@ files:
|
|
375
375
|
- gemfiles/sinatra1.4.gemfile
|
376
376
|
- lib/mongo_request_logger.rb
|
377
377
|
- lib/mongo_request_logger/adapters/base.rb
|
378
|
-
- lib/mongo_request_logger/adapters/mongo.rb
|
379
378
|
- lib/mongo_request_logger/adapters/moped.rb
|
380
379
|
- lib/mongo_request_logger/backtrace_cleaner.rb
|
381
|
-
- lib/mongo_request_logger/config.rb
|
382
380
|
- lib/mongo_request_logger/ext/buffered_logger.rb
|
383
381
|
- lib/mongo_request_logger/ext/logger.rb
|
384
382
|
- lib/mongo_request_logger/log_message.rb
|
@@ -391,11 +389,9 @@ files:
|
|
391
389
|
- lib/mongo_request_logger/version.rb
|
392
390
|
- lib/mongo_request_logger/viewer.rb
|
393
391
|
- mongo_request_logger.gemspec
|
394
|
-
- spec/config_spec.rb
|
395
392
|
- spec/dummy/application.rb
|
396
393
|
- spec/dummy/config/logger.yml
|
397
394
|
- spec/dummy/log/.gitkeep
|
398
|
-
- spec/mongo_logger_spec.rb
|
399
395
|
- spec/moped_logger_spec.rb
|
400
396
|
- spec/query_spec.rb
|
401
397
|
- spec/rails_spec.rb
|
@@ -431,11 +427,9 @@ signing_key:
|
|
431
427
|
specification_version: 4
|
432
428
|
summary: Structured logger and log viewer for Rack (including Rails) requests
|
433
429
|
test_files:
|
434
|
-
- spec/config_spec.rb
|
435
430
|
- spec/dummy/application.rb
|
436
431
|
- spec/dummy/config/logger.yml
|
437
432
|
- spec/dummy/log/.gitkeep
|
438
|
-
- spec/mongo_logger_spec.rb
|
439
433
|
- spec/moped_logger_spec.rb
|
440
434
|
- spec/query_spec.rb
|
441
435
|
- spec/rails_spec.rb
|
@@ -1,90 +0,0 @@
|
|
1
|
-
# Adapted from https://github.com/le0pard/mongodb_logger/
|
2
|
-
|
3
|
-
require 'mongo_request_logger/adapters/base'
|
4
|
-
require 'mongo_request_logger/config'
|
5
|
-
|
6
|
-
module MongoRequestLogger
|
7
|
-
module Adapters
|
8
|
-
class Mongo < Base
|
9
|
-
|
10
|
-
def initialize(options = {})
|
11
|
-
options = MongoRequestLogger::Config.new(options) unless options.is_a? MongoRequestLogger::Config
|
12
|
-
@configuration = options
|
13
|
-
|
14
|
-
mongo_connection_object
|
15
|
-
connect
|
16
|
-
|
17
|
-
check_for_collection
|
18
|
-
end
|
19
|
-
|
20
|
-
def connect
|
21
|
-
@connection ||= mongo_connection_object.db(@configuration.database)
|
22
|
-
if @configuration.credentials
|
23
|
-
@authenticated = @connection.authenticate(*@configuration.credentials)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def reconnect
|
28
|
-
connect
|
29
|
-
end
|
30
|
-
|
31
|
-
def create_collection
|
32
|
-
@connection.create_collection(collection_name,
|
33
|
-
{:capped => true, :size => @configuration['capsize'].to_i*1024*1024})
|
34
|
-
end
|
35
|
-
|
36
|
-
def create_index field
|
37
|
-
@collection.create_index(field)
|
38
|
-
end
|
39
|
-
|
40
|
-
def insert_log_record(record)
|
41
|
-
# TODO: we should make this non-safe?
|
42
|
-
@collection.insert(record)
|
43
|
-
end
|
44
|
-
|
45
|
-
def collection_stats
|
46
|
-
collection_stats_hash(@collection.stats)
|
47
|
-
end
|
48
|
-
|
49
|
-
def query(criteria, options={})
|
50
|
-
q = @collection.find(criteria).sort('timestamp', -1)
|
51
|
-
if options[:limit]
|
52
|
-
q = q.limit(options[:limit])
|
53
|
-
end
|
54
|
-
q
|
55
|
-
end
|
56
|
-
|
57
|
-
def find_by_id(id)
|
58
|
-
@collection.find_one(::BSON::ObjectId(id))
|
59
|
-
end
|
60
|
-
|
61
|
-
def clear!
|
62
|
-
raise NotImplementedError
|
63
|
-
end
|
64
|
-
|
65
|
-
private
|
66
|
-
|
67
|
-
def mongo_connection_object
|
68
|
-
hosts = @configuration.hosts
|
69
|
-
if @configuration['uri']
|
70
|
-
::Mongo::Connection.from_uri(@configuration['uri'])
|
71
|
-
elsif hosts.count > 1
|
72
|
-
conn = ::Mongo::ReplSetConnection.new(*(hosts <<
|
73
|
-
{:connect => true, :pool_timeout => 6}))
|
74
|
-
@configuration['replica_set'] = true # is this correct?
|
75
|
-
else
|
76
|
-
host = @configuration['host'] || '127.0.0.1'
|
77
|
-
port = @configuration['port'] || '27017'
|
78
|
-
|
79
|
-
conn = ::Mongo::Connection.new(host,
|
80
|
-
port,
|
81
|
-
:connect => true,
|
82
|
-
:pool_timeout => 6)
|
83
|
-
end
|
84
|
-
@connection_type = conn.class
|
85
|
-
conn
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
@@ -1,165 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
|
3
|
-
begin
|
4
|
-
require 'moped/mongo_uri'
|
5
|
-
rescue LoadError
|
6
|
-
# Ignore
|
7
|
-
end
|
8
|
-
|
9
|
-
module MongoRequestLogger
|
10
|
-
class Config
|
11
|
-
attr_accessor :raw_config
|
12
|
-
attr_accessor :params
|
13
|
-
attr_accessor :namespace
|
14
|
-
|
15
|
-
def initialize(config=nil)
|
16
|
-
@params = {}
|
17
|
-
@namespace = nil
|
18
|
-
load_config(config.stringify_keys) unless config.nil?
|
19
|
-
end
|
20
|
-
|
21
|
-
def namespaced(namespace)
|
22
|
-
config = deep_merge(@params['common'], @params[namespace])
|
23
|
-
ac = self.class.new(config)
|
24
|
-
ac.namespace = namespace
|
25
|
-
ac
|
26
|
-
end
|
27
|
-
|
28
|
-
|
29
|
-
def namespaces
|
30
|
-
@params.keys - %w(common)
|
31
|
-
end
|
32
|
-
|
33
|
-
def load_file(file)
|
34
|
-
begin
|
35
|
-
hash = YAML::load(IO.read(file))
|
36
|
-
rescue => e
|
37
|
-
hash = nil
|
38
|
-
end
|
39
|
-
load_config(hash) unless hash.nil?
|
40
|
-
end
|
41
|
-
|
42
|
-
def deep_merge(a, b)
|
43
|
-
if a.is_a? Hash and b.is_a? Hash
|
44
|
-
r = a.stringify_keys
|
45
|
-
b = b.stringify_keys
|
46
|
-
for k, v in b
|
47
|
-
r[k] = deep_merge(a[k], v)
|
48
|
-
end
|
49
|
-
r
|
50
|
-
elsif b.is_a? Hash
|
51
|
-
b.stringify_keys
|
52
|
-
else
|
53
|
-
b
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def load_config(config)
|
58
|
-
@params = deep_merge(@params, config)
|
59
|
-
end
|
60
|
-
|
61
|
-
def has?(param)
|
62
|
-
@params.key?(param.to_s)
|
63
|
-
end
|
64
|
-
|
65
|
-
def get(param)
|
66
|
-
@params[param.to_s]
|
67
|
-
end
|
68
|
-
|
69
|
-
def [](param)
|
70
|
-
get(param)
|
71
|
-
end
|
72
|
-
|
73
|
-
def []=(param, value)
|
74
|
-
@params[param.to_s] = value
|
75
|
-
end
|
76
|
-
|
77
|
-
def with_indifferent_access
|
78
|
-
self
|
79
|
-
end
|
80
|
-
|
81
|
-
def to_hash
|
82
|
-
@params.symbolize_keys
|
83
|
-
end
|
84
|
-
|
85
|
-
def hosts
|
86
|
-
if self['hosts']
|
87
|
-
return self['hosts']
|
88
|
-
elsif self.moped_uri
|
89
|
-
return self.moped_uri.hosts
|
90
|
-
else
|
91
|
-
host = self['host'] || '127.0.0.1'
|
92
|
-
port = self['port'] || 27017
|
93
|
-
return ["#{host}:#{port}"]
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
def credentials
|
98
|
-
if moped_uri && moped_uri.auth_provided?
|
99
|
-
[moped_uri.username, moped_uri.password]
|
100
|
-
elsif self['username'] && self['password']
|
101
|
-
[self['username'], self['password']]
|
102
|
-
else
|
103
|
-
nil
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
def database
|
108
|
-
if self['database']
|
109
|
-
self['database']
|
110
|
-
elsif moped_uri
|
111
|
-
moped_uri.database
|
112
|
-
else
|
113
|
-
nil
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def collection
|
118
|
-
self['collection'] || 'server_log'
|
119
|
-
end
|
120
|
-
|
121
|
-
def capsize
|
122
|
-
self['capsize'].to_i*1024*1024
|
123
|
-
end
|
124
|
-
|
125
|
-
def moped_uri
|
126
|
-
if self['uri'] && defined? ::Moped::MongoUri
|
127
|
-
::Moped::MongoUri.new(self['uri'])
|
128
|
-
else
|
129
|
-
nil
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
def moped_config
|
134
|
-
args = {}
|
135
|
-
|
136
|
-
if moped_uri
|
137
|
-
args = moped_uri.moped_arguments[1]
|
138
|
-
end
|
139
|
-
|
140
|
-
args[:database] = self.database
|
141
|
-
|
142
|
-
exclude = %w(host port hosts uri collection database capsize)
|
143
|
-
|
144
|
-
@params.each do |key, value|
|
145
|
-
next if exclude.include? key
|
146
|
-
args[key.to_sym] = cast(value)
|
147
|
-
end
|
148
|
-
|
149
|
-
args
|
150
|
-
end
|
151
|
-
|
152
|
-
def moped_arguments
|
153
|
-
[hosts, moped_config]
|
154
|
-
end
|
155
|
-
|
156
|
-
private
|
157
|
-
def cast(value)
|
158
|
-
if value.is_a? String
|
159
|
-
value.to_sym
|
160
|
-
else
|
161
|
-
value
|
162
|
-
end
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
data/spec/config_spec.rb
DELETED
@@ -1,130 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'mongo_request_logger/config'
|
3
|
-
require 'mongo'
|
4
|
-
|
5
|
-
include MongoRequestLogger
|
6
|
-
|
7
|
-
describe MongoRequestLogger::Config do
|
8
|
-
|
9
|
-
it "should namespace" do
|
10
|
-
pending
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should handle a simple case" do
|
14
|
-
config = MongoRequestLogger::Config.new({
|
15
|
-
host: 'example.org',
|
16
|
-
port: 1234,
|
17
|
-
database: 'request_logger_test',
|
18
|
-
collection: 'server_log',
|
19
|
-
capsize: 10,
|
20
|
-
})
|
21
|
-
|
22
|
-
config.hosts.should == ['example.org:1234']
|
23
|
-
config.moped_config.should == {database: 'request_logger_test'}
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should default to localhost:27017" do
|
27
|
-
config = MongoRequestLogger::Config.new({
|
28
|
-
database: 'request_logger_test',
|
29
|
-
collection: 'server_log',
|
30
|
-
capsize: 10,
|
31
|
-
})
|
32
|
-
|
33
|
-
config.hosts.should == ['127.0.0.1:27017']
|
34
|
-
config.moped_config.should == {database: 'request_logger_test'}
|
35
|
-
|
36
|
-
config.database.should == 'request_logger_test'
|
37
|
-
config.collection.should == 'server_log'
|
38
|
-
config.capsize.should == 10485760
|
39
|
-
end
|
40
|
-
|
41
|
-
it "should configure with a host array" do
|
42
|
-
config = MongoRequestLogger::Config.new({
|
43
|
-
hosts: [
|
44
|
-
'node1.example.org:27017',
|
45
|
-
'node2.localhost:27017',
|
46
|
-
],
|
47
|
-
database: 'request_logger_test',
|
48
|
-
collection: 'server_log',
|
49
|
-
capsize: 10,
|
50
|
-
})
|
51
|
-
|
52
|
-
config.hosts.should == [
|
53
|
-
'node1.example.org:27017',
|
54
|
-
'node2.localhost:27017',
|
55
|
-
]
|
56
|
-
config.moped_config.should == {database: 'request_logger_test'}
|
57
|
-
end
|
58
|
-
|
59
|
-
it "should pass through options" do
|
60
|
-
# This should work similarly for ssl: true. Unfortunately, we don't have a
|
61
|
-
# ssl host to test against.
|
62
|
-
config = MongoRequestLogger::Config.new({
|
63
|
-
consistency: 'strong',
|
64
|
-
timeout: 30,
|
65
|
-
ssl: true,
|
66
|
-
database: 'request_logger_test',
|
67
|
-
collection: 'server_log',
|
68
|
-
capsize: 10,
|
69
|
-
})
|
70
|
-
|
71
|
-
config.moped_config.should == {
|
72
|
-
timeout: 30,
|
73
|
-
ssl: true,
|
74
|
-
consistency: :strong,
|
75
|
-
database: 'request_logger_test'
|
76
|
-
}
|
77
|
-
end
|
78
|
-
|
79
|
-
context "when configured with an URI" do
|
80
|
-
it "should configure with a single host" do
|
81
|
-
config = MongoRequestLogger::Config.new({
|
82
|
-
uri: 'mongodb://example.org:1234/request_logger_test',
|
83
|
-
collection: 'server_log',
|
84
|
-
capsize: 10
|
85
|
-
})
|
86
|
-
|
87
|
-
config.database.should == 'request_logger_test'
|
88
|
-
config.collection.should == 'server_log'
|
89
|
-
config.capsize.should == 10485760
|
90
|
-
|
91
|
-
config.hosts.should == ['example.org:1234']
|
92
|
-
config.moped_config.should == {database: 'request_logger_test'}
|
93
|
-
end
|
94
|
-
|
95
|
-
it "should configure with multiple hosts" do
|
96
|
-
# We only have a single real host to connect to, so we add it multiple times
|
97
|
-
config = MongoRequestLogger::Config.new({
|
98
|
-
uri: 'mongodb://node1.example.org:1234,node2.example.org:1234/request_logger_test',
|
99
|
-
collection: 'server_log',
|
100
|
-
capsize: 10
|
101
|
-
})
|
102
|
-
|
103
|
-
config.database.should == 'request_logger_test'
|
104
|
-
config.collection.should == 'server_log'
|
105
|
-
|
106
|
-
config.hosts.should == ['node1.example.org:1234', 'node2.example.org:1234']
|
107
|
-
config.moped_config.should == {database: 'request_logger_test'}
|
108
|
-
end
|
109
|
-
|
110
|
-
it "should configure with URI options" do
|
111
|
-
config = MongoRequestLogger::Config.new({
|
112
|
-
uri: 'mongodb://example.org:1234/request_logger_test?consistency=strong&timeout=30&ssl=true',
|
113
|
-
collection: 'server_log',
|
114
|
-
capsize: 10
|
115
|
-
})
|
116
|
-
|
117
|
-
config.database.should == 'request_logger_test'
|
118
|
-
config.collection.should == 'server_log'
|
119
|
-
|
120
|
-
config.hosts.should == ['example.org:1234']
|
121
|
-
config.moped_config.should == {
|
122
|
-
consistency: :strong,
|
123
|
-
timeout: 30,
|
124
|
-
ssl: true,
|
125
|
-
database: 'request_logger_test'
|
126
|
-
}
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
end
|
data/spec/mongo_logger_spec.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'mongo_request_logger'
|
3
|
-
require 'mongo'
|
4
|
-
require 'shared_examples'
|
5
|
-
require 'mongo_request_logger/adapters/mongo'
|
6
|
-
|
7
|
-
include MongoRequestLogger
|
8
|
-
|
9
|
-
describe 'MongoRequestLogger::Logger with mongo' do
|
10
|
-
before do
|
11
|
-
connection = Mongo::Connection.new
|
12
|
-
db = connection.db('request_logger_test')
|
13
|
-
db.authenticate('request_logger_test', 'test_password') rescue nil
|
14
|
-
db.drop_collection 'server_log' rescue nil
|
15
|
-
|
16
|
-
config = {
|
17
|
-
database: 'request_logger_test',
|
18
|
-
collection: 'server_log',
|
19
|
-
capsize: 10,
|
20
|
-
}
|
21
|
-
|
22
|
-
@adapter = MongoRequestLogger::Adapters::Mongo.new(config)
|
23
|
-
|
24
|
-
@file = 'test-log.txt'
|
25
|
-
FileUtils.remove @file, :force => true
|
26
|
-
end
|
27
|
-
|
28
|
-
after do
|
29
|
-
FileUtils.remove @file, :force => true
|
30
|
-
end
|
31
|
-
|
32
|
-
include_examples "log"
|
33
|
-
end
|