mongo_request_logger 0.5.0 → 0.6.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/.ruby-version +1 -1
- data/.travis.yml +2 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +1 -1
- data/README.md +2 -2
- data/config.ru +4 -5
- data/gemfiles/rails4.2.gemfile +1 -0
- data/gemfiles/sinatra1.4.gemfile +1 -0
- data/lib/mongo_request_logger/adapters/base.rb +3 -1
- data/lib/mongo_request_logger/adapters/mongo.rb +62 -0
- data/lib/mongo_request_logger/mongoid_config.rb +95 -0
- data/lib/mongo_request_logger/railtie.rb +16 -13
- data/lib/mongo_request_logger/version.rb +1 -1
- data/lib/mongo_request_logger.rb +8 -5
- data/spec/mongodriver_logger_spec.rb +34 -0
- data/spec/rails_spec.rb +0 -2
- metadata +7 -7
- data/lib/mongo_request_logger/adapters/moped.rb +0 -64
- data/lib/mongo_request_logger/ext/moped_session.rb +0 -5
- data/spec/moped_logger_spec.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10ab543ab4fed5e432099b38ce6c32bc3e745aa0
|
4
|
+
data.tar.gz: b202b63d0c93bd20baea08a806f3325bdd1114c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7a24b97466f0803cd2ff95a180ff122fbc82bd646e39c381f65e7ecf15bfdac68a328b2f9ac029bb496c415d4d3ae0a391674a0c4a0de1c501aada5529bdeeb
|
7
|
+
data.tar.gz: 1f591f39c0926808a0f1308fda972968e54798a24226bc6b1a7901e33eba9dc13f90a5725d7fcf34d4da29562744a1abadfdb5f110e115e911b82282d8efb497
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.2.
|
1
|
+
2.2.2
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -18,7 +18,7 @@ Example:
|
|
18
18
|
|
19
19
|
development:
|
20
20
|
sessions:
|
21
|
-
|
21
|
+
logger:
|
22
22
|
database: your_log_db
|
23
23
|
hosts:
|
24
24
|
- host1:27017
|
@@ -44,7 +44,7 @@ In config.ru:
|
|
44
44
|
require './yourapp'
|
45
45
|
|
46
46
|
# Configure database details
|
47
|
-
session =
|
47
|
+
session = Mongo::Client.new(%w(127.0.0.1:27017))
|
48
48
|
session.use 'your_log_db'
|
49
49
|
session.options[:capsize] = 10 # MB
|
50
50
|
|
data/config.ru
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
# Sinatra app for viewing logs
|
2
|
-
require '
|
2
|
+
require 'mongo'
|
3
3
|
require 'mongo_request_logger/viewer'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
session.options[:capsize] = 10 # MB
|
5
|
+
client = Mongo::Client.new(%w(127.0.0.1:27017))
|
6
|
+
client.use ENV['DB'] || 'request_logger_test'
|
8
7
|
|
9
|
-
MongoRequestLogger::Viewer.adapter = MongoRequestLogger::Adapters::Moped.new(
|
8
|
+
MongoRequestLogger::Viewer.adapter = MongoRequestLogger::Adapters::Moped.new(client)
|
10
9
|
|
11
10
|
MongoRequestLogger::Viewer.run!
|
12
11
|
|
data/gemfiles/rails4.2.gemfile
CHANGED
data/gemfiles/sinatra1.4.gemfile
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
module MongoRequestLogger
|
4
4
|
module Adapters
|
5
5
|
class Base
|
6
|
+
DEFAULT_CAP_SIZE = 100
|
6
7
|
|
7
8
|
attr_reader :configuration, :connection, :connection_type, :collection, :authenticated
|
8
9
|
|
@@ -12,7 +13,8 @@ module MongoRequestLogger
|
|
12
13
|
|
13
14
|
# Capsize in bytes
|
14
15
|
def capsize
|
15
|
-
options[:capsize]
|
16
|
+
cap = options[:capsize] || DEFAULT_CAP_SIZE
|
17
|
+
cap * 1024 * 1024
|
16
18
|
end
|
17
19
|
|
18
20
|
def authenticated?
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'mongo'
|
2
|
+
require 'mongo_request_logger/adapters/base'
|
3
|
+
|
4
|
+
module MongoRequestLogger
|
5
|
+
module Adapters
|
6
|
+
class Mongo < Base
|
7
|
+
def initialize client, opts = {}
|
8
|
+
@connection = client.database
|
9
|
+
|
10
|
+
@options = {
|
11
|
+
read: {
|
12
|
+
mode: :secondary_preferred
|
13
|
+
},
|
14
|
+
write: {
|
15
|
+
w: 0
|
16
|
+
}
|
17
|
+
}
|
18
|
+
@options.merge!(@connection.options.deep_dup)
|
19
|
+
@options.merge!(opts)
|
20
|
+
|
21
|
+
check_for_collection
|
22
|
+
end
|
23
|
+
|
24
|
+
def options
|
25
|
+
@options.with_indifferent_access
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_collection
|
29
|
+
@collection = @connection[collection_name, capped: true, size: capsize]
|
30
|
+
@collection.create
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_index field
|
34
|
+
@collection.indexes.create_one({ field: 1 }, background: true)
|
35
|
+
end
|
36
|
+
|
37
|
+
def insert_log_record(record)
|
38
|
+
@collection.insert_one(record)
|
39
|
+
end
|
40
|
+
|
41
|
+
def collection_stats
|
42
|
+
collection_stats_hash(@connection.command(collStats: collection_name).first)
|
43
|
+
end
|
44
|
+
|
45
|
+
def query(criteria, options={})
|
46
|
+
q = @collection.find(criteria).sort({'timestamp' => -1})
|
47
|
+
if options[:limit]
|
48
|
+
q = q.limit(options[:limit])
|
49
|
+
end
|
50
|
+
q
|
51
|
+
end
|
52
|
+
|
53
|
+
def find_by_id(id)
|
54
|
+
@collection.find("_id" => ::BSON::ObjectId.from_string(id)).first
|
55
|
+
end
|
56
|
+
|
57
|
+
def clear!
|
58
|
+
@collection.drop
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module MongoRequestLogger
|
2
|
+
# Directly parse the mongoig.yml config and present accessors similar to Mongoid 5.
|
3
|
+
# TODO: to be replaced by the Mongoid 5 default behaviour
|
4
|
+
module MongoidConfig
|
5
|
+
def self.clients
|
6
|
+
@clients ||= ClientMap.new
|
7
|
+
end
|
8
|
+
|
9
|
+
class ClientMap
|
10
|
+
attr_reader :config
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@clients = HashWithIndifferentAccess.new
|
14
|
+
config_file = Rails.root.join("config", "mongoid.yml")
|
15
|
+
@config = Environment.load_yaml(config_file).with_indifferent_access
|
16
|
+
@config = @config[:sessions]
|
17
|
+
@config = @config.with_indifferent_access
|
18
|
+
end
|
19
|
+
|
20
|
+
def [] key
|
21
|
+
config = @config[key]
|
22
|
+
raise Mongoid::Errors::NoSessionConfig.new(key) unless config
|
23
|
+
@clients[key] ||= ClientFactory.create(config)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class ClientFactory
|
28
|
+
def self.create configuration
|
29
|
+
raise unless configuration
|
30
|
+
if configuration[:uri]
|
31
|
+
Mongo::Client.new(configuration[:uri], options(configuration))
|
32
|
+
else
|
33
|
+
opts = options(configuration).merge(database: configuration[:database])
|
34
|
+
opts = opts.merge(credentials(configuration))
|
35
|
+
Mongo::Client.new(configuration[:hosts], opts)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def self.options(configuration)
|
42
|
+
config = configuration.dup
|
43
|
+
options = config.delete(:options) || {}
|
44
|
+
options.reject{ |k, v| k == :hosts }.to_hash.symbolize_keys!
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.credentials(configuration)
|
48
|
+
details = {}
|
49
|
+
details[:user] = configuration[:username] unless configuration[:username].nil?
|
50
|
+
details[:password] = configuration[:password] unless configuration[:password].nil?
|
51
|
+
details
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class NoEnvironment < RuntimeError; end
|
56
|
+
# Encapsulates logic for getting environment information.
|
57
|
+
module Environment
|
58
|
+
extend self
|
59
|
+
|
60
|
+
# Get the name of the environment that we are running under. This first
|
61
|
+
# looks for Rails, then Sinatra, then a RACK_ENV environment variable,
|
62
|
+
# and if none of those are found raises an error.
|
63
|
+
#
|
64
|
+
# @example Get the env name.
|
65
|
+
# Environment.env_name
|
66
|
+
#
|
67
|
+
# @raise [ Errors::NoEnvironment ] If no environment was set.
|
68
|
+
#
|
69
|
+
# @return [ String ] The name of the current environment.
|
70
|
+
#
|
71
|
+
# @since 2.3.0
|
72
|
+
def env_name
|
73
|
+
return Rails.env if defined?(Rails) && Rails.respond_to?(:env)
|
74
|
+
return Sinatra::Base.environment.to_s if defined?(Sinatra)
|
75
|
+
ENV["RACK_ENV"] || ENV["MONGOID_ENV"] || raise(NoEnvironment.new)
|
76
|
+
end
|
77
|
+
|
78
|
+
# Load the yaml from the provided path and return the settings for the
|
79
|
+
# current environment.
|
80
|
+
#
|
81
|
+
# @example Load the yaml.
|
82
|
+
# Environment.load_yaml("/work/mongoid.yml")
|
83
|
+
#
|
84
|
+
# @param [ String ] path The location of the file.
|
85
|
+
#
|
86
|
+
# @return [ Hash ] The settings.
|
87
|
+
#
|
88
|
+
# @since 2.3.0
|
89
|
+
def load_yaml(path, environment = nil)
|
90
|
+
env = environment ? environment.to_s : env_name
|
91
|
+
YAML.load(ERB.new(File.new(path).read).result)[env]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -7,6 +7,7 @@ if defined? Rails
|
|
7
7
|
class Railtie < ::Rails::Railtie
|
8
8
|
class << self
|
9
9
|
def setup(app)
|
10
|
+
Mongo::Logger.logger.level = Logger::INFO # Because spammy.
|
10
11
|
logger = MongoRequestLogger.configure(fallback_log: Rails.root.join("log/#{Rails.env}.log"))
|
11
12
|
|
12
13
|
app.config.logger = logger
|
@@ -24,20 +25,22 @@ if defined? Rails
|
|
24
25
|
|
25
26
|
app.config.middleware.insert_after ActionDispatch::DebugExceptions, MongoRequestLogger::Rack
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
::
|
34
|
-
|
28
|
+
unless logger.adapter.is_a?(MongoRequestLogger::Adapters::Mongo)
|
29
|
+
app.config.after_initialize do
|
30
|
+
# Unicorn clears the START_CTX when a worker is forked, so if we have
|
31
|
+
# data in START_CTX then we know we're being preloaded. Unicorn does
|
32
|
+
# not provide application-level hooks for executing code after the
|
33
|
+
# process has forked, so we reconnect lazily.
|
34
|
+
if defined?(Unicorn) && !Unicorn::HttpServer::START_CTX.empty?
|
35
|
+
::MongoRequestLogger.session.disconnect
|
36
|
+
end
|
35
37
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
# Passenger provides the :starting_worker_process event for executing
|
39
|
+
# code after it has forked, so we use that and reconnect immediately.
|
40
|
+
if ::Mongoid::Config.running_with_passenger?
|
41
|
+
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
42
|
+
::MongoRequestLogger.session.disconnect if forked
|
43
|
+
end
|
41
44
|
end
|
42
45
|
end
|
43
46
|
end
|
data/lib/mongo_request_logger.rb
CHANGED
@@ -9,8 +9,10 @@ if defined?(Rails)
|
|
9
9
|
end
|
10
10
|
|
11
11
|
require 'mongoid'
|
12
|
-
|
13
|
-
require '
|
12
|
+
|
13
|
+
require 'mongo'
|
14
|
+
require_relative './mongo_request_logger/adapters/mongo.rb'
|
15
|
+
require_relative './mongo_request_logger/mongoid_config.rb'
|
14
16
|
|
15
17
|
|
16
18
|
module MongoRequestLogger
|
@@ -24,13 +26,14 @@ module MongoRequestLogger
|
|
24
26
|
require 'mongo_request_logger/viewer'
|
25
27
|
|
26
28
|
environment = ENV['RACK_ENV'] || ENV['RAILS_ENV']
|
27
|
-
|
29
|
+
|
30
|
+
session = MongoidConfig.clients[:logger]
|
28
31
|
if session
|
29
|
-
log_config =
|
32
|
+
log_config = MongoidConfig.clients.config[:logger][:options].with_indifferent_access
|
30
33
|
|
31
34
|
fallback_log = options[:fallback_log] || log_config['fallback_log'] || "log/#{environment}.log"
|
32
35
|
|
33
|
-
adapter = MongoRequestLogger::Adapters::
|
36
|
+
adapter = MongoRequestLogger::Adapters::Mongo.new(session, log_config)
|
34
37
|
|
35
38
|
@session = session
|
36
39
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'mongo_request_logger'
|
3
|
+
require 'mongo'
|
4
|
+
require 'shared_examples'
|
5
|
+
|
6
|
+
include MongoRequestLogger
|
7
|
+
|
8
|
+
describe 'MongoRequestLogger::Logger with mongo-ruby-driver' do
|
9
|
+
before do
|
10
|
+
Mongo::Logger.logger.level = Logger::INFO
|
11
|
+
client = Mongo::Client.new(%w(127.0.0.1:27017), database: "request_logger_test")
|
12
|
+
begin
|
13
|
+
client = client.with(user: "request_logger_test", password: "test_password")
|
14
|
+
client.command(ping: 1)
|
15
|
+
rescue Mongo::Auth::Unauthorized => e
|
16
|
+
# Try without authentication
|
17
|
+
client.command(ping: 1)
|
18
|
+
end
|
19
|
+
|
20
|
+
client['server_log'].drop rescue nil
|
21
|
+
|
22
|
+
@adapter = MongoRequestLogger::Adapters::Mongo.new(client, capsize: 10)
|
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
|
+
|
34
|
+
end
|
data/spec/rails_spec.rb
CHANGED
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.6.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:
|
11
|
+
date: 2016-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|
@@ -376,15 +376,15 @@ files:
|
|
376
376
|
- gemfiles/sinatra1.4.gemfile
|
377
377
|
- lib/mongo_request_logger.rb
|
378
378
|
- lib/mongo_request_logger/adapters/base.rb
|
379
|
-
- lib/mongo_request_logger/adapters/
|
379
|
+
- lib/mongo_request_logger/adapters/mongo.rb
|
380
380
|
- lib/mongo_request_logger/backtrace_cleaner.rb
|
381
381
|
- lib/mongo_request_logger/ext/activesupport_logger.rb
|
382
382
|
- lib/mongo_request_logger/ext/logger.rb
|
383
|
-
- lib/mongo_request_logger/ext/moped_session.rb
|
384
383
|
- lib/mongo_request_logger/log_message.rb
|
385
384
|
- lib/mongo_request_logger/logged_job.rb
|
386
385
|
- lib/mongo_request_logger/logger.rb
|
387
386
|
- lib/mongo_request_logger/logger_extensions.rb
|
387
|
+
- lib/mongo_request_logger/mongoid_config.rb
|
388
388
|
- lib/mongo_request_logger/rack.rb
|
389
389
|
- lib/mongo_request_logger/railtie.rb
|
390
390
|
- lib/mongo_request_logger/search_terms.rb
|
@@ -394,7 +394,7 @@ files:
|
|
394
394
|
- spec/dummy/application.rb
|
395
395
|
- spec/dummy/config/mongoid.yml
|
396
396
|
- spec/dummy/log/.gitkeep
|
397
|
-
- spec/
|
397
|
+
- spec/mongodriver_logger_spec.rb
|
398
398
|
- spec/query_spec.rb
|
399
399
|
- spec/rails_spec.rb
|
400
400
|
- spec/shared_examples.rb
|
@@ -424,7 +424,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
424
424
|
version: '0'
|
425
425
|
requirements: []
|
426
426
|
rubyforge_project:
|
427
|
-
rubygems_version: 2.4
|
427
|
+
rubygems_version: 2.6.4
|
428
428
|
signing_key:
|
429
429
|
specification_version: 4
|
430
430
|
summary: Structured logger and log viewer for Rack (including Rails) requests
|
@@ -432,7 +432,7 @@ test_files:
|
|
432
432
|
- spec/dummy/application.rb
|
433
433
|
- spec/dummy/config/mongoid.yml
|
434
434
|
- spec/dummy/log/.gitkeep
|
435
|
-
- spec/
|
435
|
+
- spec/mongodriver_logger_spec.rb
|
436
436
|
- spec/query_spec.rb
|
437
437
|
- spec/rails_spec.rb
|
438
438
|
- spec/shared_examples.rb
|
@@ -1,64 +0,0 @@
|
|
1
|
-
# Adapted from https://github.com/le0pard/mongodb_logger/
|
2
|
-
require 'moped'
|
3
|
-
|
4
|
-
require 'mongo_request_logger/adapters/base'
|
5
|
-
require 'mongo_request_logger/ext/moped_session'
|
6
|
-
|
7
|
-
module MongoRequestLogger
|
8
|
-
module Adapters
|
9
|
-
class Moped < Base
|
10
|
-
class << self
|
11
|
-
attr_accessor :connection
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
def initialize(session)
|
16
|
-
@connection = session
|
17
|
-
MongoRequestLogger::Adapters::Moped.connection = @connection
|
18
|
-
|
19
|
-
@connection_type = @connection.class
|
20
|
-
|
21
|
-
check_for_collection
|
22
|
-
end
|
23
|
-
|
24
|
-
def options
|
25
|
-
@connection.options
|
26
|
-
end
|
27
|
-
|
28
|
-
def create_collection
|
29
|
-
@connection.cluster.with_primary do |node|
|
30
|
-
node.command(@connection.send(:current_database).name, {create: collection_name, capped: true, size: capsize})
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def create_index field
|
35
|
-
@collection.indexes.create({field => 1})
|
36
|
-
end
|
37
|
-
|
38
|
-
def insert_log_record(record)
|
39
|
-
record[:_id] = ::BSON::ObjectId.new
|
40
|
-
@connection.with(safe: false)[collection_name].insert(record)
|
41
|
-
end
|
42
|
-
|
43
|
-
def collection_stats
|
44
|
-
collection_stats_hash(@connection.command(collStats: collection_name))
|
45
|
-
end
|
46
|
-
|
47
|
-
def query(criteria, options={})
|
48
|
-
q = @collection.find(criteria).sort({'timestamp' => -1})
|
49
|
-
if options[:limit]
|
50
|
-
q = q.limit(options[:limit])
|
51
|
-
end
|
52
|
-
q
|
53
|
-
end
|
54
|
-
|
55
|
-
def find_by_id(id)
|
56
|
-
@collection.find("_id" => ::BSON::ObjectId.from_string(id)).first
|
57
|
-
end
|
58
|
-
|
59
|
-
def clear!
|
60
|
-
@collection.drop
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
data/spec/moped_logger_spec.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'mongo_request_logger'
|
3
|
-
require 'moped'
|
4
|
-
require 'shared_examples'
|
5
|
-
|
6
|
-
include MongoRequestLogger
|
7
|
-
|
8
|
-
describe 'MongoRequestLogger::Logger with moped' do
|
9
|
-
before do
|
10
|
-
session = Moped::Session.new(%w(127.0.0.1:27017))
|
11
|
-
session.use "request_logger_test"
|
12
|
-
begin
|
13
|
-
session.login("request_logger_test", "test_password")
|
14
|
-
session.command(ping: 1)
|
15
|
-
rescue Moped::Errors::AuthenticationFailure => e
|
16
|
-
session.logout
|
17
|
-
session.command(ping: 1)
|
18
|
-
end
|
19
|
-
|
20
|
-
session['server_log'].drop rescue nil
|
21
|
-
|
22
|
-
session.options[:capsize] = 10
|
23
|
-
@adapter = MongoRequestLogger::Adapters::Moped.new(session)
|
24
|
-
|
25
|
-
@file = 'test-log.txt'
|
26
|
-
FileUtils.remove @file, :force => true
|
27
|
-
end
|
28
|
-
|
29
|
-
after do
|
30
|
-
FileUtils.remove @file, :force => true
|
31
|
-
end
|
32
|
-
|
33
|
-
include_examples "log"
|
34
|
-
|
35
|
-
end
|