mongo_missing_indexes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mongo/missing_indexes.rb +94 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 656e0d6771c56d4c4b8bd70eab09552ce06a296f
4
+ data.tar.gz: 7105302316c6d969f58228c60ac7f6c9ec62c1d3
5
+ SHA512:
6
+ metadata.gz: 9285f24c056e4a512dbbd1cc834803f95008cebdefd52dfc660e26bab16d73da3120e9ebee31cfd136631b93d96ce4a4884a6b9b3834ab0dd968178fc8cbc93d
7
+ data.tar.gz: 432e05595fabd774a97d96d82f4a58fc5fbbb8ccd6b904508555fe0d6d61b4e3dafdebc9dbd33aa9e9c4b029c78746723a4c0d0377c6ed038ee86d5d1bcd6c4e
@@ -0,0 +1,94 @@
1
+ require 'mongo'
2
+ require 'logger'
3
+ require 'colored'
4
+ require 'securerandom'
5
+
6
+ class Mongo::MissingIndexes
7
+ class << self
8
+ def reset!
9
+ remove_instance_variable(:@logger) if defined?(@logger)
10
+ remove_instance_variable(:@enabled) if defined?(@enabled)
11
+ end
12
+
13
+ def enabled=(val)
14
+ @enabled = val
15
+
16
+ [
17
+ :find,
18
+ :count,
19
+ :update,
20
+ ].each do |method_name|
21
+ if @enabled
22
+ Mongo::Collection.class_eval <<-HERE, __FILE__, __LINE__
23
+ unless instance_methods.include?(:#{method_name}_aliased_from_missing_indexes)
24
+ alias_method :#{method_name}_aliased_from_missing_indexes, :#{method_name}
25
+
26
+ def #{method_name}(*args, &block)
27
+ #{method_name}_aliased_from_missing_indexes(*args, &block).tap do
28
+ Mongo::MissingIndexes.instrument_database(self, :#{method_name}, *args, &block)
29
+ end
30
+ end
31
+ end
32
+ HERE
33
+ else
34
+ Mongo::Collection.class_eval <<-HERE, __FILE__, __LINE__
35
+ if instance_methods.include?(:#{method_name}_aliased_from_missing_indexes)
36
+ alias_method :#{method_name}, :#{method_name}_aliased_from_missing_indexes
37
+ undef_method :#{method_name}_aliased_from_missing_indexes
38
+ end
39
+ HERE
40
+ end
41
+ end
42
+ end
43
+
44
+ def enabled?
45
+ @enabled
46
+ end
47
+
48
+ def logger
49
+ defined?(@logger) ?
50
+ @logger :
51
+ @logger ||= defined?(Rails) ? Rails.logger : nil
52
+ end
53
+
54
+ attr_writer :logger
55
+
56
+ def instrument_database(collection, method_name, *args, &block)
57
+ if !logger
58
+ raise "Must provide a logger"
59
+ end
60
+
61
+ if method_name == :update
62
+ explain_method_name = :find
63
+ explain_args = [args[0]]
64
+ else
65
+ return if method_name == :find && args[0] == nil
66
+ explain_method_name = :find
67
+ explain_args = args
68
+ end
69
+
70
+ explain = collection.send("#{explain_method_name}_aliased_from_missing_indexes", *explain_args, &block).explain
71
+ non_index_query = false
72
+
73
+ if explain['stats']
74
+ if explain['stats']['type'] == "COLLSCAN"
75
+ non_index_query = true
76
+ end
77
+ elsif explain['queryPlanner']
78
+ if explain['queryPlanner']['winningPlan']['stage'] == "COLLSCAN"
79
+ non_index_query = true
80
+ end
81
+ end
82
+
83
+ if non_index_query
84
+ query_id = SecureRandom.uuid
85
+
86
+ logger.info("#{query_id} - unindexed query: #{collection.name}.#{method_name}(#{args.map { |a| a.inspect }.join(", ")})".red)
87
+ logger.info("#{query_id} - Query backtrace:".yellow)
88
+ caller.map(&:to_s).each do |line|
89
+ logger.info("#{query_id} - #{line}".yellow)
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_missing_indexes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Scott Taylor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Detect missing indexes in mongo queries
14
+ email: scott@railsnewbie.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/mongo/missing_indexes.rb
20
+ homepage: http://github.com/GoLearnUp/mongo_missing_indexes
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.8
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Detect missing indexes in mongo queries
44
+ test_files: []