mongo_active_instrumentation 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 04b56eebdb82174cb02f053a589eb0e4d5cbe6ea
4
+ data.tar.gz: b8702f3bcbb1f3ad89bb8f93cdc11e0528df09e1
5
+ SHA512:
6
+ metadata.gz: 3bd2e1fcb21e0b4c5af5cf5461d367ebceab08ffadcb22e65f485b45941b82c645acf6c775058a1a250133dfd1efa42f9084c6c78cf0396421b5b1a3244f3f6a
7
+ data.tar.gz: 91f8bb11f526049b8894b4ac2d6b5104e899ffd98a04893ced91543280213f17b0f39edfb3f62021cbb72f1fd18ff642b52832efcbf2763e04796279bb20ee11
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Edgars Beigarts
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,38 @@
1
+ # Mongo Active Instrumentation
2
+
3
+ Pretty logging with colors for mongo/mongoid with rails.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mongo_active_instrumentation'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ ## Example
20
+
21
+ ```
22
+ Started GET "/" for ::1
23
+ Processing by PagesController#show as HTML
24
+ Parameters: {"path"=>"/"}
25
+ Mongo (2.1ms) {"find"=>"users", "filter"=>{"deleted_at"=>nil, "_id"=>BSON::ObjectId('4f796a9e83fa26bacf000360')}, "limit"=>1, "singleBatch"=>true}
26
+ Completed 200 OK in 54ms (Views: 9.7ms | Mongo: 2.1ms)
27
+ ```
28
+
29
+ vs
30
+
31
+ ```
32
+ Started GET "/" for ::1
33
+ Processing by PagesController#show as HTML
34
+ Parameters: {"path"=>"/"}
35
+ MONGODB | localhost:27017 | app_development.find | STARTED | {"find"=>"users", "filter"=>{"deleted_at"=>nil, "_id"=>BSON::ObjectId('4f796a9e83fa26bacf000360')}, "limit"=>1, "singleBatch"=>true}
36
+ MONGODB | localhost:27017 | app_development.find | SUCCEEDED | 0.0021879999999999998s
37
+ Completed 200 OK in 54ms (Views: 9.7ms)
38
+ ```
@@ -0,0 +1,6 @@
1
+ require 'active_support'
2
+ require 'mongo_active_instrumentation/log_subscriber'
3
+ require 'mongo_active_instrumentation/railtie' if defined?(Rails)
4
+
5
+ module MongoActiveInstrumentation
6
+ end
@@ -0,0 +1,50 @@
1
+ require "active_support/core_ext/module/attr_internal"
2
+ require "active_record/log_subscriber"
3
+
4
+ module MongoActiveInstrumentation
5
+ module ControllerRuntime #:nodoc:
6
+ extend ActiveSupport::Concern
7
+
8
+ protected
9
+
10
+ attr_internal :mongo_runtime
11
+
12
+ private
13
+
14
+ def process_action(action, *args)
15
+ # We also need to reset the runtime before each action
16
+ # because of queries in middleware or in cases we are streaming
17
+ # and it won't be cleaned up by the method below.
18
+ MongoActiveInstrumentation::LogSubscriber.reset_runtime
19
+ super
20
+ end
21
+
22
+ def cleanup_view_runtime
23
+ if logger && logger.info? && ActiveRecord::Base.connected?
24
+ mongo_rt_before_render = MongoActiveInstrumentation::LogSubscriber.reset_runtime
25
+ self.mongo_runtime = (mongo_runtime || 0) + mongo_rt_before_render
26
+ runtime = super
27
+ mongo_rt_after_render = MongoActiveInstrumentation::LogSubscriber.reset_runtime
28
+ self.mongo_runtime += mongo_rt_after_render
29
+ runtime - mongo_rt_after_render
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ def append_info_to_payload(payload)
36
+ super
37
+ if ActiveRecord::Base.connected?
38
+ payload[:mongo_runtime] = (mongo_runtime || 0) + MongoActiveInstrumentation::LogSubscriber.reset_runtime
39
+ end
40
+ end
41
+
42
+ module ClassMethods # :nodoc:
43
+ def log_process_action(payload)
44
+ messages, mongo_runtime = super, payload[:mongo_runtime]
45
+ messages << ("Mongo: %.1fms" % mongo_runtime.to_f) if mongo_runtime
46
+ messages
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,67 @@
1
+ module MongoActiveInstrumentation
2
+ class LogSubscriber < ActiveSupport::LogSubscriber
3
+ def self.runtime=(value)
4
+ Thread.current["mongo_runtime"] = value
5
+ end
6
+
7
+ def self.runtime
8
+ Thread.current["mongo_runtime"] ||= 0
9
+ end
10
+
11
+ def self.reset_runtime
12
+ rt, self.runtime = runtime, 0
13
+ rt
14
+ end
15
+
16
+ def initialize(options = {})
17
+ super()
18
+ @events = {}
19
+ end
20
+
21
+ def started(event)
22
+ return unless logger.debug?
23
+ @events[event.request_id] = event
24
+ end
25
+
26
+ def succeeded(event)
27
+ return unless logger.debug?
28
+ completed event, CYAN
29
+ end
30
+
31
+ def failed(event)
32
+ return unless logger.debug?
33
+ completed event, RED
34
+ end
35
+
36
+ private
37
+
38
+ def completed(event, name_color)
39
+ started_event = @events.delete(event.request_id)
40
+ if started_event
41
+ duration = event.duration * 1000
42
+ self.class.runtime += duration
43
+ command_name = started_event.command_name.to_s
44
+ name = "Mongo (#{(duration).round(1)}ms)"
45
+ name = color(name, name_color, true)
46
+ command = event.respond_to?(:message) ? event.message : started_event.command.inspect
47
+ command = color(command, command_color(command_name), true)
48
+ debug " #{name} #{command}"
49
+ end
50
+ end
51
+
52
+ def command_color(command_name)
53
+ case command_name
54
+ when 'find', 'count', 'aggregate'
55
+ BLUE
56
+ when 'insert'
57
+ GREEN
58
+ when 'update'
59
+ YELLOW
60
+ when 'delete'
61
+ RED
62
+ else
63
+ MAGENTA
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,23 @@
1
+ require 'mongo_active_instrumentation/controller_runtime'
2
+
3
+ module MongoActiveInstrumentation
4
+ class Railtie < Rails::Railtie
5
+ initializer "mongo_active_instrumentation" do |app|
6
+ Mongo::Monitoring::Global.subscribe(
7
+ Mongo::Monitoring::COMMAND,
8
+ MongoActiveInstrumentation::LogSubscriber.new
9
+ )
10
+
11
+ # Disable the existing log subscriber
12
+ Mongo::Monitoring::CommandLogSubscriber.class_eval do
13
+ def started(event); end
14
+ def succeeded(event); end
15
+ def failed(event); end
16
+ end
17
+
18
+ ActiveSupport.on_load(:action_controller) do
19
+ include MongoActiveInstrumentation::ControllerRuntime
20
+ end
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_active_instrumentation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Edgars Beigarts
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongo
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ description: ActiveSupport/Rails instrumentation for Mongo
42
+ email:
43
+ - edgars.beigarts@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - LICENSE.txt
49
+ - README.md
50
+ - lib/mongo_active_instrumentation.rb
51
+ - lib/mongo_active_instrumentation/controller_runtime.rb
52
+ - lib/mongo_active_instrumentation/log_subscriber.rb
53
+ - lib/mongo_active_instrumentation/railtie.rb
54
+ homepage: https://github.com/ebeigarts/mongo_active_instrumentation
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.5.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: ActiveSupport/Rails instrumentation for Mongo
78
+ test_files: []