Commit bcee44ad authored by Yorick Peterse's avatar Yorick Peterse

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.
parent a93a32a2
...@@ -210,6 +210,7 @@ gem 'net-ssh', '~> 3.0.1' ...@@ -210,6 +210,7 @@ gem 'net-ssh', '~> 3.0.1'
# Metrics # Metrics
group :metrics do group :metrics do
gem 'method_source', '~> 0.8', require: false
gem 'influxdb', '~> 0.2', require: false gem 'influxdb', '~> 0.2', require: false
gem 'connection_pool', '~> 2.0', require: false gem 'connection_pool', '~> 2.0', require: false
end end
......
...@@ -887,6 +887,7 @@ DEPENDENCIES ...@@ -887,6 +887,7 @@ DEPENDENCIES
kaminari (~> 0.16.3) kaminari (~> 0.16.3)
letter_opener (~> 1.1.2) letter_opener (~> 1.1.2)
mail_room (~> 0.6.1) mail_room (~> 0.6.1)
method_source (~> 0.8)
minitest (~> 5.7.0) minitest (~> 5.7.0)
mousetrap-rails (~> 1.4.6) mousetrap-rails (~> 1.4.6)
mysql2 (~> 0.3.16) mysql2 (~> 0.3.16)
......
...@@ -2,6 +2,7 @@ if Gitlab::Metrics.enabled? ...@@ -2,6 +2,7 @@ if Gitlab::Metrics.enabled?
require 'influxdb' require 'influxdb'
require 'socket' require 'socket'
require 'connection_pool' require 'connection_pool'
require 'method_source'
# These are manually require'd so the classes are registered properly with # These are manually require'd so the classes are registered properly with
# ActiveSupport. # ActiveSupport.
...@@ -18,6 +19,26 @@ if Gitlab::Metrics.enabled? ...@@ -18,6 +19,26 @@ if Gitlab::Metrics.enabled?
end end
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| Gitlab::Metrics::Instrumentation.configure do |config|
config.instrument_instance_methods(Gitlab::Shell) config.instrument_instance_methods(Gitlab::Shell)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment