diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
commit | 9f46488805e86b1bc341ea1620b866016c2ce5ed (patch) | |
tree | f9748c7e287041e37d6da49e0a29c9511dc34768 /app/services/concerns/measurable.rb | |
parent | dfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff) |
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'app/services/concerns/measurable.rb')
-rw-r--r-- | app/services/concerns/measurable.rb | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/app/services/concerns/measurable.rb b/app/services/concerns/measurable.rb new file mode 100644 index 00000000000..5a74f15506e --- /dev/null +++ b/app/services/concerns/measurable.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +# In order to measure and log execution of our service, we just need to 'prepend Measurable' module +# Example: +# ``` +# class DummyService +# prepend Measurable +# +# def execute +# # ... +# end +# end + +# DummyService.prepend(Measurable) +# ``` +# +# In case when we are prepending a module from the `EE` namespace with EE features +# we need to prepend Measurable after prepending `EE` module. +# This way Measurable will be at the bottom of the ancestor chain, +# in order to measure execution of `EE` features as well +# ``` +# class DummyService +# def execute +# # ... +# end +# end +# +# DummyService.prepend_if_ee('EE::DummyService') +# DummyService.prepend(Measurable) +# ``` +# +module Measurable + extend ::Gitlab::Utils::Override + + override :execute + def execute(*args) + measuring? ? ::Gitlab::Utils::Measuring.new(base_log_data).with_measuring { super(*args) } : super(*args) + end + + protected + + # You can set extra attributes for performance measurement log. + def extra_attributes_for_measurement + defined?(super) ? super : {} + end + + private + + def measuring? + Feature.enabled?("gitlab_service_measuring_#{service_class}") + end + + # These attributes are always present in log. + def base_log_data + extra_attributes_for_measurement.merge({ class: self.class.name }) + end + + def service_class + self.class.name.underscore.tr('/', '_') + end +end |