summaryrefslogtreecommitdiff
path: root/config/initializers/metrics.rb
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2015-12-15 17:23:23 +0100
committerYorick Peterse <yorickpeterse@gmail.com>2015-12-17 17:25:48 +0100
commitbcee44ad33d8a84822a8df068d47812594c445a3 (patch)
tree7433df4afefe513e3593cb25e0ed781d3f4c7fa4 /config/initializers/metrics.rb
parenta93a32a290c8e134763188ebd2b62935f5698e6c (diff)
downloadgitlab-ce-bcee44ad33d8a84822a8df068d47812594c445a3.tar.gz
Instrument all ActiveRecord model methods
This works by searching the raw source code for any references to commonly used ActiveRecord methods. While not bulletproof it saves us from having to list hundreds of methods by hand. It also ensures that (most) newly added methods are instrumented automatically. This _only_ instruments models defined in app/models, should a model reside somewhere else (e.g. somewhere in lib/) it _won't_ be instrumented.
Diffstat (limited to 'config/initializers/metrics.rb')
-rw-r--r--config/initializers/metrics.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/config/initializers/metrics.rb b/config/initializers/metrics.rb
index 0ac4299dcba..a47d2bf59a6 100644
--- a/config/initializers/metrics.rb
+++ b/config/initializers/metrics.rb
@@ -2,6 +2,7 @@ if Gitlab::Metrics.enabled?
require 'influxdb'
require 'socket'
require 'connection_pool'
+ require 'method_source'
# These are manually require'd so the classes are registered properly with
# ActiveSupport.
@@ -18,6 +19,26 @@ if Gitlab::Metrics.enabled?
end
end
+ # This instruments all methods residing in app/models that (appear to) use any
+ # of the ActiveRecord methods. This has to take place _after_ initializing as
+ # for some unknown reason calling eager_load! earlier breaks Devise.
+ Gitlab::Application.config.after_initialize do
+ Rails.application.eager_load!
+
+ models = Rails.root.join('app', 'models').to_s
+
+ regex = Regexp.union(
+ ActiveRecord::Querying.public_instance_methods(false).map(&:to_s)
+ )
+
+ Gitlab::Metrics::Instrumentation.
+ instrument_class_hierarchy(ActiveRecord::Base) do |_, method|
+ loc = method.source_location
+
+ loc && loc[0].start_with?(models) && method.source =~ regex
+ end
+ end
+
Gitlab::Metrics::Instrumentation.configure do |config|
config.instrument_instance_methods(Gitlab::Shell)